-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdekkai.rb
156 lines (126 loc) · 4.43 KB
/
dekkai.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/env ruby
# encoding: utf-8
require_relative "settings"
require_relative "tweet"
#==============================================================================
# ** Dekkai
#-----------------------------------------------------------------------------
# The skeleton of the main bot. All the main functions of the bot are
# performed by this class.
#==============================================================================
class Dekkai
#---------------------------------------------------------------------------
# * Initialisation
#---------------------------------------------------------------------------
def initialize(rest_client, stream_client)
@rest_client = rest_client
@stream_client = stream_client
@screen_name = @rest_client.user.screen_name
@tweet_queue = []
create_tweet_stream
load_scripts
init_all_scripts
end
# Delegate a thread for the timeline to be streamed concurrently
def create_tweet_stream
@stream = Thread.new {
@stream_client.user { |twitter_object|
case twitter_object
when Twitter::Tweet
parse_received_tweet(twitter_object)
when Twitter::Streaming::Event
parse_received_event(twitter_object)
else
parse_received_object(twitter_object)
end
}
}
end
def load_scripts
Settings::Enabled_Scripts.each { |script|
require_relative "scripts/#{script}"
}
end
def init_all_scripts
end
#----------------------------------------------------------------------------
# * Main loop
#----------------------------------------------------------------------------
def update
send_scheduled_tweets
update_all_scripts
sleep(1)
end
def update_all_scripts
end
#----------------------------------------------------------------------------
# * Stream Handlers
#----------------------------------------------------------------------------
def parse_received_tweet(tweet)
if tweet.text.split(" ").any? { |s| s == "@#{@screen_name}" }
update_mention(tweet)
end
end
def parse_received_event(event)
end
# To give scripts an endpoint for unknown objects that are received
def parse_received_object(twitter_object)
end
#----------------------------------------------------------------------------
# * API Calls
#----------------------------------------------------------------------------
def send_tweet(tweet, options={})
begin
@rest_client.update!(tweet.message, options)
puts "Sent Tweet: #{tweet.message}"
rescue Twitter::Error::DuplicateStatus
puts "Error: Status was a duplicate. Removing from tweet queue."
end
resolve_tweet(tweet)
end
def reply_to_mention(mention, reply)
send_tweet(reply, {:in_reply_to_status => mention})
@replied_to_mention = true
end
def follow_user(user)
begin
@rest_client.follow([user])
puts "Follow request sent to: #{user.screen_name}"
rescue Twitter::Error::Forbidden
puts "Follow request already sent to #{user}."
end
end
#----------------------------------------------------------------------------
# * Mention Handling
#----------------------------------------------------------------------------
def update_mention(tweet)
@replied_to_mention = false
end
def filter_links(text)
text.gsub!(/http.+?(?:\s|\Z)/, "")
text.gsub!(/pic\.tw.+?(?:\s|\Z)/, "")
return text
end
def strip_mentions(text)
return text.encode("utf-8", "utf-8").gsub(/@\w+/u, "")
end
#----------------------------------------------------------------------------
# * Tweet Resolution
#----------------------------------------------------------------------------
def resolve_tweet(tweet)
tweet.sent = true
end
#----------------------------------------------------------------------------
# * Helper Methods
#----------------------------------------------------------------------------
def send_scheduled_tweets
@tweet_queue.each { |tweet| send_tweet(tweet) if tweet.ready? }
@tweet_queue.reject! { |tweet| tweet.sent }
end
#----------------------------------------------------------------------------
# * Exit Routine
#----------------------------------------------------------------------------
def exit_gracefully
@stream.kill
end
end