-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_bot.py
executable file
·989 lines (781 loc) · 28.8 KB
/
telegram_bot.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
import os, sys, shutil
import json
from telegram.ext import CommandHandler, filters, MessageHandler, Application, ContextTypes
from telegram.constants import ParseMode
from telegram import Update
from functools import wraps
import time
import codecs
import re
import string
messages_path = "./"
"""
File "secret.txt" contains a single line with secret code (access token) you received from the telegram bot father
File "config.json" has "users" root entry with this format for each user:
"<First> <Last>" {
"id": <telegram user id>,
"settings": [,admin=1][,uploads=<subdir for personal uploads"]
}
"John Doe": {
"id": 123303756,
"settings": "admin=1, uploads=JD_Uploads/"
},
only admins can delete messages and upload new ones. all plain users just search/view messages
Note:
with user's telegram id set to 0 the bot will accept any user with that user name. This is unsecure,
and you should use this option only for the very first time. The bot will let the user in and print the telegram id.
You should use that id and put it into the config entry for the user.
"""
messages = []
new_messages = []
admins = {}
users = {}
pic_dir = {}
pic_files = []
all_tags = {}
max_messages_in_search = 10
user_settings = {}
user_session_settings = {}
def read_config(file_name):
try:
c = json.load(open(file_name))
except Exception as e:
print("Error reading config", e)
return None
return c
def read_users():
global users, admins
users = {}
admins = {}
for uname, user in cfg['users'].items():
idd = user.get("id")
if idd is None:
idd = 0
users[uname] = idd
items = user.get("settings", "").split(",")
settings = {}
for i in items:
j = i.strip().split("=")
if len(j) == 2:
if j[0] == "admin":
if j[1] == "1":
admins[uname] = idd
print("added admin '%s' with id %s" % (uname, idd))
else:
settings[j[0].strip()] = j[1].strip()
user_settings[uname] = settings
print("added user '%s' with id %s " % (uname, idd), settings.items())
user_session_settings[uname] = {}
print("%d users known for the bot" % len(users))
def is_user_ok(first, last, uid):
i = users.get("%s %s" % (first, last))
if i is None:
return False
if i == 0:
users["%s %s" % (first, last)] = uid
print("Registered id %s for user %s %s" % (uid, first, last))
return True
return i == uid
def is_admin_ok(first, last, uid):
i = admins.get("%s %s" % (first, last))
if i is None:
return False
if i == 0:
admins["%s %s" % (first, last)] = uid
print("Registered id %s for admin %s %s" % (uid, first, last))
return True
return i == uid
def xis_user_ok(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_user_ok(update.message.chat.first_name, update.message.chat.last_name,
update.message.chat.id):
context.bot.send_message(
chat_id=update.message.chat_id,
text="<i>(.. You are not authorized to use this service.)</i>",
parse_mode=ParseMode.HTML,
)
print(update.message.chat.first_name, update.message.chat.last_name, "not authorized for the service")
return False
return True
def xis_admin_ok(update: Update, context: ContextTypes.DEFAULT_TYPE):
if not is_admin_ok(update.message.chat.first_name, update.message.chat.last_name,
update.message.chat.id):
context.bot.send_message(
chat_id=update.message.chat_id,
text="<i>(.. You are not authorized for this operation.)</i>",
parse_mode=ParseMode.HTML,
)
print(update.message.chat.first_name, update.message.chat.last_name, "not authorized for the op")
return False
return True
def build_pic_dir():
global pic_dir, pic_files
pic_files = []
pic_dir = {}
for m in messages:
if m.startswith("/p#i#c"):
m = m[7:]
file_name = m.split(" ")[0]
m = m[len(file_name) + 1:]
pic_dir[file_name] = m
pic_files = [s for s in os.listdir(pic_path) if s.lower().endswith(".jpg")]
def update_tags(m):
# wordList = re.sub("[^#a-zA-Z0-9_]", " ", curr_message).split()
sp = string.punctuation.replace("#", "")
word_list = re.sub("[" + sp + "]", "", m).split()
for w in word_list:
if w.startswith("#") and len(w) > 1:
l_tags = all_tags.setdefault(w, set())
l_tags.add(len(messages) - 1)
def rebuild_tags():
global messages, all_tags
ms = messages
messages = []
all_tags = {}
for m in ms:
messages.append(m)
update_tags(m)
def read_messages():
global messages
msg_name = messages_path + "notes.txt"
if not os.path.exists(msg_name):
return []
# f = file(msg_name)
f = codecs.open(msg_name, "r", "cp1251")
all_s = f.read()
messages = []
curr_message = ""
for s in all_s.split("\n"):
s += "\n"
if s.startswith("---"):
if len(curr_message) == 0:
continue
messages.append(curr_message)
update_tags(curr_message)
curr_message = ""
continue
curr_message += s
if len(curr_message) != 0:
messages.append(curr_message)
update_tags(curr_message)
f.close()
return messages
def update_messages():
global new_messages
for m in new_messages:
messages.append(m)
update_tags(m)
new_messages = []
def save_messages():
msg_name = messages_path + "notes.txt"
# f = file(msg_name,'w')
f = codecs.open(msg_name, "w", "cp1251")
for s in messages:
f.write("---\n")
f.write(s)
if not s.endswith("\n"):
f.write("\n")
f.close()
def send_action(action):
"""Sends `action` while processing func command."""
def decorator(func):
@wraps(func)
def command_func(*args, **kwargs):
bot = args[1].bot
bot.send_chat_action(chat_id=args[0].message.chat_id, action=action)
func(args[0], args[1], args[1].args)
return command_func
return decorator
async def hello(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await bot.message.reply_text(
"Hello {}".format(bot.message.from_user.first_name)
+ ", I have %d notes" % (len(messages) + len(new_messages))
)
await del_u_settings(bot, context)
async def send_message(bot, context: ContextTypes.DEFAULT_TYPE, mess_no, full=False):
if len(messages) < mess_no:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..could not find message %03d, max number is %d)</i> " % (mess_no,len(messages)),
parse_mode=ParseMode.HTML,
)
return
m1 = messages[mess_no - 1]
if m1.startswith("/d#o#c"):
m1 = m1[7:]
if m1.startswith('"'):
file_name = m1[1:].split('"')[0].replace("\n", "")
else:
file_name = m1.split(" ")[0].replace("\n", "")
if full:
try:
fo = open(doc_path + file_name, "rb")
await context.bot.send_document(chat_id=bot.message.chat_id, document=fo)
fo.close()
except:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="[%03d] <i>(..document is missing %s)</i> " % (mess_no, file_name),
parse_mode=ParseMode.HTML,
)
else:
await context.bot.send_message(chat_id=bot.message.chat_id,
text="[%03d] (%s) %s" % (mess_no, "document", file_name))
elif m1.startswith("/p#i#c"):
m1 = m1[7:]
file_name = m1.split(" ")[0].replace("\n", "")
m1 = m1[len(file_name) + 1:]
if full:
try:
fo = open(pic_path + file_name, "rb")
await context.bot.send_photo(chat_id=bot.message.chat_id, photo=fo, caption="[%03d] " % mess_no + m1)
fo.close()
except:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="[%03d] <i>(..picture is missing %s)</i> " % (mess_no, m1),
parse_mode=ParseMode.HTML,
)
else:
await context.bot.send_message(chat_id=bot.message.chat_id, text="[%03d] (%s)" % (mess_no, "picture") + m1)
else:
await context.bot.send_message(chat_id=bot.message.chat_id, text="[%03d] " % mess_no + m1)
def scan1dir4docs(p):
res = []
d = os.listdir(p)
for od in d:
if os.path.isdir(p + od):
res += scan1dir4docs(p + od + "/")
else:
res.append(p + od)
return res
def scan4docs(path):
n = len(path)
res = [d[n:] for d in scan1dir4docs(path)]
# for d in res :
# print d
return res
# @send_action(ChatAction.TYPING)
async def all_docs(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
res = scan4docs(doc_path)
s = set()
for m in messages:
if m.startswith("/d#o#c"):
m = m[7:]
if m.startswith('"'):
file_name = m[1:].split('"')[0]
else:
file_name = m.split(" ")[0]
s.add(file_name.replace("\n", ""))
for r in res:
if r not in s:
if r.find(" ") >= 0:
m = '/d#o#c "%s"' % r
else:
m = "/d#o#c %s" % r
new_messages.append(m)
update_messages() ## debug
save_messages() ## debug
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) == 1:
args = ["/f","d#o#c"]
await actual_find_substring(bot, context, args)
else:
args = ["/f","+d#o#c"] + bot.message.text.split()
await actual_find_substring(bot, context, args)
# @send_action(ChatAction.TYPING)
async def find_substring(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) <2:
await bot.message.reply_text(
"Please use syntax: /f [+<must_be_sub_string1> ..] [!<must_not_be_sub_string1> ..] [<substring1> ..] "
)
return
await actual_find_substring(bot,context, args)
async def actual_find_substring(bot: Update, context: ContextTypes.DEFAULT_TYPE, args) -> None:
included = []
excluded = []
must_be = []
anything = False
done = 0
for s in args[1:]:
s = s.replace("'", "")
s = s.replace('"', "")
if s.startswith("!"):
excluded.append(s[1:].lower())
elif s.startswith("+"):
must_be.append(s[1:].lower())
else:
included.append(s.lower())
mess_no = 0
for m1 in messages:
mess_no += 1
m = m1.lower()
need_break = False
for e in excluded:
if m.find(e) >= 0:
need_break = True
break
if need_break:
continue
for o in must_be:
if m.find(o) < 0:
need_break = True
break
if need_break:
continue
maybe = False ##if len(must_be)==0 else True
if not maybe:
for o in included:
if m.find(o) >= 0:
maybe = True
break
if maybe:
done += 1
if done >= max_messages_in_search:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. there are more notes in search, please rectify.)</i>",
parse_mode=ParseMode.HTML,
)
break
anything = True
await send_message(bot, context, mess_no)
if not anything:
await context.bot.send_message(
chat_id=bot.message.chat_id, text="<i>(.. there are no matches.)</i>", parse_mode=ParseMode.HTML
)
async def refresh(_: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
print("Refreshing..")
global messages, new_messages
read_messages()
new_messages = []
read_users()
build_pic_dir()
## exit()
# @send_action(ChatAction.TYPING)
async def del_message(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_admin_ok(bot, context):
return
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) < 2:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. Please specify at least one message number.)</i>",
parse_mode=ParseMode.HTML,
)
return
to_del = set()
for a1 in args[1:]:
i = int(a1) - 1
if i >= len(messages):
continue
print("Deleting message no %d" % i)
m = messages[i]
if m.startswith("/d#o#c"):
m = m[7:]
if m.startswith('"'):
file_name = m[1:].split('"')[0].replace("\n", "")
else:
file_name = m.split(" ")[0].replace("\n", "")
try:
os.unlink(doc_path + file_name)
print("Deleted file ", doc_path + file_name)
except:
print("Could not delete ", doc_path + file_name)
to_del.add(i)
to_del = list(to_del)
to_del.sort()
to_del.reverse()
repl = ""
for td in to_del:
repl += "<i>(..message #%d deleted)</i>\n" % (td + 1)
del messages[td]
if repl != "":
await context.bot.send_message(chat_id=bot.message.chat_id, text=repl, parse_mode=ParseMode.HTML)
save_messages() ## debug
rebuild_tags()
build_pic_dir()
async def unknown_cmd(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
txt = bot.message.text
if txt is not None and len(txt) > 1:
txt = txt[1:]
if txt[0] == "#":
await find_tag(bot, context)
return
if txt.isdigit():
num = int(txt)
await send_message(bot, context, num, True)
return
else:
args = ["/f"] + txt.split()
await actual_find_substring(bot, context, args)
return
await context.bot.send_message(chat_id=bot.message.chat_id, text="Sorry, I didn't understand that command: " + txt)
# @send_action(ChatAction.TYPING)
async def get_pic(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
ndx = 0
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) > 1:
ndx = int(args[1]) - 1
else:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..please define picture number)</i> ",
parse_mode=ParseMode.HTML,
)
return
if ndx <0 or ndx >= len(pic_files):
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. Photo #%d not found.)</i>" % (ndx + 1),
parse_mode=ParseMode.HTML,
)
return
file_name = pic_files[ndx]
capt = pic_dir.get(file_name, file_name)
capt = "[pic %03d] " % (ndx + 1) + capt
try:
fo = open(pic_path + file_name, "rb")
await context.bot.send_photo(chat_id=bot.message.chat_id, photo=fo, caption=capt)
except:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..picture #%d is missing)</i> " % (ndx + 1),
parse_mode=ParseMode.HTML,
)
async def del_pic(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_admin_ok(bot, context):
return
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) < 2:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. Please specify at least one picture number.)</i>",
parse_mode=ParseMode.HTML,
)
return
ndx = int(args[1]) - 1
if ndx >= len(pic_files):
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. Photo #%d not found.)</i>" % (ndx + 1),
parse_mode=ParseMode.HTML,
)
return
if pic_files[ndx] == "(..deleted)":
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..picture #%d is already deleted)</i> " % (ndx + 1),
parse_mode=ParseMode.HTML,
)
return
file_name = pic_files[ndx]
try:
os.unlink(pic_path + file_name)
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..picture #%d deleted)</i> " % (ndx + 1),
parse_mode=ParseMode.HTML,
)
pic_files[ndx] = "(..deleted)"
except:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(..picture #%d is missing)</i> " % (ndx + 1),
parse_mode=ParseMode.HTML,
)
# @send_action(ChatAction.TYPING)
async def find_tag(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) == 1 and len(args[0]) >1 and (args[0][0] == '#' or args[0].startswith('/#')) :
tg = args[0][1:]
if tg[0] == '#':
tg = tg[1:]
args =["/tag", tg]
if len(args) < 2:
await context.bot.send_message(
chat_id=bot.message.chat_id, text="<i>(.. Please specify tag.)</i>", parse_mode=ParseMode.HTML
)
return
t = args[1]
if not t.startswith("#"):
t = "#" + t
l_tags = all_tags[t]
if l_tags is None:
await context.bot.send_message(chat_id=bot.message.chat_id, text="<i>(..tag is not found)</i> ",
parse_mode=ParseMode.HTML)
return
ll = list(l_tags)
ll.sort()
for ndx in ll[:10]:
await send_message(bot, context, ndx + 1, len(ll) == 1)
# @send_action(ChatAction.TYPING)
async def tags_dir(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
rep = "<i>(.. tags found: %d)</i>\n" % len(all_tags)
for k, l in all_tags.items():
rep += "%s (%d)," % (k, len(l))
if len(rep) > 0:
rep = rep[: len(rep) - 1]
await context.bot.send_message(chat_id=bot.message.chat_id, text=rep, parse_mode=ParseMode.HTML)
# @send_action(ChatAction.TYPING)
async def del_u_settings(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
nm = "%s %s" % (bot.message.from_user.first_name, bot.message.from_user.last_name)
user_session_settings[nm] = {}
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. session level user settings deleted.)</i>",
parse_mode=ParseMode.HTML,
)
# @send_action(ChatAction.TYPING)
async def show_settings(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
nm = "%s %s" % (bot.message.from_user.first_name, bot.message.from_user.last_name)
ss = user_session_settings.get(nm)
m = "<i>Session active settings:\n</i>"
if ss is None or len(ss) == 0:
m += "<i>(None)</i>\n"
else:
for k in ss.items():
m += "%s=%s\n" % k
ss = user_settings.get(nm)
m += "<i>Permanent settings:\n</i>"
if ss is None or len(ss) == 0:
m += "<i>(None)</i>\n"
else:
for k in ss.items():
m += "%s=%s\n" % k
await context.bot.send_message(chat_id=bot.message.chat_id, text=m, parse_mode=ParseMode.HTML)
# @send_action(ChatAction.TYPING)
async def u_settings(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
nm = "%s %s" % (bot.message.from_user.first_name, bot.message.from_user.last_name)
settings = user_session_settings.get(nm, dict())
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
for i in args:
j = i.split("=")
if len(j) == 2:
settings[j[0]] = j[1]
print("User setting ", j[0], j[1])
user_session_settings[nm] = settings
# @send_action(ChatAction.TYPING)
async def pics_dir(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
ndx = 0
args = [s1 for s1 in bot.message.text.split() if len(s1) > 0]
if len(args) > 1:
ndx = int(args[1]) - 1
else:
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. Please specify at least one picture number.)</i>",
parse_mode=ParseMode.HTML,
)
return
ndx1 = min(ndx + 20, len(pic_files))
txt = ""
i = ndx
for pf in pic_files[ndx:ndx1]:
d = pic_dir.get(pf, "")
i += 1
txt += "[pic %03d] %s" % (i, pf)
if len(d) > 0:
txt += " " + d
if not os.path.exists(pic_path + pf):
txt += " MISSING"
if not txt.endswith("\n"):
txt += "\n"
if len(txt) == 0:
await context.bot.send_message(
chat_id=bot.message.chat_id, text="<i>(.. No pictures found.)</i>", parse_mode=ParseMode.HTML
)
else:
tx1 = "<i>(.. pictures %d..%d of %d )</i>\n" % (ndx + 1, ndx1, len(pic_files))
await context.bot.send_message(chat_id=bot.message.chat_id, text=tx1 + txt, parse_mode=ParseMode.HTML)
async def just_message(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
txt = bot.message.text
if txt is not None:
if txt.startswith("#"):
args = [s1 for s1 in txt.split() if len(s1) > 0]
if len(args) == 1:
await find_tag(bot, context)
return
if txt.lower().startswith("find "):
await find_substring(bot, context)
return
elif txt.lower().startswith("pic "):
await get_pic(bot, context)
return
elif txt.lower().startswith("docs"):
await all_docs(bot, context)
return
elif txt.lower().startswith("pics"):
await pics_dir(bot, context)
return
elif txt.lower().startswith("tags"):
await tags_dir(bot, context)
return
elif txt.lower().startswith("tag "):
await find_tag(bot, context)
return
elif txt.lower().startswith("del "):
await del_message(bot, context)
return
if not xis_admin_ok(bot, context):
return
if txt is not None:
print(txt)
new_messages.append(txt)
update_messages() ## debug
save_messages() ## debug
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. message saved as [%03d].)</i>" % len(messages),
parse_mode=ParseMode.HTML,
)
return
ph = bot.message.photo
if ph is not None and len(ph) > 0:
txt = bot.message.caption
file_id = ph[-1]["file_id"]
if file_id is None:
print(bot.message)
return
new_file = await context.bot.get_file(file_id)
tst = time.localtime()
file_name = pic_path + "%d-%02d-%02d_%02d%02d%02d" % (
tst.tm_year,
tst.tm_mon,
tst.tm_mday,
tst.tm_hour,
tst.tm_min,
tst.tm_sec,
)
if os.path.exists(file_name + ".jpg"):
ii = 0
while True:
f1 = file_name + "(%d)" % ii
if not os.path.exists(f1 + ".jpg"):
file_name = f1
break
ii += 1
await new_file.download_to_drive(file_name + ".jpg")
if txt is not None:
m = "/p#i#c %s.jpg %s" % (os.path.basename(file_name), txt)
pic_dir[os.path.basename(file_name) + ".jpg"] = txt
new_messages.append(m)
update_messages() ## debug
save_messages() ## debug
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. message saved as [%03d].)</i>" % len(messages),
parse_mode=ParseMode.HTML,
)
pic_files.append(os.path.basename(file_name) + ".jpg")
else:
doc = bot.message.document
if doc is not None:
file_name = doc.file_name
if file_name is not None:
file_id = doc.file_id
print("File ", file_id, file_name)
new_file = context.bot.get_file(file_id)
nm = "%s %s" % (bot.message.from_user.first_name, bot.message.from_user.last_name)
upath = user_session_settings[nm].get("uploads", user_settings[nm].get("uploads", ""))
if not upath.endswith("/"):
upath += "/"
file_name1 = doc_path + upath + file_name
if not os.path.exists(doc_path + upath):
os.mkdir(doc_path + upath)
new_file.download(file_name1)
print("Saving file", file_name1)
m = "/d#o#c %s" % upath + file_name
new_messages.append(m)
update_messages() ## debug
save_messages() ## debug
await context.bot.send_message(
chat_id=bot.message.chat_id,
text="<i>(.. message saved as [%03d].)</i>" % len(messages),
parse_mode=ParseMode.HTML,
)
# @send_action(ChatAction.TYPING)
async def tech_help(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
cmd_list = []
for k, i in updater.handlers.items():
for j in i:
try:
for z in j.command:
cmd_list.append(z)
except:
pass
t = "<i>Available commands:\n</i>"
for c in cmd_list:
t += "%s\n" % c
await context.bot.send_message(chat_id=bot.message.chat_id, text=t, parse_mode=ParseMode.HTML)
# @send_action(ChatAction.TYPING)
async def show_help(bot: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not xis_user_ok(bot, context):
return
t = open(messages_path + "help.txt").read()
await context.bot.send_message(chat_id=bot.message.chat_id, text=t)
if __name__ == "__main__":
for a in sys.argv[1:]:
messages_path = a
print("Bot Folder: ", messages_path)
pic_path = messages_path + "images/"
doc_path = messages_path + "documents/"
secret = open(messages_path + "secret.txt").read()
secret = secret.replace("\n", "")
updater = Application.builder().token(secret).build()
# updater = Updater(secret)
if not os.path.exists(messages_path):
os.mkdir(messages_path)
if not os.path.exists(pic_path):
os.mkdir(pic_path)
if not os.path.exists(doc_path):
os.mkdir(doc_path)
m_name = messages_path + "notes.txt"
if os.path.exists(m_name):
shutil.copyfile(m_name, m_name + ".saved")
read_messages()
cfg = read_config(messages_path + "config.json")
read_users()
build_pic_dir()
updater.add_handler(CommandHandler("hello", hello))
updater.add_handler(CommandHandler("start", hello))
updater.add_handler(CommandHandler("find", find_substring))
updater.add_handler(CommandHandler("f", find_substring))
updater.add_handler(CommandHandler("re", refresh))
updater.add_handler(CommandHandler("refresh", refresh))
updater.add_handler(CommandHandler("d", del_message))
updater.add_handler(CommandHandler("del", del_message))
updater.add_handler(CommandHandler("set", u_settings))
updater.add_handler(CommandHandler("restore", del_u_settings))
updater.add_handler(CommandHandler("settings", show_settings))
updater.add_handler(CommandHandler("docs", all_docs))
updater.add_handler(CommandHandler("pics", pics_dir))
updater.add_handler(CommandHandler("pic", get_pic))
updater.add_handler(CommandHandler("delpic", del_pic))
updater.add_handler(CommandHandler("tags", tags_dir))
updater.add_handler(CommandHandler("tag", find_tag))
updater.add_handler(CommandHandler("help", show_help))
# updater.add_handler(CommandHandler("?", tech_help))
unknown_handler = MessageHandler(filters.COMMAND, unknown_cmd)
updater.add_handler(unknown_handler)
just_handler = MessageHandler(filters.ALL, just_message)
updater.add_handler(just_handler)
updater.run_polling()