-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbegbot.py
752 lines (640 loc) · 31.2 KB
/
begbot.py
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
""" This is the Bouncing Egg bot backend for a Telegram bot. """
# Imports
import telegram
import ts3.query
import time
import datetime
import os
import sqlite3
import json
import urllib.request
import urllib.error
import pymysql
import copy
# Global Variable Dictionaries for Configuration, Emoji, and Messages.
CONFIG = {"VERSION": "0.032; 29.12.2015"}
EMOJI = {}
MSGS = {}
# Sets whether or not the bot should archive to a remove MySQL database.
# Note: This requires the mysql fields set in the configuration file.
REMOTE_ARCHIVE = True
def main():
"""
Bot startup routine, loads the configuration/emoji and enters the main loop.
"""
load_cfg()
init_emoji()
init_msgs()
bot = telegram.Bot(token=CONFIG["TOKEN"])
bot_info = bot.getMe()
CONFIG["BOTINFO"] = bot_info
started = datetime.datetime.now()
try:
CONFIG["LAST_UPDATE_ID"] = bot.getUpdates()[-1].update_id + 1
except IndexError:
CONFIG["LAST_UPDATE_ID"] = None
print("BEGBot connected: Name={}, ID={}, Started={}".format(bot_info.username,
bot_info.id, started))
print("SessionID: {}".format(CONFIG["SESSION_ID"]))
while True:
loop(bot) # this includes a few seconds timeout
send_keep_alive(CONFIG["SESSION_ID"])
def loop(bot):
"""
Main loop of the bot, collects updates from the Telegram API and performs actions on them.
@param bot The telegram bot instance created by the telegram module.
"""
global CONFIG
# Management of the Birthday check. Reset from 0-1, check from 9-10.
now = datetime.datetime.now()
if now.hour == 0:
CONFIG["TODAYS_BDAY_CHECK"] = False
if now.hour == 9 and not CONFIG["TODAYS_BDAY_CHECK"]:
bday_messages = check_for_birthdays()
for bdm in bday_messages:
bot.sendMessage(chat_id=CONFIG["BEG_ID"], text=bdm)
CONFIG["TODAYS_BDAY_CHECK"] = True
# Receiving and processing updates.
try:
for u in bot.getUpdates(offset=CONFIG["LAST_UPDATE_ID"], timeout=6):
archive(u, bot)
# UTF-8 Encoded message text
if u.message is not None:
message_text = u.message.text.encode('utf-8')
sender = u.message.from_user.id
if sender not in CONFIG["KNOWN_USERS"]:
add_user(u.message.from_user)
# print("Received Message from {}: {}".format(sender, message_text))
# Public string matching
response = match_text(message_text)
if response is not None:
bot.sendMessage(chat_id=u.message.chat_id, text=response)
# BEG only commands
# Maybe check if u.message.chat.type == "private" or "group", if necessary.
if u.message.text == "/ts3" or u.message.text == "/ts3{}".format(CONFIG["BOTINFO"].name):
if is_beg(sender):
ts3status = get_ts3_status()
bot.sendMessage(chat_id=u.message.chat_id, text=ts3status)
else:
bot.sendMessage(chat_id=u.message.chat_id,
text=MSGS["ONLY_FOR_BEG"])
if u.message.text == "/steam" or u.message.text == "/steam{}".format(CONFIG["BOTINFO"].name):
if is_beg(sender):
steamstats = get_steam_status()
bot.sendMessage(chat_id=u.message.chat_id, text=steamstats)
else:
bot.sendMessage(chat_id=u.message.chat_id,
text=MSGS["ONLY_FOR_BEG"])
if u.message.text == "/version" or u.message.text == "/version{}".format(CONFIG["BOTINFO"].name):
if is_beg(sender):
bot.sendMessage(chat_id=u.message.chat_id,
text="BEGBot - Current Version: {}".format(CONFIG["VERSION"]))
else:
bot.sendMessage(chat_id=u.message.chat_id,
text=MSGS["ONLY_FOR_BEG"])
# Admin only commands
if u.message.text == "/session" or u.message.text == "/session{}".format(CONFIG["BOTINFO"].name):
if is_admin(sender):
(s_id, s_start, s_end, s_duration) = get_session(CONFIG["SESSION_ID"])
bot.sendMessage(chat_id=u.message.chat_id,
text="Session: id={}, start={}, lastka={}, duration={}s"
.format(s_id, s_start, s_end, s_duration))
else:
bot.sendMessage(chat_id=u.message.chat_id, text=MSGS["ONLY_FOR_ADMINS"])
if u.message.text == "/listusers" or u.message.text == "/listusers{}".format(CONFIG["BOTINFO"].name):
if is_admin(sender):
users = "\n".join([str(u) for u in get_all_users()])
bot.sendMessage(chat_id=u.message.chat_id, text="Users:\n{}".format(users))
else:
bot.sendMessage(chat_id=u.message.chat_id, text=MSGS["ONLY_FOR_ADMINS"])
if u.message.text == "/listnonbeg" or u.message.text == "/listnonbeg{}".format(CONFIG["BOTINFO"].name):
if is_admin(sender):
nonbeg = "\n".join([str(u) for u in get_non_beg_users()])
bot.sendMessage(chat_id=u.message.chat_id,
text="Non-BEG Users:\n{}".format(nonbeg))
else:
bot.sendMessage(chat_id=u.message.chat_id, text=MSGS["ONLY_FOR_ADMINS"])
if u.message.text.startswith("/setbday ") or u.message.text.startswith("/setbday{} ".format(CONFIG["BOTINFO"].name)):
if is_admin(sender):
params = message_text[9:].split()
if len(params) != 2:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Wrong parameter count."
.format(EMOJI["BANG"]))
else:
try:
uid = int(params[0])
bday = params[1].decode("utf-8")
if len(bday) != 10:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Malformed date, expected format:"
" \"YYYY-MM-DD\"."
.format(EMOJI["BANG"]))
else:
result = set_bday(uid, bday)
if result is None:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Unknown user ID."
.format(EMOJI["BANG"]))
else:
(uname, _, tid) = result
bot.sendMessage(chat_id=u.message.chat_id,
text="Successfully set birthday for"
" {} ({}) to {}."
.format(uname, tid, bday))
except ValueError:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Malformed user ID."
.format(EMOJI["BANG"]))
if u.message.text.startswith("/addbeg ") or u.message.text.startswith("/addbeg{} ".format(CONFIG["BOTINFO"].name)):
if is_admin(sender):
try:
uid = int(message_text[8:])
uinfo = get_user_by_id(uid)
if uinfo is None:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Unknown user ID."
.format(EMOJI["BANG"]))
else:
add_user_to_beg(uid)
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Successfully added: {} ({}) {} Welcome! {}"
.format(EMOJI["PARTY_BALL"], uinfo[1], uinfo[3],
EMOJI["SPARKLE"], EMOJI["PARTY_CONE"]))
except ValueError:
bot.sendMessage(chat_id=u.message.chat_id,
text="{} Error. Malformed user ID."
.format(EMOJI["BANG"]))
else:
bot.sendMessage(chat_id=u.message.chat_id, text=MSGS["ONLY_FOR_ADMINS"])
CONFIG["LAST_UPDATE_ID"] = u.update_id + 1
except telegram.TelegramError as err:
print("{} Telegram Error, sleeping {}s and trying again: {}"
.format(datetime.datetime.now(), CONFIG["ERROR_TIMEOUT"], err))
time.sleep(CONFIG["ERROR_TIMEOUT"])
except ValueError as err:
print("{} Value Error, sleeping {}s and trying again: {}"
.format(datetime.datetime.now(), CONFIG["ERROR_TIMEOUT"], err))
time.sleep(CONFIG["ERROR_TIMEOUT"])
except urllib.error.URLError as err:
print("{} URL Error, sleeping {}s and trying again: {}"
.format(datetime.datetime.now(), CONFIG["ERROR_TIMEOUT"], err))
time.sleep(CONFIG["ERROR_TIMEOUT"])
def archive(update, bot):
"""
Archives/persists the received update into the database.
@param update: The update as defined by the telegram module.
@param bot: The Telegram bot instance.
"""
if update.edited_message is not None:
print("Someone edited a message..... grrrr.")
# Persist into remote MySQL database, if enabled.
if REMOTE_ARCHIVE:
mysql_con = pymysql.connect(host=CONFIG["MYSQL_SRV"], user=CONFIG["MYSQL_USR"],
password=CONFIG["MYSQL_PWD"], db=CONFIG["MYSQL_DB"],
charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor)
try:
with mysql_con.cursor() as mysql_c:
# Copy update object and encode message text to UTF-8
update_copy = copy.deepcopy(update)
update_copy.edited_message.text = update.edited_message.text.encode("utf-8")
mysql_c.execute("insert into edited_message (message_id, json_obj) values (%s, %s);",
(update.edited_message.message_id, str(update_copy)))
finally:
mysql_con.close()
elif update.message is None:
print("Error: message is None ... Update is:")
print(update)
else:
if update.message.chat.type == "group":
# Persist into local SQLite3 database.
user = update.message.from_user
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("insert into message (session_id, telegram_id, message_id, update_id, group_id,"
" content, received) values (?, ?, ?, ?, ?, ?, datetime('now'))",
(CONFIG["SESSION_ID"], user.id, update.message.message_id, update.update_id,
update.message.chat.id, str(update)))
con.commit()
c.close()
con.close()
# Persist into remote MySQL database, if enabled.
if REMOTE_ARCHIVE:
mysql_con = pymysql.connect(host=CONFIG["MYSQL_SRV"], user=CONFIG["MYSQL_USR"],
password=CONFIG["MYSQL_PWD"], db=CONFIG["MYSQL_DB"],
charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor)
try:
with mysql_con.cursor() as mysql_c:
# Copy update object and encode message text to UTF-8
update_copy = copy.deepcopy(update)
update_copy.message.text = update.message.text.encode("utf-8")
mysql_c.execute("insert into message (session_id, telegram_id, message_id, update_id, group_id,"
" content, received) values (%s, %s, %s, %s, %s, %s, NOW());",
(CONFIG["SESSION_ID"], user.id, update.message.message_id, update.update_id,
update.message.chat.id, str(update_copy)))
finally:
mysql_con.close()
if update.message.sticker is not None:
download_file(update.message.sticker.file_id, bot, ftype="sticker")
download_file(update.message.sticker.thumb.file_id, bot, ftype="sticker_thumb")
elif update.message.document is not None:
download_file(update.message.document.file_id, bot, ftype=str(update.message.document.mime_type))
if update.message.document.thumb is not None:
download_file(update.message.document.thumb.file_id, bot, ftype="document_thumb")
elif update.message.voice is not None:
download_file(update.message.voice.file_id, bot, ftype=str(update.message.voice.mime_type))
elif update.message.video is not None:
download_file(update.message.video.file_id, bot, ftype="video")
download_file(update.message.video.thumb.file_id, bot, ftype="video_thumb")
elif update.message.photo is not None:
for i, p in enumerate(update.message.photo):
download_file(p.file_id, bot, ftype="photo_s{}".format(i))
def download_file(file_id, bot, ftype=None):
"""
Downloads a file from Telegram and stores it to the file system
and a description into the remote MySQL database, if enabled.
@param file_id: File ID of the file do be downloaded
@param bot: The telegram bot instance.
@param ftype: File type description.
@return: The file path of the downloaded file.
"""
file_path = "{}/{}".format(CONFIG["FILES_DIR"], file_id)
if not os.path.isfile(file_path):
file = bot.getFile(file_id=file_id)
file.download(custom_path=str(file_id))
os.rename(str(file_id), file_path)
if REMOTE_ARCHIVE:
mysql_con = pymysql.connect(host=CONFIG["MYSQL_SRV"], user=CONFIG["MYSQL_USR"],
password=CONFIG["MYSQL_PWD"], db=CONFIG["MYSQL_DB"],
charset="utf8mb4", cursorclass=pymysql.cursors.DictCursor)
try:
with mysql_con.cursor() as mysql_c:
mysql_c.execute("insert into file (file_id, type, received) values (%s, %s, NOW());",
(str(file_id), ftype))
finally:
mysql_con.close()
return file_path
def match_text(text):
"""
Matches the given text to several patterns and returns a string if a bot reply is
warranted, or None if not.
@param text The text that we try to match the patterns to.
"""
# Matches an Imgur direct link.
text = text.decode("utf-8")
if text.startswith("http://i.imgur.com/") and not text.endswith(".gifv"):
site = urllib.request.urlopen(text)
if site.getheader("Content-Type") == "image/gif":
# GIF not linked as GIFV
sitesize = int(site.getheader("Content-Length")) / 1024 / 1024
newsite = "{}.gifv".format(text[:-4])
return "{} Warning: Non-GIFV GIF detected! You don't want to download" \
" {:.2f}MB, do you? Here's the proper link: {} {}" \
.format(EMOJI["WARNING"], sitesize, EMOJI["EARTH_AFRICA_EUROPE"], newsite)
# If nothing has been matched until this point, just return None.
return None
def send_keep_alive(session_id):
"""
Saves the current time to the database to keep track of the last known working time in
case the bot crashed.
@param session_id The ID of the current bot session.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
con.execute("update session set end = datetime('now') where id=?", (session_id,))
con.commit()
con.close()
def get_session(session_id):
"""
Gets information (start time, end time, and duration about the current session
by session_id.
@param session_id The ID of the current bot session.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select id, start, end, strftime('%s', end) - strftime('%s', start) as duration "
"from session where id=?",
(session_id,))
result = c.fetchone()
c.close()
con.close()
return result
def set_bday(user_id, bday):
"""
Adds the given birth date for the given user to the database.
@param user_id The user id as it is in the database.
@param bday The birth date as a string in the format "YYYY-MM-DD".
@return Returns a tuple of (user name, first name, telegram id) for the given user id.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("update user set bday = ? where id = ?", (bday, user_id))
con.commit()
c.execute("select username, firstname, telegram_id from user where id = ?", (user_id,))
result = c.fetchone()
c.close()
con.close()
return result
def add_user(user):
"""
Adds the given user to the database and the KNOWN_USERS dictionary.
@param user The user as defined by the telegram module.
"""
global CONFIG
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("insert into user (username, firstname, lastname, telegram_id, added, beg, admin)"
" values (?, ?, ?, ?, datetime('now'), 0, 0)",
(user.username, user.first_name, user.last_name, user.id))
con.commit()
c.close()
con.close()
CONFIG["KNOWN_USERS"].add(user.id)
def get_user_by_id(user_id):
"""
Returns information (username, first name, last name, telegram id)) about a user
identified by his user_id (not telegram id!).
@param user_id The user id as it is in the database.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select username, firstname, lastname, telegram_id from user where id=?",
(user_id,))
result = c.fetchone()
c.close()
con.close()
return result
def get_all_users():
"""
Returns a list of all users in the database.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select * from user")
result = c.fetchall()
c.close()
con.close()
return result
def get_non_beg_users():
"""
Returns a list of all users in the database that are not tagged as BEG members.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select id, username, firstname, lastname, telegram_id from user where beg != 1")
result = c.fetchall()
c.close()
con.close()
return result
def add_user_to_beg(user_id):
"""
Tags the user with the given user_id as a BEG member.
@param user_id The user id as it is in the database.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
con.execute("update user set beg = 1 where id=?", (user_id,))
con.commit()
con.close()
def is_admin(telegram_id):
"""
Checks if the user with the given telegram id has administrator permissions.
@param telegram_id The users telegram id.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select count(id) from user where admin=1 and telegram_id=?", (telegram_id,))
(result,) = c.fetchone()
c.close()
con.close()
return result
def is_beg(telegram_id):
"""
Checks if the user with the given telegram id is tagged as a BEG member.
@param telegram_id The users telegram id.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select count(id) from user where beg=1 and telegram_id=?", (telegram_id,))
(result,) = c.fetchone()
c.close()
con.close()
return result
def check_for_birthdays():
"""
Checks if one or more of the bouncing egg members have a birthday today and returns a
list of strings corresponding to birthday wishes for each user with a birthday today.
"""
con = sqlite3.connect(CONFIG["DB_FILE"])
c = con.cursor()
c.execute("select id, firstname, bday from user where beg = 1")
result = c.fetchall()
c.close()
con.close()
msgs = []
for r in result:
if r[2] is not None:
bday = r[2].split("-")
if len(bday) == 3:
try:
for i, bde in enumerate(bday):
bday[i] = int(bde)
today = datetime.datetime.now()
today = (today.month, today.day)
bday = (bday[1], bday[2])
if today == bday:
msg = "{} {} {} Happy Birthday, {}! {} {} {}" \
.format(EMOJI["PARTY_CONE"], EMOJI["SPARKLE"],
EMOJI["BALLOON"], r[1], EMOJI["BALLOON"],
EMOJI["CAKE"], EMOJI["PRESENT"])
msgs.append(msg)
except ValueError:
print("Encountered malformed birthday for user id {}, "
"please check and correct.".format(r[0]))
return msgs
def get_steam_status():
"""
Connects to the Steam Web API as defined in the cfguration file and returns a string
resembling the currently online users in steam, or None if the request failed.
"""
states = {
0: "Offline",
1: "Online",
2: "Busy",
3: "Away",
4: "Snooze",
5: "Looking to Trade",
6: "Looking to Play"
}
site = urllib.request.urlopen("http://api.steampowered.com/ISteamUser/"
"GetPlayerSummaries/v0002/?key={}&steamids={}"
.format(CONFIG["STEAM_API_KEY"], ",".join(CONFIG["STEAM_IDS"])))
content = site.read()
try:
data = json.loads(content.decode("utf-8"))
pinfo = []
for p in data["response"]["players"]:
if p["personastate"] != 0:
pstate = states[p["personastate"]]
if "gameid" in p:
pstate = "{} / InGame".format(pstate)
pinfo.append("{} {} {}".format(p["personaname"], EMOJI["SQUIGGLY_LINE"], pstate))
if len(pinfo) > 0:
return "{}{} Steam {}{}\n{} {}" \
.format(EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"],
EMOJI["ORANGE_RHOMBUS"], "\n{} ".format(EMOJI["ORANGE_RHOMBUS"]).join(pinfo))
else:
return "{}{} Steam {}{}\nThere's nobody online {}" \
.format(EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"],
EMOJI["THICK_DASH"], EMOJI["SAD"])
except ValueError:
print("Error: Steam API sent empty response.")
return "{}{} Steam {}{}\nThe Steam API sent nothing {}" \
.format(EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["SAD"])
def get_ts3_status():
"""
Connects to the TeamSpeak 3 server as defined in the configuration file and returns
a string resembling the currently online users.
"""
try:
ts3con = ts3.query.TS3Connection(CONFIG["TS3_SRV"])
except ConnectionRefusedError as err:
print("TS3 connection failed: {}".format(err))
return "{} TS3 Error {}".format(EMOJI["BANG"], EMOJI["BANG"])
channels = {}
clients = []
try:
ts3con.login(client_login_name=CONFIG["TS3_USR"], client_login_password=CONFIG["TS3_PWD"])
ts3con.use(sid=1)
resp = ts3con.clientlist()
for client in resp.parsed:
if not client["client_nickname"].startswith("{} from ".format(CONFIG["TS3_USR"])):
channels[client["cid"]] = 0
clients.append((client["client_nickname"], client["cid"]))
if len(clients) == 0:
return "{}{} TeamSpeak 3 {}{}\n There's nobody online {}" \
.format(EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"],
EMOJI["THICK_DASH"], EMOJI["SAD"])
resp = ts3con.channellist()
for channel in resp.parsed:
if channel["cid"] in channels:
channels[channel["cid"]] = {"name": channel["channel_name"], "clients": []}
for (cname, cid) in clients:
channels[cid]["clients"].append(cname)
entries = []
for c in channels:
clientlines = []
for client in range(0, len(channels[c]["clients"])):
if client == len(channels[c]["clients"]) - 1:
clientlines.append(" {} {} {}".format(EMOJI["CROSSING_L"], EMOJI["BLUE_BALL"],
channels[c]["clients"][client]))
else:
clientlines.append(" {} {} {}".format(EMOJI["CROSSING_T"], EMOJI["BLUE_BALL"],
channels[c]["clients"][client]))
entries.append("{} {}\n{}".format(EMOJI["SPEECH_BUBBLE"], channels[c]["name"],
"\n".join(clientlines)))
return "{}{} TeamSpeak 3 {}{}\n{}" \
.format(EMOJI["THICK_DASH"], EMOJI["THICK_DASH"], EMOJI["THICK_DASH"],
EMOJI["THICK_DASH"], "\n".join(entries))
except ts3.query.TS3QueryError as err:
print("TS3 query failed:", err.resp.error["msg"])
return "{} TS3 Error {}".format(EMOJI["BANG"], EMOJI["BANG"])
def load_cfg():
"""
Loads bot settings and cfguration from the file cfg.json.
"""
global CONFIG
# Default cfg values
CONFIG["ERROR_TIMEOUT"] = 3
CONFIG["LAST_UPDATE_ID"] = 0
CONFIG["SESSION_ID"] = -1
CONFIG["KNOWN_USERS"] = {}
CONFIG["DB_FILE"] = ""
CONFIG["ADMIN_ID"] = 1
CONFIG["BEG_ID"] = -1
CONFIG["TOKEN"] = ""
CONFIG["TS3_USR"] = ""
CONFIG["TS3_PWD"] = ""
CONFIG["TS3_SRV"] = ""
CONFIG["STEAM_API_KEY"] = ""
CONFIG["STEAM_IDS"] = []
CONFIG["TODAYS_BDAY_CHECK"] = False
# Read configuration values from file.
with open("config.json", "r") as f:
cfg = json.load(f)
try:
# Get local SQLite3 database config entries.
schema_script = cfg["db_schema"]
CONFIG["DB_FILE"] = cfg["db_file"]
# Get telegram config entries.
CONFIG["ADMIN_ID"] = cfg["admin_id"]
CONFIG["BEG_ID"] = cfg["group_id"]
CONFIG["TOKEN"] = cfg["token"]
# Get file system config entries.
CONFIG["FILES_DIR"] = cfg["files_dir"]
# Get TeamSpeak3 config entries.
CONFIG["TS3_USR"] = cfg["ts3_usr"]
CONFIG["TS3_PWD"] = cfg["ts3_pwd"]
CONFIG["TS3_SRV"] = cfg["ts3_srv"]
# Get Steam config entries.
CONFIG["STEAM_API_KEY"] = cfg["steam_api_key"]
CONFIG["STEAM_IDS"] = [str(sid) for sid in cfg["steam_ids"]]
# Connect to local SQLite3 DB (create first, if it does not exist)
new_db = not os.path.exists(CONFIG["DB_FILE"])
con = sqlite3.connect(CONFIG["DB_FILE"])
if new_db:
print("File '{}' not found. Creating new database.".format(CONFIG["DB_FILE"]))
with open(schema_script, "rt") as f:
schema = f.read()
con.executescript(schema)
c = con.cursor()
c.execute("insert into user (username, firstname, telegram_id, added, beg, admin) values "
"('Admin', 'Admin', ?, datetime('now'), 1, 1)", (CONFIG["ADMIN_ID"],))
con.commit()
c.close()
else:
print("Using database '{}'.".format(CONFIG["DB_FILE"]))
c = con.cursor()
c.execute("insert into session (start, end) values (datetime('now'), datetime('now'));")
CONFIG["SESSION_ID"] = c.lastrowid
con.commit()
c.execute("select telegram_id from user")
CONFIG["KNOWN_USERS"] = {u for (u,) in c.fetchall()}
c.close()
con.close()
# Get remote MySQL database config entries.
if REMOTE_ARCHIVE:
CONFIG["MYSQL_SRV"] = cfg["mysql_srv"]
CONFIG["MYSQL_DB"] = cfg["mysql_db"]
CONFIG["MYSQL_USR"] = cfg["mysql_usr"]
CONFIG["MYSQL_PWD"] = cfg["mysql_pwd"]
except KeyError as err:
print("Error: Missing field in config.json: {}".format(err))
exit()
def init_emoji():
global EMOJI
EMOJI["SAD"] = b"\xf0\x9f\x98\x9f"
EMOJI["BLUE_RHOMBUS"] = b"\xf0\x9f\x94\xb9"
EMOJI["BANG"] = b"\xf0\x9f\x92\xa2"
EMOJI["PARTY_CONE"] = b"\xf0\x9f\x8e\x89"
EMOJI["PARTY_BALL"] = b"\xf0\x9f\x8e\x8a"
EMOJI["SPARKLE"] = b"\xe2\x9c\xa8"
EMOJI["BLUE_BALL"] = b"\xf0\x9f\x94\xb5"
EMOJI["RED_BALL"] = b"\xf0\x9f\x94\xb4"
EMOJI["SPEECH_BUBBLE"] = b"\xf0\x9f\x92\xac"
EMOJI["THICK_DASH"] = b"\xe2\x9e\x96"
EMOJI["CROSSING_T"] = b"\xe2\x94\x9c"
EMOJI["CROSSING_L"] = b"\xe2\x94\x94"
EMOJI["WARNING"] = b"\xe2\x9a\xa0\xef\xb8\x8f"
EMOJI["EARTH_AFRICA_EUROPE"] = b"\xf0\x9f\x8c\x8d"
EMOJI["ORANGE_RHOMBUS"] = b"\xf0\x9f\x94\xb6"
EMOJI["SQUIGGLY_LINE"] = b"\xe3\x80\xb0"
EMOJI["CAKE"] = b"\xf0\x9f\x8e\x82"
EMOJI["PRESENT"] = b"\xf0\x9f\x8e\x81"
EMOJI["BALLOON"] = b"\xf0\x9f\x8e\x88"
for e in EMOJI:
EMOJI[e] = EMOJI[e].decode("utf-8")
def init_msgs():
global MSGS
MSGS["ONLY_FOR_ADMINS"] = "{} Sorry, only for bot administrators.".format(EMOJI["BANG"])
MSGS["ONLY_FOR_BEG"] = "{} Sorry, only for Bouncing Egg members.".format(EMOJI["BANG"])
if __name__ == "__main__":
main()