-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
248 lines (205 loc) · 11.5 KB
/
bot.py
File metadata and controls
248 lines (205 loc) · 11.5 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import telepot
import sqlite3
import threading
import time
import datetime
import sys
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
if (content_type == 'text'):
message_words = msg['text'].strip().lower().split()
if (msg['text'][0] != "/"):
return
else:
if (message_words[0].replace("/addteam@live_scores_bot", "/addteam") == "/addteam"):
add_team(msg)
elif (message_words[0].replace("/removeteam@live_scores_bot", "/removeteam") == "/removeteam"):
remove_team(msg)
elif (message_words[0].replace("/listteams@live_scores_bot", "/listteams") == "/listteams"):
list_teams(msg)
elif (message_words[0].replace("/help@live_scores_bot", "/help") == "/help"):
help(msg)
def add_team(msg):
requested_team = " ".join(msg["text"].split(" ")[1:len(msg["text"].split(" "))]).strip()
if len(requested_team) == 0:
bot.sendMessage(msg['chat']['id'],
"Please inform the team to be added to the watchlist. Your team name must follow the nomenclature"
" defined by http://www.superplacar.com.br",
reply_to_message_id=msg['message_id'])
else:
existing_entry = notifications_cur.execute("SELECT * FROM notification WHERE team = ?",
(requested_team,)).fetchall()
if len(existing_entry) == 0:
notifications_cur.execute('INSERT INTO notification VALUES (?, ?)', (requested_team, msg['chat']['id']))
bot.sendMessage(msg['chat']['id'],
"Now monitoring team %s" % requested_team,
reply_to_message_id=msg['message_id'])
else:
if unicode(msg['chat']['id']) in existing_entry[0][1]:
bot.sendMessage(msg['chat']['id'],
"You're already monitoring %s" % requested_team,
reply_to_message_id=msg['message_id'])
else:
notifications_cur.execute('UPDATE notification SET subscribed_users = ? WHERE team = ?',
(existing_entry[0][1] + "," + str(msg['chat']['id']), requested_team))
bot.sendMessage(msg['chat']['id'],
"Now monitoring team %s" % requested_team,
reply_to_message_id=msg['message_id'])
notifications_db.commit()
def remove_team(msg):
requested_team = " ".join(msg["text"].split(" ")[1:len(msg["text"].split(" "))])
if len(requested_team) == 0:
bot.sendMessage(msg['chat']['id'],
"Please inform the team to be removed from the watchlist",
reply_to_message_id=msg['message_id'])
else:
existing_entry = notifications_cur.execute("SELECT * FROM notification WHERE team = ?",
(requested_team,)).fetchall()
if len(existing_entry) > 0:
groups_monitoring_team = existing_entry[0][1].split(",")
if not unicode(msg['chat']['id']) in groups_monitoring_team:
bot.sendMessage(msg['chat']['id'],
"You're not currently monitoring %s" % requested_team,
reply_to_message_id=msg['message_id'])
else:
groups_monitoring_team.remove(unicode(msg['chat']['id']))
if len(groups_monitoring_team) > 0:
subscribed_users = ",".join(groups_monitoring_team, )
notifications_cur.execute('UPDATE notification SET subscribed_users = ?',
(subscribed_users,))
else:
notifications_cur.execute('DELETE FROM notification WHERE team = ?',
(requested_team,))
bot.sendMessage(msg['chat']['id'],
"You're not monitoring %s anymore" % requested_team,
reply_to_message_id=msg['message_id'])
notifications_db.commit()
else:
bot.sendMessage(msg['chat']['id'],
"You're not currently monitoring %s" % requested_team,
reply_to_message_id=msg['message_id'])
def list_teams(msg):
subscriptions = notifications_cur.execute('SELECT team FROM notification WHERE subscribed_users LIKE ? ',
("%" + str(msg['chat']['id']) + "%",)).fetchall()
if len(subscriptions) == 0:
bot.sendMessage(msg['chat']['id'],
"You're following no teams currently",
reply_to_message_id=msg['message_id'], parse_mode='HTML')
else:
message = ""
for subscription in subscriptions:
message+="- %s\n" % subscription[0]
bot.sendMessage(msg['chat']['id'],
"You're currently monitoring the following teams:\n%s" % message,
reply_to_message_id=msg['message_id'], parse_mode='HTML')
def help(msg):
bot.sendMessage(msg['chat']['id'],
"Message me with /addteam <team> to start to monitor the scores of this team,"
" /removeteam <team> to stop monitoring a team or /listteams to check which teams"
" you're currently monitoring. In case of any issue please send an email to"
" offthread@gmail.com. \n\nCreated by OFF Thread <offthread@gmail.com>"
)
def tgram_bot():
bot.message_loop(handle)
def get_game_updates(day, home_team, away_team):
game_updates = {"started": False, "goal_home": False,
"goal_away": False, "scorer": False, "time": False}
game = notifications_cur.execute("SELECT * FROM game WHERE home_team_name=? AND away_team_name=? AND match_day=?",
(home_team, away_team, day)).fetchall()[0]
if game[3] == u"0x0":
game_updates["started"] = True
elif len(game[6]) > 0 and len(game[6].split(",")) > 0 and len(game[7]) > 0 and len(game[7].split(",")) > 0:
home_team_last_goal = game[6].split(",")[-1]
away_team_last_goal = game[7].split(",")[-1]
if home_team_last_goal.split(" ")[1] == "2T" and away_team_last_goal.split(" ")[1] == "1T":
game_updates["goal_home"] = True
game_updates["time"] = home_team_last_goal
elif home_team_last_goal.split(" ")[1] == "1T" and away_team_last_goal.split(" ")[1] == "2T":
game_updates["goal_away"] = True
game_updates["time"] = away_team_last_goal
else:
if int(home_team_last_goal.split(" ")[0].replace("'", "")) > int(
away_team_last_goal.split(" ")[0].replace("'", "")):
game_updates["goal_home"] = True
game_updates["time"] = home_team_last_goal
else:
game_updates["goal_away"] = True
game_updates["time"] = away_team_last_goal
else:
home_team_last_goal = game[6].split(",")[-1]
away_team_last_goal = game[7].split(",")[-1]
if len(game[6]) == 0:
game_updates["goal_away"] = True
game_updates["time"] = away_team_last_goal
else:
game_updates["goal_home"] = True
game_updates["time"] = home_team_last_goal
if game_updates["goal_home"]:
game_updates["scorer"] = game[4].split(",")[-1]
elif game_updates["goal_away"]:
game_updates["scorer"] = game[5].split(",")[-1]
game_updates["scoreboard"] = game[3]
return game_updates
def listenToUpdates():
while True:
print "Checking for updates"
dt = datetime.datetime.now()
today = datetime.date(dt.year, dt.month, dt.day)
scores_to_notify = notifications_cur.execute("SELECT * FROM game WHERE notify = ? AND match_day = ?",
(1, today)).fetchall()
for score in scores_to_notify:
home_team = score[1]
away_team = score[2]
updates = get_game_updates(today, home_team, away_team)
users_to_notify_for_home_team = notifications_cur.execute("SELECT * FROM notification WHERE team = ?",
(home_team,)).fetchall()
notified_users = []
if len(users_to_notify_for_home_team) > 0:
for user in users_to_notify_for_home_team[0][1].split(","):
notified_users.append(user)
if updates["started"]:
bot.sendMessage(user, "%s x %s has started. Scoreboard is 0x0!" % (home_team, away_team))
elif updates["goal_home"]:
bot.sendMessage(user, "Goal from %s! %s scores at %s. Scoreboard is now %s %s %s!" %
(home_team, updates["scorer"], updates["time"], home_team, updates["scoreboard"],
away_team))
else:
bot.sendMessage(user, "Goal from %s! %s scores at %s. Scoreboard is now %s %s %s!" %
(away_team, updates["scorer"], updates["time"], home_team, updates["scoreboard"],
away_team))
users_to_notify_for_away_team = notifications_cur.execute("SELECT * FROM notification WHERE team = ?",
(away_team,)).fetchall()
if len(users_to_notify_for_away_team) > 0:
for user in users_to_notify_for_away_team[0][1].split(","):
if user not in notified_users:
if updates["started"]:
bot.sendMessage(user, "%s x %s has started. Scoreboard is 0x0!" % (home_team, away_team))
elif updates["goal_away"]:
bot.sendMessage(user, "Goal from %s! %s scores at %s. Scoreboard is now %s %s %s!" %
(away_team, updates["scorer"], updates["time"], home_team, updates["scoreboard"],
away_team))
else:
bot.sendMessage(user, "Goal from %s! %s scores at %s. Scoreboard is now %s %s %s!" %
(home_team, updates["scorer"], updates["time"], home_team, updates["scoreboard"],
away_team))
notifications_cur.execute("UPDATE game SET notify = ? WHERE home_team_name = ? AND away_team_name = ?"
"AND match_day=?", (0, home_team, away_team, today))
notifications_db.commit()
time.sleep(30)
if len(sys.argv) > 1:
notifications_db = sqlite3.connect("live_scores.db", check_same_thread=False)
notifications_cur = notifications_db.cursor()
notifications_cur.execute(
'CREATE TABLE IF NOT EXISTS notification (team TEXT, subscribed_users TEXT )')
notifications_db.commit()
bot = telepot.Bot(sys.argv[1])
t = threading.Thread(target=tgram_bot)
t.daemon = True
t.start()
print("I have successfully spun off the telegram bot")
listenToUpdates()
else:
print "usage: bot.py bot_key\n" \
"prog.py: error: the following arguments are required: bot_key"