Skip to content

Instantly share code, notes, and snippets.

@cdl
Created January 4, 2015 00:55
Show Gist options
  • Save cdl/97344c39893c7d6bebf3 to your computer and use it in GitHub Desktop.
Save cdl/97344c39893c7d6bebf3 to your computer and use it in GitHub Desktop.
require 'twitter_ebooks'
# eva_gbooks (v1.0)
# written by @cdl for @evagiselle
class EvaBot < Ebooks::Bot
attr_accessor :original, :model, :model_path
# Configuration here applies to all EvaBots
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 = ['']
# Range in seconds to randomize delay when bot.delay is called
self.delay_range = 1..6
end
def good_content
return %w(put some good words here)
end
def on_startup
load_model!
scheduler.every '3h' do
# Tweet something every 3 hours
# See https://github.com/jmettraux/rufus-scheduler
tweet(model.make_statement())
end
end
def on_message(dm)
# Reply to a 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
# reply(tweet, "oh hullo")
tokens = Ebooks::NLP.tokenize(tweet.text)
interesting = tokens.find { |t| good_content.include?(t.downcase) }
if interesting
delay do
favorite(tweet)
end
end
delay do
reply(tweet, model.make_response(meta(tweet).mentionless, meta(tweet).limit))
end
end
def on_timeline(tweet)
# Favourite interesting tweets in the bot's timeline
tokens = Ebooks::NLP.tokenize(tweet.text)
interesting = tokens.find {|t| good_content.include?(t.downcase) }
if interesting
delay do
favorite(tweet)
end
end
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 EvaBot and attach it to an account
EvaBot.new("eva_gbooks") do |bot|
bot.access_token = "ACCESS_TOKEN" # Token connecting the app to this account
bot.access_token_secret = "ACCESS_TOKEN_SECRET" # Secret connecting the app to this account
bot.original = "evagiselle"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment