-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1252 lines (1049 loc) · 55 KB
/
main.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# The teampy bot, a simple command bot
# Created 2023-03-16
# by DerSafterXD & WargamerSenpai
#
#
#
#
#
#
# Not working Features:
# - set avatar
# - sending images
# Background: teamspeak uses a diffrent url for uploading the images, then in the documentation
#
# Credits:
# ChatGPT helped creating the code
# Gamer08YT helped with the user agent for the teamspeak Matrix Server
#
# imports
#
import requests
import json
import time
import random
import subprocess
import platform
import os
import sys
import importlib
from distutils.version import LooseVersion
import logging
import datetime
import inspect
import re
import shutil
try:
# import config.py
import config
except:
DUMMY="DUMMY"
# import bad_words.py
from modules.bad_words import *
# import version.py file
from modules.version import *
#####################
# #
# script variables #
# #
#####################
# special User Agent for teamspeak matrix, dont touch or else it is broken
user_agent = "Go-http-client/2.0"
# url for syncing, 20000ms -> 20 seconds
sync_base_url = "/_matrix/client/r0/sync?20000"
# tmp variable for preparing the message for sending
matrix_prepare_message = ""
# data from api
data = {}
# check new invite
check_invite_key = ""
# anwser from matrix sync
sync_response = {}
# http header for sync
sync_headers = {}
# matrix id from bot itself
matrix_self = ""
# access token from matrix, requested on every startup
access_token = ""
# get os windows/linux
os_name = platform.system()
# saves rooms that are privat chat, when invite received
matrix_privat_request = ""
# get current execution directory
script_path = os.path.dirname(os.path.abspath(__file__))
# error message if user doesnt have permission
rank_error_message = "It looks like you dont have the permission for that!"
# info message if a command is disabled
command_disabled_message = "The command is disabled :eyes:"
# interval the ts version gets checked, in seconds
interval = 120
# start time for time check
start_time = time.time()
# variables for ts5 client version
ts_version_saved = ""
ts_version_request = ""
# rooms to notify on new update
matrix_notify_rooms = ()
# get current script path
main_script_path = os.path.dirname(os.path.abspath(__file__))
# the file where all stats are saved to
stats_file = os.path.join(main_script_path,"modules", "stats.txt")
# check if the dictonarys has been check by the config merge
commands_dict_checked = False
admin_dict_checked = False
# response when searching for a user/users
user_room_ids = {}
# description for stats
stats_description = {
"messages_send_count": "How many Messages were sent",
"gifs_count": "How many gifs were sent",
"startup_count": "How many times the bot started",
"help_command_count": "help command count",
"btc_command_count": "BTC command count",
"eth_command_count": "ETH command count",
"whoami_command_count": "whoami command count",
"whois_command_count": "whois command count",
"ping_command_count": "ping command count",
"roll_command_count": "roll command count",
"poll_command_count": "poll command count"
}
# some preset emojis if the poll has no emojis set
poll_emojis = [":+1:",":-1:",":wave:",":ok_hand:",":100:",":pinch:"]
# set log file
log_file = os.path.join(main_script_path, "logs","output_" + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) + ".log"
# set debug mode
debug_mode = False
# file for saving the chats, that get notified
notify_file = os.path.join(main_script_path,"modules","notify.txt")
###############
# #
# functions #
# #
###############
#######################
# return current time #
#######################
def func_time_now():
return str(datetime.datetime.now())
#####################
# write to log file #
#####################
def func_write_to_log(log_message, log_level, log_function):
# fallback if conf_log_level has been wrongly configured
if not config.log_level in ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"]:
config.log_level = "INFO"
# prepare logging file
# log file format: output_YYYY-MM-DD_HH-MM-SS.log
logging.basicConfig(filename=log_file, level=config.log_level, format='%(asctime)s - %(levelname)s - %(message)s')
log_message = log_function + " - " + log_message
# print to cli if started in debug mode
if debug_mode == True:
print(func_time_now() + " - " + log_level + " - " + log_function + " - " + log_message)
if log_level == "CRITICAL":
logging.critical(log_message)
elif log_level == "ERROR":
logging.error(log_message)
elif log_level == "WARNING":
logging.warn(log_message)
elif log_level == "INFO":
logging.info(log_message)
else:
logging.debug(log_message)
########################
# send message to room #
########################
def func_send_message(matrix_send_message):
current_function = inspect.currentframe().f_code.co_name
global matrix_sender
global matrix_prepare_message
global script_path
if matrix_send_message:
message_url = config.matrix_base_url + "/_matrix/client/r0/rooms/" + room_id + "/send/m.room.message"
message_data = {"msgtype": "m.text", "body": matrix_send_message}
message_headers = {"Authorization": "Bearer " + access_token, "User-Agent": user_agent}
response = requests.post(message_url, json=message_data, headers=message_headers)
matrix_send_message=""
if response.status_code == 200:
func_write_to_log("Message sent successfully! (room "+room_id+")", "INFO", current_function)
func_add_stats("messages_send_count")
else:
func_write_to_log("Error sending message to Matrix: %s" % response.text, "ERROR", current_function)
time.sleep(2)
# set message to read, only works in privat chat
try:
payload = {"m.fully_read": event["event_id"], "m.read": event["event_id"]}
response = requests.post(config.matrix_base_url + "/_matrix/client/r0/rooms/" + room_id + "/read_markers", headers=message_headers, json=payload)
if response.status_code == 200:
func_write_to_log("Message successfully set to read!", "INFO", current_function)
else:
func_write_to_log("Error setting message to Read: %s" % response.text, "ERROR", current_function)
time.sleep(2)
except NameError:
payload = {}
# write new stats to file
with open(stats_file, "w") as f:
for key, value in stat_dict.items():
f.write(str(key) + "=" + str(value) + "\n")
time.sleep(1)
matrix_sender = "" #clear sender of message
matrix_prepare_message = ""
####################################
# check if new invite is available #
####################################
def func_check_invite():
current_function = inspect.currentframe().f_code.co_name
global sync_response
global sync_url
global next_batch
matrix_privat_request = ""
check_current_rooms = str(sync_response.get("rooms"))
if check_current_rooms != "None" and config.matrix_join_rooms == True:
check_invite_key = str(sync_response["rooms"].get("invite"))
if check_invite_key != "None":
matrix_new_room = sync_response["rooms"]["invite"]
matrix_new_room = list(matrix_new_room.keys())[0]
#pretty print
#print(json.dumps(sync_response["rooms"]["invite"],sort_keys=True, indent=4))
for room in sync_response["rooms"]["invite"]:
check_for_privat = str(sync_response["rooms"]["invite"][room]["invite_state"]["events"][3]["content"].get("is_direct"))
if check_for_privat != "None":
matrix_privat_request += room +" "
# send post request, to join the room
for room in sync_response["rooms"]["invite"]:
if room in matrix_privat_request:
params = {"membership": "join"}
response = requests.post(config.matrix_base_url + "/_matrix/client/r0/join/"+matrix_new_room, headers=sync_headers, params=params)
if response.status_code == 200:
func_write_to_log("successfully joined direct chat", "INFO", current_function)
else:
func_write_to_log("couldnt join direct chat ("+matrix_new_room+") %s" % response.text, "ERROR", current_function)
else:
response = requests.post(config.matrix_base_url + "/_matrix/client/r0/join/"+matrix_new_room, headers=sync_headers)
if response.status_code == 200:
name_url = config.matrix_base_url + "/_matrix/client/r0/rooms/"+matrix_new_room+"/state"
response = requests.get(name_url, headers=sync_headers)
response_room_name = response.json()[6]
check_room_name = str(response_room_name.get("rooms"))
if check_room_name != "None":
room_name = response_room_name["content"]["name"]
else:
room_name = "privat room"
func_write_to_log("successfully joined room (Name: "+room_name+")", "INFO", current_function)
else:
func_write_to_log("couldnt join room ("+matrix_new_room+") %s" % response.text, "ERROR", current_function)
time.sleep(1)
response = requests.get(sync_url, headers=sync_headers)
next_batch = sync_response["next_batch"]
sync_url = config.matrix_base_url + sync_base_url + "&since=" + next_batch
response = requests.get(sync_url, headers=sync_headers)
next_batch = sync_response["next_batch"]
###################################################
# set avatar, currently not known where to upload #
###################################################
def func_set_avatar():
if config.avatar_url:
current_function = inspect.currentframe().f_code.co_name
avatar_data = {"avatar_url": config.avatar_url}
response = requests.put(config.matrix_base_url + "/_matrix/client/r0/profile/"+matrix_self+"/avatar_url", headers=sync_headers, json=avatar_data)
if response.status_code == 200:
response = requests.get(config.matrix_base_url + "/_matrix/client/r0/profile/"+matrix_self+"/avatar_url", headers=sync_headers)
new_avatar_url = response.json()
func_write_to_log("Bot avatar changed successfully. ("+ new_avatar_url +")", "INFO", current_function)
else:
func_write_to_log("Failed to change bot avatar. %s" % response.text, "ERROR", current_function)
###################################
# set status message and presence #
###################################
def func_set_status():
current_function = inspect.currentframe().f_code.co_name
if config.status_text and config.presence_state:
status_data = {"presence": config.presence_state, "status_msg": config.status_text}
response = requests.put(config.matrix_base_url + "/_matrix/client/r0/presence/"+matrix_self+"/status", headers=sync_headers, json=status_data)
if response.status_code == 200:
func_write_to_log("Status message updated successfully.", "INFO", current_function)
else:
func_write_to_log("Failed to update status message. %s" % response.text, "ERROR", current_function)
###########################
# send gif with giphy api #
###########################
def func_send_gif():
if config.giphy_api_key:
message_index = matrix_received_message.find(" ")
#get search string, if available
giphy_search_string = matrix_received_message[message_index+1:]
if not config.command_prefix + config.command_gif == giphy_search_string:
random_gif = random.randint(0, 30)
url = "https://api.giphy.com/v1/gifs/search?api_key="+config.giphy_api_key+"&q="+giphy_search_string+"&limit=1&offset="+str(random_gif)+"&rating=g&lang=en"
else:
url = "https://api.giphy.com/v1/gifs/random?api_key="+config.giphy_api_key+"&tag=&rating=r"
response = requests.get(url)
data = response.json()
if not config.command_prefix + config.command_gif == giphy_search_string:
if len(data["data"]) > 0:
func_send_message(data['data'][0]['images']['original']['url'])
else:
func_send_message("couldnt find your search string (404)")
else:
func_send_message(data['data']['images']['original']['url'])
else:
func_send_message("i have a problem, the api key is missing in the config, please contact the admin :anxious:")
#################################
# check for update of teamspeak #
#################################
def func_check_client_update():
global ts_version_saved
global ts_version_request
current_function = inspect.currentframe().f_code.co_name
request_version_header = {"Authorization": "Basic dGVhbXNwZWFrNTpMRlo2Wl5rdkdyblh+YW4sJEwjNGd4TDMnYTcvYVtbJl83PmF0fUEzQVJSR1k=", "User-Agent": "teamspeak.downloader/1.0"}
request_version_url= "http://update.teamspeak.com/windows/x64/latest/info.json"
version_response = requests.get(request_version_url, headers=request_version_header, auth=("teamspeak5", "LFZ6Z^kvGrnX~an,$L#4gxL3'a7/a[[&_7>at}A3ARRGY"))
ts_version_request = version_response.json()["version_string"]
func_write_to_log("Version Check for Client: " + str(ts_version_request), "INFO", current_function)
if ts_version_request:
if ts_version_saved:
if ts_version_saved != ts_version_request:
ts_version_saved = ts_version_request
func_notify_update()
else:
ts_version_saved = ts_version_request
########################################################
# send a message to the chats that want to be notified #
########################################################
def func_notify_update():
current_function = inspect.currentframe().f_code.co_name
if matrix_notify_rooms:
for room_id in matrix_notify_rooms:
message_url = config.matrix_base_url + "/_matrix/client/r0/rooms/" + room_id + "/send/m.room.message"
message_data = {"msgtype": "m.text", "body": config.matrix_update_message + ts_version_saved}
message_headers = {"Authorization": "Bearer " + access_token, "User-Agent": user_agent}
response = requests.post(message_url, json=message_data, headers=message_headers)
if not response.status_code == 200:
func_write_to_log("couldnt notify room: " + room_id + ", probably not member of the room anymore. %s" % response.text, "ERROR", current_function)
if response.text["errcode"] == "M_FORBIDDEN":
func_write_to_log("removing room (" + room_id + ") from notifing", "INFO", current_function)
func_notify_room_remove(room_id)
####################################################
# add a room to notify group if a update was found #
####################################################
def func_notify_room_add(room_id):
global matrix_notify_rooms
current_function = inspect.currentframe().f_code.co_name
if not room_id in matrix_notify_rooms:
func_write_to_log("added " + room_id + " to notify group", "INFO", current_function)
matrix_notify_rooms = matrix_notify_rooms + (room_id,)
file_content = " ".join(matrix_notify_rooms)
with open(notify_file, "w") as file:
# Write the string representation of the tuple to the file
file.write(file_content)
func_send_message("added to notify group :100:")
else:
func_write_to_log(room_id + " already in notify group", "INFO", current_function)
func_send_message("alredy in notify groupd :eyes:")
#####################################################
# remove a room from notifing if a update was found #
#####################################################
def func_notify_room_remove(room_id):
global matrix_notify_rooms
current_function = inspect.currentframe().f_code.co_name
matrix_notify_rooms = tuple(value for value in matrix_notify_rooms if value != room_id)
file_content = " ".join(matrix_notify_rooms)
with open(notify_file, "w") as file:
# Write the string representation of the tuple to the file
file.write(file_content)
func_write_to_log("removed " + room_id + "from notify group", "INFO", current_function)
func_send_message("removed from notify group :salute:")
################################
# get notify rooms out of file #
################################
def func_notify_rooms_get():
global matrix_notify_rooms
# open file in read only mode, if exist
if os.path.exists(notify_file):
with open(notify_file, "r") as file:
# read first line
first_line = file.readline().strip()
# split values with spaces
values = first_line.split()
# convert to tuple
matrix_notify_rooms = tuple(values)
####################
# toggle auto join #
####################
def func_toggle_autojoin():
current_function = inspect.currentframe().f_code.co_name
# invert Boolean
matrix_join_rooms_new = not config.matrix_join_rooms
func_write_to_log("toggling auto Join to " + str(matrix_join_rooms_new), "INFO", current_function)
# open file and
with open("config.py", "r") as f:
content = f.readlines()
# search line with variable
for i, line in enumerate(content):
if line.startswith("matrix_join_rooms"):
content[i] = "matrix_join_rooms = "+str(matrix_join_rooms_new)+"\n"
break
# overwrite found line
with open("config.py", "w") as f:
f.writelines(content)
importlib.reload(config)
#######################
# health check of bot #
#######################
def func_health_check():
current_function = inspect.currentframe().f_code.co_name
bot_health_check_config = ""
func_write_to_log("Checking health", "INFO", current_function)
if func_container_check():
bot_health_container = "True"
func_write_to_log("!! running inside a container !!", "INFO", "startup")
else:
bot_health_container = "False"
func_write_to_log("not running inside a container", "INFO", "startup")
if os.path.isfile(os.path.join(main_script_path,"config.py")):
bot_health_check_config = "Config file found"
func_write_to_log("Config File found", "INFO", current_function)
else:
func_write_to_log("Config file not found", "ERROR", current_function)
bot_health_check_config = "**Config file not found**"
if os.path.isfile(os.path.join(main_script_path,"modules","bad_words.py")):
bot_health_check_badwords = "File was found"
else:
bot_health_check_badwords = "**File was not found**"
if config.matrix_base_url == "https://chat.teamspeak.com":
bot_health_check_baseurl = "URL is set correct"
func_write_to_log("URL set correctly", "INFO", current_function)
else:
bot_health_check_baseurl = "**URL is not correct**"
func_write_to_log("URL empty or pointing to wrong matrix homebase", "ERROR", current_function)
if config.matrix_username:
bot_health_check_username = "Username is set"
func_write_to_log("Username is set", "INFO", current_function)
else:
bot_health_check_username = "**Username is empty**"
func_write_to_log("Username wasnt found, error in config", "ERROR", current_function)
if config.matrix_password:
bot_health_check_password = "Password is set"
func_write_to_log("Password is set", "INFO", current_function)
else:
bot_health_check_password = "**Password is empty**"
func_write_to_log("Password wasnt found, error in config", "ERROR", current_function)
func_send_message("Summary of Health Check: \nConfig: " + bot_health_check_config \
+ "\nMatrix Server URL: " + bot_health_check_baseurl + "\nUsername: " + bot_health_check_username \
+ "\nPassword: " + bot_health_check_password + "\nBad Words File: " + bot_health_check_badwords + "\nContainer: "+bot_health_container)
#################################################
# add stat to stats.txt, or create missing stat #
#################################################
def func_add_stats(key):
current_function = inspect.currentframe().f_code.co_name
func_write_to_log("key parsed:" + key , "INFO", current_function)
if key in stat_dict:
func_write_to_log("key inside array" , "INFO", current_function)
stat_dict[key] += 1
else:
func_write_to_log("key outside array" , "INFO", current_function)
stat_dict[key] = 1
################
# stop the bot #
################
def func_bot_stop():
current_function = inspect.currentframe().f_code.co_name
func_write_to_log("stopping bot...", "INFO", current_function)
exit(0)
###################
# restart the bot #
###################
def func_bot_restart():
current_function = inspect.currentframe().f_code.co_name
func_write_to_log("restarting bot... on " + os_name , "INFO", current_function)
# splitted linux and windows if function shouldnt work
if os_name == "Linux":
python_executable = sys.executable
script_file = os.path.join(main_script_path, "./modules/restart.py")
os.system(python_executable + " " + script_file + " " + os_name )
if os_name == "Windows":
python_executable = sys.executable
script_file = main_script_path +".\\modules\\restart.py"
subprocess.call([python_executable, script_file, os_name])
else:
func_write_to_log("Couldnt detect OS for proper restart", "CRITICAL", current_function)
exit(255)
#########################################################################
# used when inside container, touch file to refresh change date of file #
#########################################################################
def func_touch_file(filename):
with open(filename, 'a'):
os.utime(filename, None)
##############################################
# check if bot is running inside a container #
##############################################
def func_container_check():
if 'CONTAINER_BOOL' in os.environ:
return True
else:
return False
#################################################
# get priv roomid with identifier #
# Explanation: #
# 50/50 % Chance it will find the private room, #
# this function searches for a room with two #
# members, himself and the target user. But if #
# there is a group chat that also has these #
# conditions, it could return the group chat #
#################################################
def func_find_roomid(user_identifiers):
global user_room_ids
user_room_ids = {}
response = requests.get(config.matrix_base_url+"/_matrix/client/r0/joined_rooms",
headers={"Authorization": f"Bearer {access_token}","User-Agent": user_agent})
room_list = response.json().get("joined_rooms", [])
for user_identifier in user_identifiers:
for room_id in room_list:
response = requests.get(config.matrix_base_url+"/_matrix/client/r0/rooms/"+room_id+"/members",
headers={"Authorization": f"Bearer {access_token}","User-Agent": user_agent})
if response.status_code == 200:
member_list = response.json().get("chunk", [])
# Check if the room contains any of the specified users
for member in member_list:
if len(member_list) == 2 and member.get("state_key") in user_identifier:
# Get room information
response = requests.get(config.matrix_base_url+"/_matrix/client/r0/rooms/"+room_id+"/state",
headers={"Authorization": f"Bearer {access_token}","User-Agent": user_agent})
user_room_ids[user_identifier] = room_id
break
else:
func_write_to_log("Error retrieving member list for room "+room_id+": " +response.status_code, "ERROR", "func_find_roomid")
return user_room_ids
######################################
# merge old config file with new one #
######################################
def func_merge_configs():
global admin_dict_checked, commands_dict_checked, room_id
if merge_bool:
new_config = False
config_url = 'https://raw.githubusercontent.com/Wargamer-Senpai/teampy/main/config.py'
example_config_file = os.path.join(main_script_path,'example_config.py')
# get a fresh config from github
response = requests.get(config_url)
if response.status_code == 200:
with open(example_config_file, 'wb') as file:
file.write(response.content)
with open(example_config_file, 'r') as file:
content = file.read()
import example_config
if func_container_check():
import configs.config as config
else:
import config
example_config_overview = dir(example_config)
config_overview = dir(config)
for variable_name in example_config_overview:
if not variable_name.startswith("__"):
if variable_name in config_overview:
example_variable_value = getattr(example_config, variable_name)
variable_value = getattr(config, variable_name)
variable_type = type(variable_value)
if variable_type == str or variable_type == bool or variable_type == list or variable_type == list:
if example_variable_value != variable_value:
if variable_type == str:
content = re.sub(f"{variable_name} = .*", f"{variable_name} = \"{variable_value}\"", content)
elif variable_type == bool or variable_type == list:
content = re.sub(f"{variable_name} = .*", f"{variable_name} = {variable_value}", content)
elif variable_type == dict:
notify_admin_merge = True
else:
func_write_to_log("New Variable Found: " + variable_name, "DEBUG", "func_merge_configs")
new_config = True
if new_config == True:
with open(example_config_file, 'w', encoding="utf-8") as file:
file.write(content)
if func_container_check():
shutil.move(os.path.join("configs","config.py"), os.path.join("configs","config_backup_"+datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')+".py"))
else:
os.rename(os.path.join(main_script_path,"config.py"), "config_backup_"+datetime.datetime.now().strftime('%Y-%m-%d_%H-%M')+".py")
os.rename(os.path.join(main_script_path,example_config_file), os.path.join(main_script_path,"config.py"))
if func_container_check():
shutil.move("config.py", os.path.join("configs","config.py"))
import configs.config as config
else:
import config
if notify_admin_merge == True:
room_ids = func_find_roomid(config.bot_admin)
for user in room_ids:
room_id = room_ids[user]
func_send_message("The bot config has been merged with a newer version, please check the config on errors.")
############################
# #
# starting of the script #
# #
############################
if func_container_check():
# if running inside a container
source_config = "/opt/teampy/config.py"
target_config = "/opt/teampy/configs/config.py"
if not os.path.exists(target_config):
with open(source_config, 'rb') as src_file:
with open(target_config, 'wb') as dest_file:
dest_file.write(src_file.read())
# func_write_to_log("config.py copied successfully!", "INFO", "startup_container_check")
# func_write_to_log("config.py already exists!", "INFO", "startup_container_check")
# import config file from persistent folder
import configs.config as config
func_write_to_log("!! running inside a container !!", "INFO", "startup_container_check")
# change folders for files that needs to be persistent
notify_file = os.path.join(main_script_path,"configs","notify.txt")
stats_file = os.path.join(main_script_path,"configs", "stats.txt")
else:
func_write_to_log("not running inside a container", "INFO", "startup_container_check")
# contains the boolean if configs should be merged
try:
merge_bool = config.merge_config
except AttributeError:
merge_bool = True
if not config.matrix_username or not config.matrix_password:
func_write_to_log("finish configuration step first! (username and/or password empty)", "ERROR", "startup")
exit()
func_write_to_log("starting bot....", "INFO", "startup")
if os_name == "Windows":
func_write_to_log("Detected Windows", "DEBUG", "startup")
elif os_name == "Linux":
func_write_to_log("Detected Linux", "DEBUG", "startup")
else:
func_write_to_log("Coulndt detect OS ("+os_name+"), script wont work well!", "CRITICAL", "startup")
# check if stats file exists
if not os.path.exists(stats_file):
# if not create it with preset
with open(stats_file, "w") as f:
f.write("messages_send_count=0\n"
"gifs_count=0\n"
"startup_count=0\n"
"help_command_count=0\n"
"btc_command_count=0\n"
"eth_command_count=0\n"
"whoami_command_count=0\n"
"whois_command_count=0\n"
"ping_command_count=0\n"
"roll_command_count=0\n"
"poll_command_count=0\n"
)
with open(stats_file, "r") as f:
stat_dict = {}
for line in f:
key, value = line.strip().split("=")
stat_dict[key] = int(value)
# login to Matrix and get access token and own user id
login_url = config.matrix_base_url + "/_matrix/client/r0/login"
login_data = {"type": "m.login.password", "user": config.matrix_username, "password": config.matrix_password}
login_headers = {"User-Agent": user_agent}
response = requests.post(login_url, json=login_data, headers=login_headers)
if response.json().get("access_token"):
access_token = response.json()["access_token"]
matrix_self = response.json()["user_id"]
func_write_to_log("Login Successfull, own Matrix ID: " + matrix_self, "INFO", "startup_login")
else:
func_write_to_log("Error Login failed, Username or Password wrong %s" % response.text, "ERROR", "startup_login")
exit(1)
func_merge_configs() # merge new configs
# prepare first sync
sync_url = config.matrix_base_url + sync_base_url
sync_headers = {"Authorization": "Bearer " + access_token, "User-Agent": user_agent}
next_batch = None
#make initial request (so he ignores old chats)
response = requests.get(sync_url, headers=sync_headers)
sync_response = json.loads(response.text)
# not working
#func_set_avatar()
func_check_invite()
func_set_status()
func_add_stats("startup_count")
func_check_client_update()
func_notify_rooms_get()
func_write_to_log("Startup complete...", "INFO", "startup_finished")
while True:
time.sleep(1)
if func_container_check():
func_touch_file("check_container")
# check for new update of ts client
elapsed_time = time.time() - start_time
if elapsed_time >= interval:
start_time = time.time()
func_check_client_update()
next_batch = sync_response["next_batch"]
sync_url = config.matrix_base_url + sync_base_url + "&since=" + next_batch
# make a request to the sync API to check for new events
try:
response = requests.get(sync_url, headers=sync_headers)
except:
time.sleep(5)
response = requests.get(sync_url, headers=sync_headers)
# check if the response was successful
if response.status_code == 200:
# parse the response JSON to extract any new events
sync_response = json.loads(response.text)
check_current_rooms = str(sync_response.get("rooms"))
if check_current_rooms != "None":
check_active_rooms = str(sync_response["rooms"].get("join"))
if check_active_rooms != "None":
# check if in any room is something new, if not just wait and retry
if "rooms" in sync_response and sync_response["rooms"] is not None:
# check if there are any new messages in the room
for room_id in sync_response["rooms"]["join"]:
# Loop through all new events in the room
for event in sync_response["rooms"]["join"][room_id]["timeline"]["events"]:
# check if the event is a message
if event["type"] == "m.room.message" and event["sender"] != matrix_self:
# Print the message body, need to changed to loggin into a file
matrix_sender = event["sender"]
matrix_received_message = event["content"]["body"]
matrix_room = room_id
func_write_to_log(matrix_sender + ": " + matrix_received_message +" (room: "+ matrix_room +")", "INFO", "main_loop")
func_write_to_log(json.dumps(event,sort_keys=True, indent=4), "DEBUG", "main_loop")
#print(json.dumps(event,sort_keys=True, indent=4))
#############################################
# check what if content matches any command #
#############################################
# gif command
if config.command_prefix + config.command_gif in matrix_received_message:
if config.commands_overview[config.command_gif]["command_enabled"] == True:
func_add_stats("gifs_count")
func_send_gif()
else:
func_send_message(command_disabled_message)
elif config.command_prefix + config.command_btc in matrix_received_message:
if config.commands_overview[config.command_btc]["command_enabled"] == True:
btc_base_url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies="
btc_gbp_respond = requests.get(btc_base_url + "gbp")
btc_gbp = btc_gbp_respond.json()["bitcoin"]["gbp"]
btc_eur_respond = requests.get(btc_base_url + "eur")
btc_eur = btc_eur_respond.json()["bitcoin"]["eur"]
btc_usd_respond = requests.get(btc_base_url + "usd")
btc_usd = btc_usd_respond.json()["bitcoin"]["usd"]
func_send_message("__**Prices are not so precise**__\nCurrent Price of BTC: \
\nEuro: "+str(btc_eur)+" \nGBP: "+str(btc_gbp)+" \nUSD: "+str(btc_usd)+" \
\nfrom https://www.coingecko.com/")
func_add_stats("btc_command_count")
else:
func_send_message(command_disabled_message)
elif config.command_prefix + config.command_eth in matrix_received_message:
if config.commands_overview[config.command_eth]["command_enabled"] == True:
eth_base_url = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies="
eth_gbp_respond = requests.get(eth_base_url + "gbp")
eth_gbp = eth_gbp_respond.json()["ethereum"]["gbp"]
eth_eur_respond = requests.get(eth_base_url + "eur")
eth_eur = eth_eur_respond.json()["ethereum"]["eur"]
eth_usd_respond = requests.get(eth_base_url + "usd")
eth_usd = eth_usd_respond.json()["ethereum"]["usd"]
func_send_message("__**Prices are not so precise**__\nCurrent Price of ETH: \
\nEuro: "+str(eth_eur)+" \nGBP: "+str(eth_gbp)+" \nUSD: "+str(eth_usd)+" \
\nfrom https://www.coingecko.com/")
func_add_stats("eth_command_count")
else:
func_send_message(command_disabled_message)
# help
elif config.command_prefix + config.command_help in matrix_received_message:
if config.commands_overview[config.command_help]["command_enabled"] == True:
matrix_prepare_message = "Here is help, dont worry!\n"
for key in config.commands_overview:
if config.commands_overview[key]["command_enabled"] == True:
matrix_prepare_message += "**" + config.command_prefix + key + ":** " + config.commands_overview[key]["description"] + "\n"
func_send_message(matrix_prepare_message)
func_add_stats("help_command_count")
else:
func_send_message(command_disabled_message)
# public stats
elif config.command_prefix + config.command_stats in matrix_received_message:
if config.commands_overview[config.command_stats]["command_enabled"] == True:
if config.stats_visible == "public" or config.stats_visible == "admin" and matrix_sender in config.bot_admin:
params = {"filter": "{\"presence\":{\"types\":[\"m.presence\"]},\"account_data\":{\"types\":[\"m.direct\"]},\"room\":{\"rooms\":[],\"account_data\":{\"types\":[\"m.tag\",\"m.space\"]},\"state\":{\"types\":[\"m.room.member\"],\"lazy_load_members\":true},\"timeline\":{\"types\":[\"m.room.message\",\"m.room.encrypted\",\"m.sticker\",\"m.reaction\"]}}}", "timeout": 0}
friend_response = requests.get(config.matrix_base_url + "/_matrix/client/r0/sync", headers=sync_headers, params=params)
if friend_response.status_code == 200:
matrix_friends = friend_response.json()
matrix_friends = matrix_friends.get("account_data", {}).get("events", [])
matrix_friend_count = len(matrix_friends)
else:
func_write_to_log("Failed to retrieve friend list. %s" % response.text, "ERROR", "main_loop")
matrix_prepare_message = "Number of friends (all time): **" + str(matrix_friend_count) + "**"
for key in stat_dict:
matrix_prepare_message += "\n"+stats_description[key]+": **" + str(stat_dict[key]) + "**"
func_send_message(matrix_prepare_message)
else:
func_send_message("the stats are currently admin only :eyes:")
else:
func_send_message(command_disabled_message)
# whoami
elif config.command_prefix + config.command_whoami in matrix_received_message:
if config.commands_overview[config.command_whoami]["command_enabled"] == True:
request_sender_name = requests.get(config.matrix_base_url + "/_matrix/client/r0/profile/"+ matrix_sender, headers=sync_headers)
matrix_sender_name = request_sender_name.json()["displayname"]
user_rank = "User"
if matrix_sender in config.bot_admin:
user_rank = "Admin"
func_send_message("Your identifier is: "+ matrix_sender + "\nYour name is: " + matrix_sender_name \
+ "\nI see you as a " + user_rank)
matrix_sender_name = ""
func_add_stats("whoami_command_count")
else:
func_send_message(command_disabled_message)
# whois
elif config.command_prefix + config.command_whois in matrix_received_message:
if config.commands_overview[config.command_whois]["command_enabled"] == True:
user_rank = "User"
message_index = matrix_received_message.find(" ")
# get search string, if available
matrix_identifier = matrix_received_message[message_index+1:]
if matrix_identifier:
request_sender_name = requests.get(config.matrix_base_url + "/_matrix/client/r0/profile/" + matrix_identifier, headers=sync_headers)
repsonse_name = request_sender_name.json()
check_identifier_name = str(repsonse_name.get("displayname"))
if check_identifier_name != "None":
matrix_sender_name = repsonse_name["displayname"]
else:
matrix_sender_name = "**Couldnt find Name**"
user_rank = "Unknown"
if matrix_identifier in config.bot_admin:
user_rank = "Admin"
func_send_message("The identifier is: "+ matrix_identifier + "\nThe name is: " + matrix_sender_name \
+ "\nI see that person as a " + user_rank)
matrix_sender_name = ""
func_add_stats("whois_command_count")
else:
matrix_sender_name = rank_error_message
else:
func_send_message(command_disabled_message)
# polls
elif config.command_prefix + config.command_poll in matrix_received_message :
if config.commands_overview[config.command_poll]["command_enabled"] == True:
func_add_stats("poll_command_count")
poll_choices = ""
poll_count = 0
poll_question = matrix_received_message.split("\n")[0]
poll_question = poll_question.split()
poll_question = ' '.join(poll_question[1:])
if not poll_question:
poll_question = "Do you like polls ?"
poll_choices_raw = matrix_received_message.split("\n")
for choice in poll_choices_raw:
if choice != poll_choices_raw[0]:
if not choice.startswith(":") and choice:
choice = str(poll_emojis[poll_count]) + " " + str(choice)
if poll_count >= 5:
poll_count = 0
poll_count += 1
poll_choices += str(choice) + "\n"
if not poll_choices:
poll_choices = ":+1: yes \n :-1: no"