forked from FluffyPira/picture-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbots.rb
More file actions
executable file
·127 lines (95 loc) · 3.15 KB
/
bots.rb
File metadata and controls
executable file
·127 lines (95 loc) · 3.15 KB
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
require 'twitter_ebooks'
require 'set'
# This is an example bot definition with event handlers commented out
# You can define and instantiate as many bots as you like
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
OAUTH_TOKEN = "" # oauth token for ebooks account
OAUTH_TOKEN_SECRET = "" # oauth secret for ebooks account
TWITTER_USERNAME = "" # Ebooks account username
AUTHOR_NAME = "" # Put your twitter handle in here
HASH = "" # Hashtag if you post to one
SPECIAL_WORDS = [''] # Words associated with your bot!
TRIGGER_WORDS = [''] # will trigger auto block
BLACKLIST = ['tnietzschequote'] # Users who don't want interaction; not currently in use
class MyBot < Ebooks::Bot
# Configuration here applies to all MyBots
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 = BLACKLIST
# Range in seconds to randomize delay when bot.delay is called
self.delay_range = 1..6
end
def on_startup
@pics = (Dir.entries("pictures/") - %w[.. . .DS_Store]).sort()
log @pics.take(5) # poll for consistency and tracking purposes.
@status_count = twitter.user.statuses_count
post_picture
prune_following
scheduler.every '3600' do
post_picture
end
end
def on_message(dm)
# Reply to a DM
poop = Random.new.bytes(5)
delay do
bot.reply dm, "Talk to @#{AUTHOR_NAME} #{poop}"
end
end
def on_follow(user)
follow(user.screen_name)
end
def on_mention(tweet)
tokens = Ebooks::NLP.tokenize(tweet.text)
special = tokens.find { |t| SPECIAL_WORDS.include?(t.downcase) }
trigger = tokens.find { |t| TRIGGER_WORDS.include?(t.downcase) }
if trigger
block(tweet)
end
if special
favorite(tweet)
end
end
def on_timeline(tweet)
tokens = Ebooks::NLP.tokenize(tweet.text)
special = tokens.find { |t| SPECIAL_WORDS.include?(t.downcase) }
if special
favorite(tweet) if rand < 0.20
end
end
def favorite(tweet)
delay do
super(tweet)
end
end
def next_index()
seq = (0..(@pics.size - 1)).to_a
seed = @status_count / @pics.size
r = Random.new(seed)
seq.shuffle!(random: r)
res = seq[@status_count % @pics.size]
@status_count = @status_count + 1
return res
end
def prune_following
following = Set.new(twitter.friend_ids.to_a)
followers = Set.new(twitter.follower_ids.to_a)
to_unfollow = (following - followers).to_a
log("Unfollowing user ids: #{to_unfollow}")
twitter.unfollow(to_unfollow)
end
def post_picture
pic = @pics[next_index]
pictweet(HASH,"pictures/#{pic}")
end
end
# Make a MyBot and attach it to an account
MyBot.new(TWITTER_USERNAME) 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
end