Skip to content

Instantly share code, notes, and snippets.

@roberto
Created August 21, 2008 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roberto/6555 to your computer and use it in GitHub Desktop.
Save roberto/6555 to your computer and use it in GitHub Desktop.
smtp:
address: smtp.server.com
port: 25
user_name: your@email.com
password: your_password
authentication: plain
twitter:
user: your_user
password: your_password
start: 2008-05-10
mobile:
email: 7599999999@claroonline.com.br
require 'rubygems'
require 'net/smtp'
require 'twitter'
require 'yaml'
require "time"
require 'logger'
log = Logger.new(STDOUT)
log.level = Logger::DEBUG
log.info "Starting..."
File.join(File.dirname(__FILE__), "twitter-bot.cache")
log.info "Loading configuration"
config = YAML.load_file(File.join(File.dirname(__FILE__),"config.yml"))
smtp_conf = config["smtp"]
twitter_conf = config["twitter"]
mobile_conf = config["mobile"]
log.debug "Configuration loaded"
log.debug "Loading cache..."
LAST_CHECK = File.join(File.dirname(__FILE__), "twitter-date.cache")
dm_since = File.exist?(LAST_CHECK) ? IO.read(LAST_CHECK) : twitter_conf["start"]
now = nil
log.debug "dm_since = #{dm_since}"
log.info "Starting twitter..."
twitter = Twitter::Base.new(twitter_conf["user"],twitter_conf["password"])
if smtp_conf["address"].include?("gmail")
log.info "Gmail configuration"
require 'tlsmail'
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
end
log.info "SMTP and twitter loop"
Net::SMTP.start(smtp_conf['address'], smtp_conf['port'].to_i,
'localhost.localdomain', smtp_conf['user_name'],
smtp_conf['password'], smtp_conf['authentication'].to_sym) do |smtp|
twitter.direct_messages(dm_since).each do |message|
now ||= message.created_at
msg = <<_________EOM
Subject: dm
Date: #{Time.now.rfc2822}
@#{message.sender_screen_name.strip}: #{message.text.strip}
_________EOM
log.info "sending @#{message.sender_screen_name.strip}: #{message.text.strip}"
smtp.send_message(msg, smtp_conf['user_name'], config['mobile']['email'])
end
end
log.info "SMTP and twitter loop finished"
log.debug "Writing cache.."
File.open LAST_CHECK, 'w' do |f|
f.write now
end if now
log.info "END!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment