added dm2twitter which posts DMs to Twitter

This commit is contained in:
Jeena Paradies 2010-01-21 23:05:51 +01:00
parent ac2d420505
commit c134ec0899

57
dm2twitter.rb Normal file
View file

@ -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 <spam@jeenaparadies.net>
#
#
# 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