-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.rb
139 lines (113 loc) · 3.49 KB
/
bots.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require 'dotenv'
Dotenv.load
require 'twitter_ebooks'
# Information about a particular Twitter user we know
class UserInfo
attr_reader :username
# @return [Integer] how many times we can pester this user unprompted
attr_accessor :pesters_left
# @param username [String]
def initialize(username)
@username = username
@pesters_left = 1
end
end
class CloneBot < Ebooks::Bot
attr_accessor :original, :model, :model_path
def configure
# Configuration for all CloneBots
self.consumer_key = ENV['CONSUMER_KEY']
self.consumer_secret = ENV['CONSUMER_SECRET']
self.blacklist = []
self.delay_range = 1..6
@userinfo = {}
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,8,16 * * *' do
# Each day at midnight, post a single tweet
tweet(model.make_statement)
end
end
def on_message(dm)
delay do
reply(dm, model.make_response(dm.text))
end
end
def on_mention(tweet)
# Become more inclined to pester a user when they talk to us
userinfo(tweet.user.screen_name).pesters_left += 1
delay do
reply(tweet, model.make_response(meta(tweet).mentionless, meta(tweet).limit))
end
end
def on_timeline(tweet)
return if tweet.retweeted_status?
return unless can_pester?(tweet.user.screen_name)
tokens = Ebooks::NLP.tokenize(tweet.text)
interesting = tokens.find { |t| top100.include?(t.downcase) }
very_interesting = tokens.find_all { |t| top20.include?(t.downcase) }.length > 2
delay do
if very_interesting
favorite(tweet) if rand < 0.5
retweet(tweet) if rand < 0.1
if rand < 0.01
userinfo(tweet.user.screen_name).pesters_left -= 1
reply(tweet, model.make_response(meta(tweet).mentionless, meta(tweet).limit))
end
elsif interesting
favorite(tweet) if rand < 0.05
if rand < 0.001
userinfo(tweet.user.screen_name).pesters_left -= 1
reply(tweet, model.make_response(meta(tweet).mentionless, meta(tweet).limit))
end
end
end
end
# Find information we've collected about a user
# @param username [String]
# @return [Ebooks::UserInfo]
def userinfo(username)
@userinfo[username] ||= UserInfo.new(username)
end
# Check if we're allowed to send unprompted tweets to a user
# @param username [String]
# @return [Boolean]
def can_pester?(username)
userinfo(username).pesters_left > 0
end
# # Only follow our original user or people who are following our original user
# # @param user [Twitter::User]
# def can_follow?(username)
# @original.nil? || username == @original || twitter.friendship?(username, @original)
# end
# def favorite(tweet)
# if can_follow?(tweet.user.screen_name)
# super(tweet)
# else
# log "Unfollowing @#{tweet.user.screen_name}"
# twitter.unfollow(tweet.user.screen_name)
# end
# end
# def on_follow(user)
# if can_follow?(user.screen_name)
# follow(user.screen_name)
# else
# log "Not following @#{user.screen_name}"
# end
# end
private
def load_model!
return if @model
@model_path ||= "model/all.model"
log "Loading model #{model_path}"
@model = Ebooks::Model.load(model_path)
end
end
CloneBot.new("kellyirc_ebooks") do |bot|
bot.access_token = ENV['ACCESS_TOKEN']
bot.access_token_secret = ENV['ACCESS_TOKEN_SECRET']
bot.original = "rahatarmanahmed"
end