Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
97 lines (75 sloc) 2.85 KB
require 'twitter_ebooks'
require 'dotenv'
Dotenv.load(".env")
CONSUMER_KEY = ENV['EBOOKS_CONSUMER_KEY']
CONSUMER_SECRET = ENV['EBOOKS_CONSUMER_SECRET']
OAUTH_TOKEN = ENV['EBOOKS_OAUTH_TOKEN']
OAUTH_TOKEN_SECRET = ENV['EBOOKS_OAUTH_TOKEN_SECRET']
# This is an example bot definition with event handlers commented out
# You can define and instantiate as many bots as you like
class MyBot < Ebooks::Bot
# Configuration here applies to all MyBots
attr_accessor :original, :model, :model_path
def configure
# Consumer details come from registering an app at https://dev.twitter.com/
# Once you have consumer details, use "ebooks auth" for new access tokens
self.consumer_key = CONSUMER_KEY # Your app consumer key
self.consumer_secret = CONSUMER_SECRET # Your app consumer secret
# Users to block instead of interacting with
self.blacklist = ['food_libs']
# Range in seconds to randomize delay when bot.delay is called
self.delay_range = 1..6
end
def top100; @top100 ||= model.keywords.take(100); end
def top20; @top20 ||= model.keywords.take(20); end
def on_startup
load_model!
# scheduler.cron '0 0 * * *' do
# # Each day at midnight, post a single tweet
# tweet(model.make_statement)
# end
scheduler.every '69m' do
# generate parts of a tweet
prefix = model.make_statement(15)
statement = model.make_statement(Random.rand(15...60))
suffix = model.make_statement(Random.rand(2...15))
# randomize order that tweet is composed
letters = [prefix, statement, suffix]
letters = letters.shuffle
# make sure combined fragments are under twitter char limit (140)
if (letters[0].length + letters[1].length + letters[2].length + 2) < 140
tweet(letters[0] + ' ' + letters[1] + ' ' + letters[2])
else
# tweet a random fragment
tweet(letters[Random.rand(0..2)])
end
end
end
def on_message(dm)
delay do
reply(dm, model.make_response(dm.text))
end
end
def on_follow(user)
# Follow a user back
follow(user.screen_name)
end
def on_mention(tweet)
# Reply to a mention
end
def on_timeline(tweet)
end
private
def load_model!
return if @model
@model_path ||= "model/#{original}.model"
log "Loading model #{model_path}"
@model = Ebooks::Model.load(model_path)
end
end
# Make a MyBot and attach it to an account
MyBot.new("food_libs") do |bot|
bot.access_token = OAUTH_TOKEN # Token connecting the app to this account
bot.access_token_secret = OAUTH_TOKEN_SECRET # Secret connecting the app to this account
bot.original = "foodpyramids"
end