From c134ec0899dbf32c55f0df0ab5251d295311b27e Mon Sep 17 00:00:00 2001 From: Jeena Paradies Date: Thu, 21 Jan 2010 23:05:51 +0100 Subject: [PATCH] added dm2twitter which posts DMs to Twitter --- dm2twitter.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 dm2twitter.rb diff --git a/dm2twitter.rb b/dm2twitter.rb new file mode 100644 index 0000000..4891d83 --- /dev/null +++ b/dm2twitter.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby + +require "twitter" + +# dm2twitter.rb looks up the latest direct messages and posts +# them as a update if the sender was in the whitelist. +# +# Licence: BSD +# Author: Jeena Paradies +# +# +# You could use it with Postfix and add to /etc/aliases +# myuser: myuser, "|/home/myuser/scripts/sender.sh" +# +# Or you could run it with help of a crontab every couple +# of minutes. + +# Change this: +USERNAME = "myusername" +PASSWORD = "mypassword" + +SENDER_WHITELIST = %w(someuser otheruser thirduser) + +# If you already have DMs which you don't want to post +# create a new file with just the last ID number of the +# unwanted DMs. +LASTID_PATH = "dm2twitter-lastid.txt" + + +# +# Do not change this +# +client = Twitter::Base.new(Twitter::HTTPAuth.new(USERNAME, PASSWORD)) + +lastid = nil +lastid = File.read(LASTID_PATH).to_i if File.writable? LASTID_PATH + +client.direct_messages(:since_id => lastid).each do |message| + + if SENDER_WHITELIST.include? message["sender_screen_name"] + + via = " (via #{message["sender_screen_name"]})" + update_text = message["text"] + via + + unless update_text.length > 140 + client.update(update_text) + File.open(LASTID_PATH, 'w') { |f| f.write(message["id"]) } + else + client.direct_message_create( + message["sender_screen_name"], + "Sorry your message was too long, was: #{update_text.length} chars, should be: #{140 - via.length} chars." + ) + end + + end + +end \ No newline at end of file