-
Notifications
You must be signed in to change notification settings - Fork 0
/
lljbot.py
executable file
·937 lines (784 loc) · 34.5 KB
/
lljbot.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
import webapp2
import logging
import json
import HTMLParser
import textwrap
import scriptures
from google.appengine.api import urlfetch, taskqueue
from google.appengine.ext import db
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
def strip_markdown(string):
return string.replace('*', ' ').replace('_', ' ').replace('`', '\'')
def make_first_line_bold(text):
text_split = text.split('\n', 1)
output = '*' + text_split[0].strip().replace(' ', '\a') + '*'
if len(text_split) > 1:
output += '\n' + strip_markdown(text_split[1].strip())
return output
def to_sup(text):
sups = {u'0': u'\u2070',
u'1': u'\xb9',
u'2': u'\xb2',
u'3': u'\xb3',
u'4': u'\u2074',
u'5': u'\u2075',
u'6': u'\u2076',
u'7': u'\u2077',
u'8': u'\u2078',
u'9': u'\u2079',
u'-': u'\u207b'}
return ''.join(sups.get(char, char) for char in text)
def to_chunks(text):
lines = [line.strip() for line in text.strip().splitlines()]
chunks = []
current_chunk = None
for line in lines:
if line:
if current_chunk:
current_chunk += '\n' + line
else:
current_chunk = line
else:
if current_chunk:
chunks.append(current_chunk)
current_chunk = None
else:
continue
chunks.append(current_chunk)
return chunks
def canonicalise(verse):
c_verse = verse.lower().replace('songs', 'song')
try:
c_verse = scriptures.reference_to_string(*scriptures.extract(c_verse)[0])
except:
return None
first_word = c_verse.partition(' ')[0]
if first_word in ('I', 'II', 'III'):
c_verse = str(len(first_word)) + c_verse.lstrip('I')
elif first_word == 'Psalms':
c_verse = 'Psalm' + c_verse[6:]
elif first_word == 'Song':
c_verse = 'Song of Songs' + c_verse[15:]
elif first_word == 'Revelation':
c_verse = 'Revelation' + c_verse[26:]
return c_verse
def get_devo_old(delta=0):
today_date = datetime.utcnow() + timedelta(hours=8, days=delta)
date_url = today_date.strftime('%Y-%m-%d')
devo_url = 'http://qt.swim.org/user_dir/living/user_print_web.php?edit_all=' + date_url
try:
result = urlfetch.fetch(devo_url, deadline=30)
except Exception as e:
logging.warning('Error fetching devo:\n' + str(e))
return None
try:
html = result.content
soup = BeautifulSoup(html, 'lxml')
date = today_date.strftime('%b %-d, %Y ({})').format(today_date.strftime('%a').upper())
heading_soup = soup.find('td', {'align': 'center', 'class': 'padding'})
heading_soup.find('a', {'class': 'mobile-button'}).decompose()
heading = strip_markdown(heading_soup.text).strip()
bodies = soup.find_all('td', {'align': 'left', 'class': 'padding'})
verse = strip_markdown(bodies[0].find('strong').text)
c_verse = canonicalise(verse)
if c_verse is None:
logging.warning('Unrecognised scripture reference: ' + verse)
raise Exception
verse = c_verse
lines = map(lambda l: l.text.strip(), bodies[0].find('div').find_all('div'))
passage = ''
for line in lines:
if not line:
continue
idx = line.find('.')
num = strip_markdown(line[:idx]).strip()
if num.isdigit():
num = to_sup(num)
rest_of_line = strip_markdown(line[idx + 1:]).strip()
passage += num + ' ' + rest_of_line + '\n'
else:
unnumbered_line = strip_markdown(line).strip()
if ' ' not in unnumbered_line:
unnumbered_line = '_' + unnumbered_line + '_'
passage += unnumbered_line + '\n'
passage = passage.strip()
for tag in bodies[1].select('b'):
text = strip_markdown(tag.text).strip()
tag.string = '*' + text.replace(' ', '\a') + '*'
reflection = bodies[1].text.strip()
application = strip_markdown(bodies[2].text).strip()
reflection_chunks = to_chunks(reflection)
application_chunks = to_chunks(application)
if len(reflection_chunks) == len(application_chunks):
application_chunks = [line.lstrip('-').lstrip() for line in application_chunks]
chunks = [val for pair in zip(reflection_chunks, application_chunks) for val in pair]
else:
chunks = reflection_chunks + application_chunks
reflection = '\n\n'.join(chunks)
prayer = strip_markdown(bodies[3].text).strip()
daynames = ['Yesterday\'s', 'Today\'s', 'Tomorrow\'s']
devo = u'\U0001F4C5' + ' ' + daynames[delta + 1] + ' QT - _' + date + '_\n\n' + \
'*' + heading + '*\n' + verse + '\n\n' + \
u'\U0001F4D9' + ' *Scripture* _(NIV)_\n\n' + passage + '\n\n' + \
u'\U0001F4DD' + ' *Reflection*\n\n' + reflection + '\n\n' + \
u'\U0001F64F' + ' *Prayer*\n\n' + prayer
return devo
except:
if delta == -1:
return 'Sorry, the LLJ website is no longer hosting yesterday\'s material.'
elif delta == 0:
return 'Sorry, the LLJ website does not have today\'s material yet.'
else:
return 'Sorry, the LLJ website hasn\'t made tomorrow\'s material available yet.'
def get_devo(delta=0):
date = (datetime.utcnow() + timedelta(hours=8, days=delta)).strftime('%Y-%m-%d')
devo_url = 'http://www.duranno.com/livinglife/qt/reload_default1.asp?OD=' + date
try:
result = urlfetch.fetch(devo_url, deadline=30)
except Exception as e:
logging.warning('Error fetching devo:\n' + str(e))
return None
content = result.content.decode('utf-8', 'ignore')
h = HTMLParser.HTMLParser()
def get_text(html):
return BeautifulSoup(html, 'lxml').text
def prep_str(string):
string = string.replace('<br>', '\n')
return h.unescape(string).strip()
def prep_passage(string):
result = ''
first = string.find('<!-- bible verse and text -->')
string = string[first:]
while '<!-- bible verse and text -->' in string:
start = string.find('<div class="listTxt">') + 21
end = string.find('</div>', start)
num = to_sup(prep_str(string[start:end]))
start = string.find('<div class="listCon">') + 21
end = string.find('</div>', start)
text = get_text(prep_str(string[start:end]))
result += num + ' ' + strip_markdown(text) + '\n'
string = string[end:]
return result.strip()
def get_remote_date(content):
start = content.find('var videoNowDate = "') + 20
return content[start:start + 10]
if delta != 0 and get_remote_date(content) != date:
if delta == -1:
return 'Sorry, the LLJ website is no longer hosting yesterday\'s material.'
else:
return 'Sorry, the LLJ website hasn\'t made tomorrow\'s material available yet.'
title_start = content.find('<!-- today QT -->')
title_end = content.find('<!-- bible words -->')
title = prep_str(content[title_start:title_end])
start = title.find('<div class="today_m">') + 21
end = title.find('</div>', start)
date = strip_markdown(prep_str(title[start:end]))
start = title.find('<div class="title">') + 59
end = title.find('</a>', start)
heading = strip_markdown(prep_str(title[start:end]))
start = title.find('<div class="sub_title">') + 23
end = title.find('</div>', start)
verse = strip_markdown(prep_str(title[start:end]))
passage_start = title_end
passage_end = content.find('<!-- Reflection-->')
passage = prep_passage(content[passage_start:passage_end])
reflection_start = passage_end
reflection_end = content.find('<!-- Letter to God -->')
reflection = content[reflection_start:reflection_end]
start = reflection.find('<div class="con">') + 17
end = reflection.find('</div>', start)
reflection = strip_markdown(prep_str(reflection[start:end]))
reflection_chunks = to_chunks(reflection)
if len(reflection_chunks) % 2 == 0:
for i in range(0, len(reflection_chunks), 2):
reflection_chunks[i] = make_first_line_bold(reflection_chunks[i])
reflection = '\n\n'.join(reflection_chunks)
prayer_start = reflection_end
prayer_end = content.find('<!-- Share SNS -->')
prayer = content[prayer_start:prayer_end]
start = prayer.find('<div class="con" style="padding-top:25px;">') + 43
end = prayer.find('</div>', start)
prayer = strip_markdown(prep_str(prayer[start:end]))
if not reflection or not prayer or not passage:
return None
daynames = ['Yesterday\'s', 'Today\'s', 'Tomorrow\'s']
devo = u'\U0001F4C5' + ' ' + daynames[delta + 1] + ' QT - _' + date + '_\n\n' + \
'*' + heading + '*\n' + verse + '\n\n' + \
u'\U0001F4D9' + ' *Scripture* _(NIV)_\n\n' + passage + '\n\n' + \
u'\U0001F4DD' + ' *Reflection*\n\n' + reflection + '\n\n' + \
u'\U0001F64F' + ' *Prayer*\n\n' + prayer
return devo
from secrets import TOKEN, ADMIN_ID, BOT_ID, BOTFAMILY_HASH
TELEGRAM_URL = 'https://api.telegram.org/bot' + TOKEN
TELEGRAM_URL_SEND = TELEGRAM_URL + '/sendMessage'
TELEGRAM_URL_SEND_PHOTO = TELEGRAM_URL + '/sendPhoto'
TELEGRAM_URL_CHAT_ACTION = TELEGRAM_URL + '/sendChatAction'
JSON_HEADER = {'Content-Type': 'application/json;charset=utf-8'}
LOG_SENT = '{} {} sent to uid {} ({})'
LOG_ENQUEUED = 'Enqueued {} to uid {} ({})'
LOG_DID_NOT_SEND = 'Did not send {} to uid {} ({}): {}'
LOG_ERROR_SENDING = 'Error sending {} to uid {} ({}):\n{}'
LOG_ERROR_DAILY = 'Error enqueueing dailies:\n'
LOG_ERROR_QUERY = 'Error querying uid {} ({}): {}'
LOG_TYPE_FEEDBACK = 'Type: Feedback\n'
LOG_TYPE_START_NEW = 'Type: Start (new user)'
LOG_TYPE_START_EXISTING = 'Type: Start (existing user)'
LOG_TYPE_NON_TEXT = 'Type: Non-text'
LOG_TYPE_NON_MESSAGE = 'Type: Non-message'
LOG_TYPE_COMMAND = 'Type: Command\n'
LOG_UNRECOGNISED = 'Unrecognised command'
LOG_USER_MIGRATED = 'User {} migrated to uid {} ({})'
LOG_USER_DELETED = 'Deleted uid {} ({})'
LOG_USER_REACHABLE = 'Uid {} ({}) is still reachable'
LOG_USER_UNREACHABLE = 'Unable to reach uid {} ({}): {}'
RECOGNISED_ERROR_PARSE = 'Bad Request: can\'t parse'
RECOGNISED_ERROR_MIGRATE = 'Bad Request: group chat was upgraded to a supergroup chat'
RECOGNISED_ERRORS = ('PEER_ID_INVALID',
'Bot was blocked by the user',
'Forbidden: user is deleted',
'Forbidden: user is deactivated',
'Forbidden: User is deactivated',
'Forbidden: bot was blocked by the user',
'Forbidden: Bot was blocked by the user',
'Forbidden: bot was kicked from the group chat',
'Forbidden: bot was kicked from the channel chat',
'Forbidden: bot was kicked from the supergroup chat',
'Forbidden: bot is not a member of the supergroup chat',
'Forbidden: bot can\'t initiate conversation with a user',
'Forbidden: Bot can\'t initiate conversation with a user',
'Bad Request: chat not found',
'Bad Request: PEER_ID_INVALID',
'Bad Request: group chat was deactivated',
'Bad Request: have no rights to send a message',
'Bad Request: not enough rights to send text messages to the chat',
RECOGNISED_ERROR_MIGRATE)
def telegram_post(data, deadline=10):
return urlfetch.fetch(url=TELEGRAM_URL_SEND, payload=data, method=urlfetch.POST,
headers=JSON_HEADER, deadline=deadline)
def telegram_query(uid, deadline=10):
data = json.dumps({'chat_id': uid, 'action': 'typing'})
return urlfetch.fetch(url=TELEGRAM_URL_CHAT_ACTION, payload=data, method=urlfetch.POST,
headers=JSON_HEADER, deadline=deadline)
def telegram_photo(data, deadline=10):
return urlfetch.fetch(url=TELEGRAM_URL_SEND_PHOTO, payload=data, method=urlfetch.POST,
headers=JSON_HEADER, deadline=deadline)
def get_today_time():
today = (datetime.utcnow() + timedelta(hours=8)).date()
today_time = datetime(today.year, today.month, today.day) - timedelta(hours=8)
return today_time
class User(db.Model):
username = db.StringProperty(indexed=False)
first_name = db.StringProperty(multiline=True, indexed=False)
last_name = db.StringProperty(multiline=True, indexed=False)
created = db.DateTimeProperty(auto_now_add=True)
last_received = db.DateTimeProperty(auto_now_add=True, indexed=False)
last_sent = db.DateTimeProperty(indexed=False)
last_auto = db.DateTimeProperty(auto_now_add=True)
active = db.BooleanProperty(default=True)
promo = db.BooleanProperty(default=False)
def get_uid(self):
return self.key().name()
def get_name_string(self):
def prep(string):
return string.encode('utf-8', 'ignore').strip()
name = prep(self.first_name)
if self.last_name:
name += ' ' + prep(self.last_name)
if self.username:
name += ' @' + prep(self.username)
return name
def get_description(self):
user_type = 'group' if self.is_group() else 'user'
return user_type + ' ' + self.get_name_string()
def is_group(self):
return int(self.get_uid()) < 0
def is_active(self):
return self.active
def set_active(self, active):
self.active = active
self.put()
def set_promo(self, promo):
self.promo = promo
self.put()
def update_last_received(self):
self.last_received = datetime.now()
self.put()
def update_last_sent(self):
self.last_sent = datetime.now()
self.put()
def update_last_auto(self):
self.last_auto = get_today_time()
self.put()
def migrate_to(self, uid):
props = dict((prop, getattr(self, prop)) for prop in self.properties().keys())
props.update(key_name=str(uid))
new_user = User(**props)
new_user.put()
self.delete()
return new_user
def get_user(uid):
key = db.Key.from_path('User', str(uid))
user = db.get(key)
if user == None:
user = User(key_name=str(uid), first_name='-')
user.put()
return user
def update_profile(uid, uname, fname, lname):
existing_user = get_user(uid)
if existing_user:
existing_user.username = uname
existing_user.first_name = fname
existing_user.last_name = lname
existing_user.update_last_received()
#existing_user.put()
return existing_user
else:
user = User(key_name=str(uid), username=uname, first_name=fname, last_name=lname)
user.put()
return user
def send_message(user_or_uid, text, msg_type='message', force_reply=False, markdown=False,
disable_web_page_preview=False):
try:
uid = str(user_or_uid.get_uid())
user = user_or_uid
except AttributeError:
uid = str(user_or_uid)
user = get_user(user_or_uid)
def send_short_message(text, countdown=0):
build = {
'chat_id': uid,
'text': text.replace('\a', ' ')
}
if force_reply:
build['reply_markup'] = {'force_reply': True}
if markdown:
build['parse_mode'] = 'Markdown'
if msg_type == 'promo' or disable_web_page_preview:
build['disable_web_page_preview'] = True
data = json.dumps(build)
def queue_message():
payload = json.dumps({
'msg_type': msg_type,
'data': data
})
taskqueue.add(url='/message', payload=payload, countdown=countdown)
logging.info(LOG_ENQUEUED.format(msg_type, uid, user.get_description()))
if msg_type in ('daily', 'promo', 'mass'):
if msg_type == 'daily':
user.update_last_auto()
elif msg_type == 'promo':
user.set_promo(True)
queue_message()
return
try:
result = telegram_post(data)
except Exception as e:
logging.warning(LOG_ERROR_SENDING.format(msg_type, uid, user.get_description(), str(e)))
queue_message()
return
response = json.loads(result.content)
error_description = str(response.get('description'))
if error_description.startswith(RECOGNISED_ERROR_PARSE):
if build.get('parse_mode'):
del build['parse_mode']
data = json.dumps(build)
queue_message()
elif handle_response(response, user, uid, msg_type) == False:
queue_message()
if len(text) > 4096:
chunks = textwrap.wrap(text, width=4096, replace_whitespace=False, drop_whitespace=False)
i = 0
for chunk in chunks:
send_short_message(chunk, i)
i += 1
else:
send_short_message(text)
def handle_response(response, user, uid, msg_type):
if response.get('ok') == True:
msg_id = str(response.get('result').get('message_id'))
logging.info(LOG_SENT.format(msg_type.capitalize(), msg_id, uid, user.get_description()))
user.update_last_sent()
else:
error_description = str(response.get('description'))
if error_description.startswith(RECOGNISED_ERROR_PARSE):
logging.warning(LOG_ERROR_SENDING.format(msg_type, uid, user.get_description(),
error_description))
return True
if error_description not in RECOGNISED_ERRORS:
logging.warning(LOG_ERROR_SENDING.format(msg_type, uid, user.get_description(),
error_description))
return False
logging.info(LOG_DID_NOT_SEND.format(msg_type, uid, user.get_description(),
error_description))
if error_description == RECOGNISED_ERROR_MIGRATE:
new_uid = response.get('parameters', {}).get('migrate_to_chat_id')
if new_uid:
user = user.migrate_to(new_uid)
logging.info(LOG_USER_MIGRATED.format(uid, new_uid, user.get_description()))
else:
user_description = user.get_description()
user.delete()
logging.info(LOG_USER_DELETED.format(uid, user_description))
return True
user.set_active(False)
if msg_type == 'promo':
user.set_promo(False)
return True
def send_typing(uid):
data = json.dumps({'chat_id': uid, 'action': 'typing'})
try:
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, url=TELEGRAM_URL_CHAT_ACTION, payload=data,
method=urlfetch.POST, headers=JSON_HEADER)
except:
return
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('LLJBot backend running...\n')
class LljPage(webapp2.RequestHandler):
CMD_LIST = '\n\n' + \
'/today - get today\'s material\n' + \
'/yesterday - get yesterday\'s material\n' + \
'/tomorrow - get tomorrow\'s material'
CMD_UNSUB = '/unsubscribe - disable automatic updates'
CMD_SUB = '/subscribe - re-enable automatic updates'
CMD_LIST_UNSUB = CMD_LIST + '\n' + CMD_UNSUB
CMD_LIST_SUB = CMD_LIST + '\n' + CMD_SUB
WELCOME_GROUP = 'Hello, friends in {}! Thanks for adding me in! ' + \
'This group chat is now subscribed.'
WELCOME_USER = 'Hello, {}! Welcome! You are now subscribed.'
WELCOME_GET_STARTED = ' You may enter one of the following commands:' + CMD_LIST_UNSUB + \
'\n\nIn the meantime, here\'s today\'s material to get you started!'
REMOTE_ERROR = 'Sorry, I\'m having some difficulty accessing the LLJ website. ' + \
'Please try again later.'
SUB_ALREADY = 'Looks like you are already subscribed!'
SUB_SUCCESS = 'Success!'
SUB_APPENDIX = ' You will receive material every day at midnight, Singapore time :)'
UNSUB_ALREADY = 'Looks like you already unsubscribed! Don\'t worry; ' + \
'you won\'t be receiving any more automatic updates.'
UNSUB_SUCCESS = 'You have successfully unsubscribed and will no longer receive ' + \
'automatic updates. Use /subscribe if this was a mistake.'
UNSUB_APPENDIX = ' You can still get material manually by using the commands :)'
SETTINGS_SUB = 'You are currently *subscribed*. Use /unsubscribe to change this.'
SETTINGS_UNSUB = 'You are currently *not subscribed*. Use /subscribe if this is a mistake.'
HELP = 'Hi {}, please enter one of the following commands:'
HELP_LINK = 'Enjoy using LLJ Bot? Click the link below to rate it!\n' + \
'https://telegram.me/storebot?start=lljbot'
FEEDBACK_STRING = 'Please reply with your feedback. ' + \
'I will relay the message to my developer.'
FEEDBACK_ALERT = 'Feedback from {} ({}){}:\n{}'
FEEDBACK_SUCCESS = 'Your message has been sent to my developer. ' + \
'Thanks for your feedback, {}!'
UNRECOGNISED = 'Sorry {}, I couldn\'t understand that. ' + \
'Please enter one of the following commands:'
def post(self):
data = json.loads(self.request.body)
logging.debug(self.request.body)
msg = data.get('message')
if not msg:
logging.info(LOG_TYPE_NON_MESSAGE)
return
msg_chat = msg.get('chat')
msg_from = msg.get('from')
if msg_chat.get('type') == 'private':
uid = msg_from.get('id')
first_name = msg_from.get('first_name')
last_name = msg_from.get('last_name')
username = msg_from.get('username')
else:
uid = msg_chat.get('id')
first_name = msg_chat.get('title')
last_name = None
username = None
user = update_profile(uid, username, first_name, last_name)
actual_id = msg_from.get('id')
name = first_name.encode('utf-8', 'ignore').strip()
actual_username = msg_from.get('username')
if actual_username:
actual_username = actual_username.encode('utf-8', 'ignore').strip()
actual_name = msg_from.get('first_name').encode('utf-8', 'ignore').strip()
actual_last_name = msg_from.get('last_name')
if actual_last_name:
actual_last_name = actual_last_name.encode('utf-8', 'ignore').strip()
text = msg.get('text')
if text:
text = text.encode('utf-8', 'ignore')
if text == '/botfamily_verification_code':
send_message(user, BOTFAMILY_HASH)
send_message(ADMIN_ID, 'Botfamily verified! :D')
return
def get_from_string():
name_string = actual_name
if actual_last_name:
name_string += ' ' + actual_last_name
if actual_username:
name_string += ' @' + actual_username
return name_string
msg_reply = msg.get('reply_to_message')
if msg_reply and str(msg_reply.get('from').get('id')) == BOT_ID and \
msg_reply.get('text') == self.FEEDBACK_STRING:
logging.info(LOG_TYPE_FEEDBACK + str(text))
if user.is_group():
group_string = ' via group {} ({})'.format(name, uid)
else:
group_string = ''
msg_dev = self.FEEDBACK_ALERT.format(get_from_string(), actual_id, group_string, text)
msg_user = self.FEEDBACK_SUCCESS.format(actual_name)
send_message(ADMIN_ID, msg_dev)
send_message(user, msg_user)
return
if user.last_sent == None or text == '/start':
if user.last_sent == None:
logging.info(LOG_TYPE_START_NEW)
new_user = True
else:
logging.info(LOG_TYPE_START_EXISTING)
new_user = False
if not user.is_active():
user.set_active(True)
if user.is_group():
response = self.WELCOME_GROUP.format(name)
else:
response = self.WELCOME_USER.format(name)
response += self.WELCOME_GET_STARTED
send_message(user, response)
send_typing(uid)
response = get_devo()
if response == None:
response = self.REMOTE_ERROR
send_message(user, response, markdown=True)
if new_user:
if user.is_group():
new_alert = 'New group: "{}" via user: {}'.format(name, get_from_string())
else:
new_alert = 'New user: ' + get_from_string()
send_message(ADMIN_ID, new_alert)
return
if text == None:
logging.info(LOG_TYPE_NON_TEXT)
migrate_to_chat_id = msg.get('migrate_to_chat_id')
if migrate_to_chat_id:
new_uid = migrate_to_chat_id
user = user.migrate_to(new_uid)
logging.info(LOG_USER_MIGRATED.format(uid, new_uid, user.get_description()))
return
logging.info(LOG_TYPE_COMMAND + text)
cmd = text.lower().strip()
short_cmd = ''.join(cmd.split())
def is_command(word):
flexi_pattern = ('/{}@lljbot'.format(word), '@lljbot/{}'.format(word))
return cmd == '/' + word or short_cmd.startswith(flexi_pattern)
if is_command('today'):
send_typing(uid)
response = get_devo()
if response == None:
response = get_devo_old()
if response == None:
response = self.REMOTE_ERROR
send_message(user, response, markdown=True)
elif is_command('yesterday'):
send_typing(uid)
response = get_devo(-1)
if response == None:
response = get_devo_old(-1)
if response == None:
response = self.REMOTE_ERROR
send_message(user, response, markdown=True)
elif is_command('tomorrow'):
send_typing(uid)
response = get_devo(1)
if response == None:
response = get_devo_old(1)
if response == None:
response = self.REMOTE_ERROR
send_message(user, response, markdown=True)
elif is_command('subscribe'):
if user.is_active():
response = self.SUB_ALREADY
else:
user.set_active(True)
response = self.SUB_SUCCESS
response += self.SUB_APPENDIX
send_message(user, response)
elif is_command('unsubscribe') or is_command('stop') or is_command('off'):
if not user.is_active():
response = self.UNSUB_ALREADY
else:
user.set_active(False)
response = self.UNSUB_SUCCESS
response += self.UNSUB_APPENDIX
send_message(user, response)
elif is_command('settings'):
if user.is_active():
response = self.SETTINGS_SUB
else:
response = self.SETTINGS_UNSUB
send_message(user, response, markdown=True)
elif is_command('help'):
response = self.HELP.format(actual_name)
if user.is_active():
response += self.CMD_LIST_UNSUB
else:
response += self.CMD_LIST_SUB
response += '\n\n' + self.HELP_LINK
send_message(user, response, disable_web_page_preview=True)
elif is_command('feedback'):
response = self.FEEDBACK_STRING
send_message(user, response, force_reply=True)
else:
logging.info(LOG_UNRECOGNISED)
if user.is_group() and '@lljbot' not in cmd:
return
response = self.UNRECOGNISED.format(actual_name)
if user.is_active():
response += self.CMD_LIST_UNSUB
else:
response += self.CMD_LIST_SUB
send_message(user, response)
class SendPage(webapp2.RequestHandler):
def run(self):
query = User.all()
query.filter('active =', True)
query.filter('last_auto <', get_today_time())
devo = get_devo()
if devo == None:
devo = get_devo_old()
if devo == None:
return False
try:
for user in query.run(batch_size=5000):
send_message(user, devo, msg_type='daily', markdown=True)
except Exception as e:
logging.warning(LOG_ERROR_DAILY + str(e))
return False
return True
def get(self):
if self.run() == False:
taskqueue.add(url='/send')
def post(self):
if self.run() == False:
self.abort(502)
class PromoPage(webapp2.RequestHandler):
def get(self):
taskqueue.add(url='/promo')
def post(self):
three_days_ago = datetime.now() - timedelta(days=3)
query = User.all()
query.filter('promo =', False)
query.filter('created <', three_days_ago)
for user in query.run(batch_size=500):
name = user.first_name.encode('utf-8', 'ignore').strip()
if user.is_group():
promo_msg = 'Hello, friends in {}! Do you find LLJ Bot useful?'.format(name)
else:
promo_msg = 'Hi {}, do you find LLJ Bot useful?'.format(name)
promo_msg += ' Why not rate it on the bot store (you don\'t have to exit' + \
' Telegram)!\nhttps://telegram.me/storebot?start=lljbot'
send_message(user, promo_msg, msg_type='promo')
class MessagePage(webapp2.RequestHandler):
def post(self):
params = json.loads(self.request.body)
msg_type = params.get('msg_type')
data = params.get('data')
uid = str(json.loads(data).get('chat_id'))
user = get_user(uid)
try:
result = telegram_post(data, 4)
except Exception as e:
logging.warning(LOG_ERROR_SENDING.format(msg_type, uid, user.get_description(), str(e)))
logging.debug(data)
self.abort(502)
response = json.loads(result.content)
if handle_response(response, user, uid, msg_type) == False:
logging.debug(data)
self.abort(502)
# class PhotoPage(webapp2.RequestHandler):
# def post(self):
# uid = self.request.body
# user = get_user(uid)
# build = {
# 'chat_id': uid,
# 'photo': 'AgADBQAD0agxGwgAAcgBjTgu4ZTCQJCCVb4yAARl_44G6ouSJaWxAAIC'
# }
# data = json.dumps(build)
# try:
# result = telegram_photo(data, 4)
# except Exception as e:
# logging.warning(LOG_ERROR_SENDING.format('Photo', uid, user.get_description(),
# str(e)))
# logging.debug(data)
# self.abort(502)
# response = json.loads(result.content)
# if handle_response(response, user, uid, 'photo') == False:
# logging.debug(data)
# self.abort(502)
class MassPage(webapp2.RequestHandler):
def get(self):
taskqueue.add(url='/mass')
def post(self):
# try:
# query = User.all()
# for user in query.run(batch_size=3000):
# uid = str(user.get_uid())
# name = user.first_name.encode('utf-8', 'ignore').strip()
# if user.is_group():
# mass_msg = 'Merry Christmas, friends in {}!'.format(name)
# else:
# mass_msg = 'Merry Christmas, {}!'.format(name)
# mass_msg += ' This Christmas, may you be filled with peace, joy and the greatest present of all - God\'s presence! '
# mass_msg += u'\U0001F31F\U0001F476\U0001F381'.encode('utf-8', 'ignore')
# mass_msg += '\n\n_"The virgin will conceive and give birth to a son, and they will call him Immanuel" (which means "God with us"). - Matthew 1:23_'
# send_message(user, mass_msg, msg_type='mass', markdown=True)
# except Exception as e:
# logging.error(e)
pass
class VerifyPage(webapp2.RequestHandler):
def get(self):
try:
query = User.all()
query.filter('active =', False)
for user in query.run(batch_size=3000):
uid = str(user.get_uid())
taskqueue.add(url='/verify', payload=uid)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Cleanup in progress\n')
except Exception as e:
logging.error(e)
def post(self):
uid = self.request.body
user = get_user(uid)
try:
result = telegram_query(uid, 4)
except Exception as e:
logging.warning(LOG_ERROR_QUERY.format(uid, user.get_description(), str(e)))
self.abort(502)
response = json.loads(result.content)
if response.get('ok') == True:
logging.info(LOG_USER_REACHABLE.format(uid, user.get_description()))
else:
error_description = str(response.get('description'))
if error_description == RECOGNISED_ERROR_MIGRATE:
new_uid = response.get('parameters', {}).get('migrate_to_chat_id')
if new_uid:
user = user.migrate_to(new_uid)
logging.info(LOG_USER_MIGRATED.format(uid, new_uid, user.get_description()))
elif error_description in RECOGNISED_ERRORS:
user_description = user.get_description()
user.delete()
logging.info(LOG_USER_DELETED.format(uid, user_description))
else:
logging.warning(LOG_USER_UNREACHABLE.format(uid, user.get_description(),
error_description))
self.abort(502)
app = webapp2.WSGIApplication([
('/', MainPage),
('/' + TOKEN, LljPage),
('/send', SendPage),
('/message', MessagePage),
('/promo', PromoPage),
('/mass', MassPage),
('/verify', VerifyPage),
# ('/photo', PhotoPage),
], debug=True)