-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
1168 lines (1012 loc) · 39.2 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
#coding: utf-8
import time
import random
import datetime
from ast import literal_eval
import pytz
import pymysql
import sqlalchemy
import urllib
import requests
import json
import langcodes
from html.parser import HTMLParser
from config import *
import corgi
import gtrans
from telegraph import telegraph
import id
import re
import wwstats
import sfake
import make_sticker
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, BaseFilter, CallbackQueryHandler, Job, RegexHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext.dispatcher import run_async
import logging
from mwt import MWT
engline = None
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
MWT(timeout=60*60)
REMINDER_REGEX = re.compile(r'((\d+)d)?((\d*)m)?((\d*)s)?')
def start(bot, update):
msg = update.message
text = "Thank you for starting me! Use /help to get some brief help. Feel free to contact @jeffffc if you have questions."
if msg.chat_id > 0:
msg.reply_text(text)
def addtest(bot, update):
msg = update.message
if msg.reply_to_message:
with open("testtest.txt", "a") as myfile:
myfile.write(str(msg.reply_to_message.message_id) + "\n")
msg.reply_text("Done.")
def showtest(bot, update):
chat_id = update.message.chat.id
with open("testtest.txt", "r") as myfile:
data = myfile.read().splitlines()
msgid = int(random.choice(data))
bot.forward_message(chat_id, chat_id, msgid)
def error(bot, update, error):
logger.warn('Update {} caused error {}'.format(update, error))
def calculatesfake(bot, update, args):
msg = update.message
add(msg)
reply_to = msg.reply_to_message
if args:
try:
num = int(args[0])
answer = sfake.calc(num)
msg.reply_markdown("```{}```".format(answer))
except Exception:
msg.reply_text("Not a number..")
elif reply_to:
try:
num = int(reply_to.text)
answer = sfake.calc(num)
msg.reply_markdown("```{}```".format(answer))
except Exception:
msg.reply_text("Not a number..")
else:
msg.reply_markdown("Use `/sf <num>` or reply to a number.")
def achv(bot, update):
msg = update.message
add(msg)
from_id = update.from_user.id
try:
msg1, msg2 = wwstats.check(from_id)
bot.send_message(from_id, msg1, parse_mode='Markdown') # Jono: maybe change to msg.from_user.send_message if it works
bot.send_message(from_id, msg2, parse_mode='Markdown')
if msg.chat_id > 0:
msg.reply_text("I sent you your achivements in private.")
except Exception:
keyboard = [[InlineKeyboardButton("Start Me!", url="telegram.dog/"+BOT_USERNAME)]]
markup = InlineKeyboardMarkup(keyboard)
msg.reply_text("Please click 'Start Me' to receive my message!", reply_markup=markup)
def dict(bot, update, args):
msg = update.message
add(msg)
bey = checkbanned(msg.from_user.id)
if bey == 1:
return
elif not args:
msg.reply_markdown("Use `/dict <word>`", quote=True)
elif len(args) > 1:
msg.reply_markdown("Use `/dict <word>`", quote=True)
else:
word = args[0]
result = dict_go(word)
msg.reply_markdown(result, quote=True)
def dict_go(word):
url = "https://od-api.oxforddictionaries.com/api/v1/entries/en/{}/definitions".format(word.lower())
randnum = random.randint(0,1)
if randnum == 0:
apikey = OXFORD_API_1
else:
apikey = OXFORD_API_2
appid = OXFORD_ID
try:
r = requests.get(url, headers = {'app_id': appid, 'app_key': apikey})
result = r.json()
list = result['results'][0]['lexicalEntries']
msg = "Definition(s) of word `{}`:\n".format(word)
num = 1
for each in list:
msg += "{}: `{}`\n".format(num, each['entries'][0]['senses'][0]['definitions'][0][:-1])
num += 1
return msg
except Exception:
msg = "Sorry, I cannot find the definitions of word `{}`.".format(word)
return msg
def ud(bot, update, args):
msg = update.message
add(msg)
bey = checkbanned(msg.from_user.id)
if bey == 1:
return
elif not args:
msg.reply_markdown("Use `/ud <something here>`", quote=True)
else:
word = " ".join(args)
result = ud_go(word)
msg.reply_markdown(result, quote=True)
def ud_go(word):
url = "https://mashape-community-urban-dictionary.p.mashape.com/define?term=" + urllib.parse.quote(word.lower(), safe='')
apikey = UD_API
try:
r = requests.get(url, headers = {"X-Mashape-Key": UD_API, "Accept": "text/plain"})
result = r.json()
lst = result['list']
if not lst:
msg = "Sorry. No results for `{}`.".format(word)
return msg
msg = "Query of `{}` on Urban Dictionary:\n".format(word)
num = 1
limit = 1
for each in lst:
msg += "{}: `{}`\n".format(num, each['definition'])
break # Jono: I'd rather use list[0] than a loop then, but idk what u r doing
return msg
except Exception:
msg = "Sorry, I found nothing about `{}` on Urban Dictionary.".format(word)
return msg
def showinfo(bot, update):
msg = update.message
info = id.showinfo(msg)
msg.reply_markdown(info)
def tg(bot, update):
msg = update.message
if msg.reply_to_message:
url = telegraph(msg.reply_to_message)
msg.reply_text(url)
else:
msg.reply_text("Reply to a message")
def repeat(bot, update):
msg = update.message
try:
text = msg.text.split(" ", 1)[1]
except IndexError:
return
msg.chat.send_action('typing')
# escape_chars = '\*_`\['
# text = re.sub(r'([%s])' % escape_chars, r'\\\1', msg)
time.sleep(3)
try:
msg.reply_markdown(text, disable_web_page_preview=True)
except BadRequest as e:
error = str(BadRequest)
if error.startswith("Can't parse entities:"):
msg.reply_text(error)
else:
raise
def get_admin_ids(bot, chat_id):
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)]
def corgii(bot, update):
msg = update.message
chat_id = msg.chat.id
msgid = msg.message_id
add(msg)
msg.chat.send_action('upload_photo')
link = corgi.corgi()
msg.reply_photo(photo=link, caption="BUTTIFUL!", quote=True)
def add(msg):
chat_type = msg.chat.type
chat_id = msg.chat.id
msgid = msg.message_id
from_id = msg.from_user.id
reply_to = msg.reply_to_message
from_user_name = msg.from_user.full_name
# from_user_e = db2.escape_string(from_user_name) # Jono: Duplicate?
from_username = msg.from_user.username
from_user_e = from_user_name
cursor = engine.connect().connection.cursor()
try:
if chat_type in ('group', 'supergroup'):
group_id = chat_id
group_name = msg.chat.title
checkgroupexist = "select * from `group` where groupid=%s"
groupcount = cursor.execute(checkgroupexist, (group_id,))
if groupcount == 0:
newgroup = 1
addgroup = "insert into `group` (`name`, `groupid`) values (%s, %s)"
cursor.execute(addgroup, (group_name, group_id))
# db2.commit()
else:
newgroup = 0
updategroup = "update `group` set name=%s where groupid=%s"
cursor.execute(updategroup, (group_name, group_id))
# db2.commit()
except Exception:
print("Add/Update Group error")
try:
adduser = "insert into user (`name`, `telegramid`, `username`) values (%s, %s, %s)"
cursor.execute(adduser, (from_user_e, from_id, from_username))
# db2.commit()
except Exception:
updateuser = "update user set name=%s, username=%s where telegramid=%s"
cursor.execute(updateuser, (from_user_e, from_username, from_id))
# db2.commit()
if reply_to:
to_user = reply_to.from_user
to_user_name = to_user.full_name
to_user_name_e = to_user_name
to_user_id = to_user.id
to_user_username = to_user.username
try:
addreplyuser = "insert into user (name, username, telegramid) values (%s, %s, %s)"
cursor.execute(addreplyuser, (to_user_name_e, to_user_username, to_user_id))
# db2.commit()
except Exception:
editreplyuser = "update user set name=%s, username=%s where telegramid=%s"
cursor.execute(editreplyuser, (to_user_name_e, to_user_username, to_user_id))
# db2.commit()
cursor.close()
@run_async
def stickers(bot, update):
msg = update.message
add(msg)
if msg.chat_id < 0:
return
elif not msg.reply_to_message:
msg.reply_text("Reply to an image")
elif not msg.reply_to_message.photo:
msg.reply_text("The replied message contains no image.")
else:
photo_id = msg.reply_to_message.photo[-1].file_id
sendmsg = "Downloading and Checking your image..."
sent = msg.reply_text(sendmsg)
url = bot.get_file(photo_id).file_path
sendmsg += "\nResizing your image..."
sent.edit_text(sendmsg)
sendpath = make_sticker.convert(url)
sendmsg += "\nDone!"
sent.edit_text(sendmsg)
msg.chat.send_action("upload_document")
msg.reply_document(open(sendpath, 'rb'))
def money(bot, update, groupdict):
amount = str(groupdict['amount']).replace(",", "")
a = groupdict['a'].upper()
b = groupdict['b'].upper()
url = "http://apilayer.net/api/live?access_key={}¤cies={},{}".format(CURRENCY_API_1, a, b)
url2 = "http://apilayer.net/api/live?access_key={}¤cies={},{}".format(CURRENCY_API_2, a, b)
# url = "http://api.fixer.io/latest"
# url += "?base=" + a + "&symbols=" + b
try:
response = requests.get(url)
except Exception:
response = requests.get(url2)
result = response.json()
# rate = result['rates'][b]
# after = "%.3f" % (float(amount)*rate)
# msg = "`" + amount + " " + a + "` = `" + str(after) + b + "`"
xa = "USD{}".format(a)
xb = "USD{}".format(b)
aftera = result['quotes'][xa]
afterb = result['quotes'][xb]
after = "%.3f" % float(float(amount) * float(afterb) / float(aftera))
with open("currency.json", 'r') as currencyjson:
currency = json.load(currencyjson)
afull = currency[a]
bfull = currency[b]
msg = "`{} {}({})` = `{} {}({})`".format(amount, afull, a, after, bfull, b)
update.message.reply_markdown(msg)
def t(to_lang, text):
if to_lang != 'English':
to_langcode = str(langcodes.find(to_lang))
# a = trans.trans2(to_langcode, text)
a = gtrans.trans(text, to_langcode)
else:
a = gtrans.trans(text)
original_langcode = a[0]
x = langcodes.Language.get(original_langcode)
lang = x.language_name()
if x.region_name() != None:
lang += " (" + x.region_name() + ")"
if x.script_name() != None:
lang += " - " + x.script_name()
translated = a[1]
output = "Translated from: %s\n" % lang
if to_lang != 'English':
y = langcodes.Language.get(to_langcode)
outputlang = y.language_name()
if y.region_name():
lang += " (" + y.region_name() + ")"
if y.script_name():
lang += " - " + y.script_name()
output += "Translated to: %s\n" % outputlang
output += "`%s`" % translated
return output
def translatee(bot, update, args):
msg = update.message
reply_to = msg.reply_to_message
add(msg)
bey = checkbanned(msg.from_user.id)
if bey == 1:
return
elif not args:
if reply_to:
tr = t('English', reply_to.text)
msg.reply_markdown(tr, quote=True)
else:
msg.reply_markdown("Reply to a message to translate, or use `/t <something here>`", quote=True)
else:
if reply_to:
to_lang = 'English'
if len(args) == 1:
to_lang = args[0]
tr = t(to_lang, reply_to.text)
msg.reply_markdown(tr, quote=True)
else:
to_lang = 'English'
if args[0][0] == "*":
to_lang = args[0][1:]
args.pop(0)
before = " ".join(args)
after = t(to_lang, before)
msg.reply_markdown(after, quote=True)
#def google(commandonly, querytype, querytext, chat_id, msgid):
# querytype = update.message.chat
# if commandonly == 1:
# bot.sendMessage(chat_id, "Please use `!gg <QUERY>` to search.", reply_to_message_id=reply_to, parse_mode='Markdown')
# return
# url = "https://www.googleapis.com/customsearch/v1?"
# apikey = GOOGLE_API
# cseid = CSE_ID
# url += "key=" + apikey
# url += "&cx=" + CSE_ID
# url += "&q=" + urllib.parse.quote(querytext)
# if querytype == 'text':
# print("Searching text")
# elif querytype == 'image':
# print("Searching image")
# url += "&searchType=image"
# print(url)
# response = requests.get(url)
# result = response.json()
# s_title = result['items'][0]['title']
# s_link = result['items'][0]['link']
# gmsg = "Search Result for <code>%s</code>:\n" % querytext
# gmsg += "<code>%s</code>\n" % s_title
# gmsg += "<a href=%s>Click here</a>" % s_link
# print(gmsg)
# bot.sendMessage(chat_id, gmsg, reply_to_message_id=msgid, parse_mode='HTML', disable_web_page_preview='True')
def pat(bot, update, args):
msg = update.message
chat_id = msg.chat.id
msgid = msg.message_id
from_id = msg.from_user.id
from_user_name = msg.from_user.full_name
chat_type = msg.chat.type
reply_to = msg.reply_to_message
if reply_to:
reply_to_id = reply_to.message_id
to_user = reply_to.from_user
to_user_id = to_user.id
to_user_name = to_user.full_name
add(msg)
bey = checkbanned(from_id)
if bey == 1:
return
elif not reply_to:
if args:
msg.reply_text("* pats {} *".format(args.join(" ")))
else:
msg.reply_text("* pats {} *".format(from_user_name))
return
cursor = engine.connect().connection.cursor()
sql = "select count(patid) from patdb"
try:
cursor.execute(sql)
data = cursor.fetchall()
for row in data:
patcount = row[0]
except Exception:
print("ERROR at count pat desc")
patnum = random.randint(1, patcount)
sql2 = "select patdesc from patdb where patid = %s"
try:
cursor.execute(sql2, (patnum,))
data = cursor.fetchall()
for row in data:
patdesc = row[0]
except Exception:
print("ERROR")
patmsg = "{} {} {}.".format(to_user_name, patdesc, from_user_name)
msg.reply_text(patmsg, reply_to_message_id=reply_to_id)
patcountadd=("update user set pattedby = (pattedby + 1) where telegramid=%s" % to_user_id)
patbycountadd=("update user set patted = (patted + 1) where telegramid=%s" % from_id)
try:
cursor.execute(patcountadd)
cursor.execute(patbycountadd)
# db2.commit()
except:
print("ERROR AT ADD PAT COUNT")
cursor.close()
def feedback(bot, update, args):
msg = update.message
chat_id = msg.chat.id
msgid = msg.message_id
from_id = msg.from_user.id
from_name = msg.from_user.full_name
from_username = update.message.from_user.username
add(msg)
bey = checkbanned(from_id)
if bey == 1:
return
elif not args:
msg.reply_markdown("Use `/feedback <Message here>` to send feedback to me!")
else:
fb = "FEEDBACK FROM: %s (%s)\n".format(from_name, from_id)
fb += " ".join(args)
bot.send_message(chat_id=ADMIN_ID, text=msg)
fbmessage = " ".join(args)
fbsql = "insert into feedback (message, name, username, telegramid) values (%s, %s, %s, %s)"
cursor = engine.connect().connection.cursor()
cursor.execute(fbsql, (fbmessage, from_name, from_username, from_id))
# db2.commit()
cursor.close()
msg.reply_text("Feedback sent!", quote=True)
def jsql(bot, update, args):
msg = update.message
chat_id = msg.chat.id
msgid = msg.message_id
from_id = msg.from_user.id
add(msg)
bey = checkbanned(from_id)
if bey == 1:
return
elif from_id != ADMIN_ID:
msg.reply_text("You are not {}!".format(ADMIN_NAME), quote=True)
return
cursor = engine.connect().connection.cursor()
try:
enteredsql = " ".join(args)
print(enteredsql)
cursor.execute(enteredsql)
# db2.commit()
result = cursor.fetchone()
sqlmsg = "PERFORMING SQL QUERY:\n"
colnames= [i[0] for i in cursor.description]
sqlmsg += "`{}`\n".format(colnames)
while result:
sqlmsg = "{}`{}`\n".format(sqlmsg, result)
result = cursor.fetchone()
sqlmsg += "`num of affected rows: {}`".format(cursor.rowcount)
msg.reply_markdown(sqlmsg, quote=True)
except pymysql.MySQLError as e:
code, errormsg = e.args
sqlerror = "`MySQL ErrorCode: {}\nErrorMsg: {}`".format(code, errormsg)
msg.reply_markdown(sqlerror, quote=True)
cursor.close()
def patstat(bot, update):
msg = update.message
from_id = msg.from_user.id
chat_id = msg.chat.id
msgid = msg.message_id
from_user_name = msg.from_user.full_name
add(msg)
bey = checkbanned(from_id)
if bey == 1:
return
cursor = engine.connect().connection.cursor()
checkpatcount=("select patted, pattedby from user where telegramid=%s" % from_id)
cursor.execute(checkpatcount)
patcount = cursor.fetchall()
for row in patcount:
pats = row["patted"]
patsby = row["pattedby"]
patcountstr="Hello {}!\nYou have patted others {} times and got patted by others {} times.".format(from_user_name, pats, patsby)
msg.reply_markdown(patcountstr, quote=True)
cursor.close()
def myloc(bot, update, args):
msg = update.message
from_id = msg.from_user.id
chat_id = msg.chat.id
msgid = msg.message_id
add(msg)
bey = checkbanned(from_id)
cursor = engine.connect().connection.cursor()
if bey == 1:
return
elif not args:
msg.reply_markdown("Please use `/myloc <location>` to set your location.", quote=True)
else:
setloc = " ".join(args)
setlocsql = "update user set loc=%s where telegramid=%s"
cursor.execute(setlocsql, (setloc, from_id))
# db2.commit()
setmsg = "Your location is set to {}.".format(setloc)
msg.reply_markdown(setmsg, quote=True)
cursor.close()
@run_async
def now(bot, update, args):
msg = update.message
from_id = msg.from_user.id
chat_id = msg.chat.id
msgid = msg.message_id
add(msg)
cursor = engine.connect().connection.cursor()
bey = checkbanned(from_id)
if bey == 1:
return
try:
if args:
loc = " ".join(args)
else:
checkloc="select loc from user where telegramid=%s" % from_id
cursor.execute(checkloc)
# db2.commit()
result = cursor.fetchall()
for row in result:
userloc = row[0]
# db2.commit()
if not userloc:
text = "Please use `/myloc <location>` to set default location or use `/now <location>`."
msg.reply_markdown(text, quote=True)
return
else:
loc = userloc
'''
url = "http://dataservice.accuweather.com/locations/v1/search"
ran = random.randint(0,1)
if ran == 0:
apikey = ACCU_API_1
else:
apikey = ACCU_API_2
url += "?apikey=" + apikey
url += "&q=" + urllib.parse.quote(loc)
response = requests.get(url)
result = response.json()
locationkey = result[0]['Key']
place = result[0]['LocalizedName'] + ", "
place += result[0]['AdministrativeArea']['LocalizedName'] + ", "
place += result[0]['Country']['LocalizedName']
localtzname = result[0]['TimeZone']['Name']
localtz = pytz.timezone(localtzname)
local = str(datetime.datetime.now(localtz))
url = "http://dataservice.accuweather.com/currentconditions/v1/"
url += locationkey
url += "?apikey=" + apikey
response = requests.get(url)
result = response.json()
localdate = local.split(" ", 1)[0]
localtimeandzone = local.split(" ", 1)[1]
localtime = localtimeandzone[:8]
localzone = localtimeandzone[-6:]
weather = result[0]['WeatherText']
ctemp = str(result[0]['Temperature']['Metric']['Value']) + "°" + result[0]['Temperature']['Metric']['Unit']
ftemp = str(result[0]['Temperature']['Imperial']['Value']) + "°" + result[0]['Temperature']['Imperial']['Unit']
'''
url = "http://api.apixu.com/v1/current.json"
url += "?key=" + APIXU_API
url += "&q=" + urllib.parse.quote(loc)
response = requests.get(url)
result = response.json()
place = "{}, {}, {}".format(result['location']['name'], result['location']['region'], result['location']['country'])
ctemp = "{} °C".format(result['current']['temp_c'])
ftemp = "{} °F".format(result['current']['temp_f'])
weather = result['current']['condition']['text']
localtime = str(result['location']['localtime'])
localzone = datetime.datetime.now(pytz.timezone(result['location']['tz_id'])).strftime('%z')
wmsg = "Currently at: " + place
wmsg += "\nTemperature:`\t{} or {}`".format(ctemp, ftemp)
wmsg += "\nDescription:`\t{}`".format(weather)
wmsg += "\nLocal Time:`\t{} (UTC{})`".format(localtime, localzone)
msg.reply_markdown(wmsg, quote=True)
except Exception:
print("Looks like some nub used /now Mars")
msg.reply_text("Something wrong with your location... or something wrong with me...", quote=True)
cursor.close()
def checkbanned(from_id):
from_id = int(from_id)
cursor = engine.connect().connection.cursor()
bansql = "select banned from user where telegramid=%s" % from_id
cursor.execute(bansql)
try:
banned = cursor.fetchall()
for row in banned:
ban = row[0]
# db2.commit()
return ban
except Exception:
return -1
cursor.close()
def jban(bot, update, args):
msg= update.message
from_id = msg.from_user.id
add(msg)
cursor = engine.connect().connection.cursor()
bey = checkbanned(from_id)
if bey == 1:
return
elif from_id != ADMIN_ID:
msg.reply_text("You are not %s!".format(ADMIN_NAME), quote=True)
elif not args:
msg.reply_markdown("Use `/jban <id>`", quote=True)
elif args[0]:
banid = args[0]
print(banid)
if banid.isdigit():
banid = int(banid)
if checkbanned(banid) == 1:
print("ban already")
msg.reply_text("User banned already")
elif checkbanned(banid) == -1:
print("id wrong")
msg.reply_text("ID wrong")
else:
print("now banning")
bannow = "update user set banned=1 where telegramid={}".format(banid)
try:
cursor.execute(bannow)
# db2.commit()
print("banned")
msg.reply_text("Ban Successful.")
except:
print("fail")
msg.reply_text("Failed. Try again.")
else:
print("not id")
msg.reply_text("not an id")
cursor.close()
def junban(bot, update, args):
msg = update.message
from_id = msg.from_user.id
add(msg)
cursor = engine.connect().connection.cursor()
bey = checkbanned(from_id)
if bey == 1:
return
elif from_id != ADMIN_ID:
msg.reply_text("You are not %s!".format(ADMIN_NAME), quote=True)
elif not args:
msg.reply_markdown("Use `/junban <id>`", quote=True)
elif args[0]:
unbanid = args[0]
if unbanid.isdigit():
unbanid = int(unbanid)
if checkbanned(unbanid) == 0:
msg.reply_text("User was not banned")
elif checkbanned(unbanid) == -1:
msg.reply_text("ID Wrong")
else:
unbannow = "update user set banned=0 where telegramid={}".format(unbanid)
try:
cursor.execute(unbannow)
# db2.commit()
msg.reply_text("Unban Successful")
except:
msg.reply_text("Failed, try again.")
else:
print("not id")
msg.reply_text("Not ID")
cursor.close()
def jbanlist(bot, update):
from_id = update.message.from_user.id
chat_id = update.message.chat.id
msgid = update.message.message_id
add(update.message)
cursor = engine.connect().connection.cursor()
bey = checkbanned(from_id)
if bey == 1:
return
if from_id != ADMIN_ID:
bot.sendMessage(chat_id, "You are not %s!" % ADMIN_NAME, reply_to_message_id=msgid)
return
banlistsql = "select name, username, telegramid from user where banned=1"
cursor.execute(banlistsql)
# db2.commit()
result=cursor.fetchone()
sqlmsg = "Banned users:\n"
if not result:
sqlmsg = "No banned users"
while result:
sqlmsg+="`"+str(result)+"`\n"
result=cursor.fetchone()
bot.sendMessage(chat_id, sqlmsg, reply_to_message_id=msgid, parse_mode='Markdown')
cursor.close()
def nopm(bot, chat_id, from_name, msgid):
nopmmsg = from_name + ", Please start me at PM first."
keyboard = [[InlineKeyboardButton("Start Me!", callback_data = 'start')]]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.sendMessage(chat_id, nopmmsg, reply_to_message_id=msgid, reply_markup=reply_markup)
def button(bot, update):
query = update.callback_query
queryid = query.id
query_from_id = query.from_user.id
query_from_first = query.from_user.first_name
query_from_last = query.from_user.last_name
if query_from_last is not None:
query_from_name = query_from_first + " " + query_from_last
else:
query_from_name = query_from_first
msg = query.message
chat_id = msg.chat.id
msgid = msg.message_id
tomsg = query.message.reply_to_message
# from_id = chat_data['from_id']
if query.data == 'start':
starturl="telegram.me/" + BOT_USERNAME + "?start=help"
bot.answerCallbackQuery(queryid, url = starturl)
if query.data == 'achv':
starturl="telegram.me/" + BOT_USERNAME + "?start=achv"
bot.answerCallbackQuery(queryid, url=starturl)
def help(bot, update):
chat_type = update.message.chat.type
from_id = update.message.from_user.id
chat_id = update.message.chat.id
from_name = update.message.from_user.first_name
msgid = update.message.message_id
add(update.message)
bey = checkbanned(from_id)
if bey == 1:
return
helpmsg = "Availble Commands:\n"
helpmsg += "`/pat: [single use or by reply], pats someone`\n"
helpmsg += "`/patstat: chat your pat history`\n"
helpmsg += "`/myloc <location>: set your current location for using /now`\n"
helpmsg += "`/now (<location>): return current weather for your already set location (or inputted location)`\n"
helpmsg += "`/feedback <message>: send feedback to me!`\n"
helpmsg += "`/t <text>: (or by reply), translate to english`"
# print(helpmsg)
# try:
bot.sendMessage(from_id, helpmsg, parse_mode='Markdown')
if chat_type != 'private':
bot.sendMessage(chat_id, "I've sent you the help message in private.", reply_to_message_id=msgid)
# except:
# if chat_type != 'private':
# nopm(bot, chat_id, from_name, msgid)
def calc_callback(bot, update, args):
if not args:
return
query = "".join(args)
try:
answer = literal_eval(query)
msg = "The answer is `{}`.".format(answer)
except:
msg = "Error occured. Try again."
update.message.reply_text(msg, parse_mode='Markdown')
def send(bot, update, args):
chat_type = update.message.chat.type
from_id = update.message.from_user.id
chat_id = update.message.chat.id
from_name = update.message.from_user.first_name
msgid = update.message.message_id
reply_to = update.message.reply_to_message
if reply_to:
to_user_id = reply_to.from_user.id
add(update.message)
cursor = engine.connect().connection.cursor()
bey = checkbanned(from_id)
if bey == 1:
return
if from_id != ADMIN_ID:
bot.sendMessage(chat_id, ("You are not %s!" % ADMIN_NAME), reply_to_message_id=msgid)
return
if not reply_to:
if not args:
bot.sendMessage(chat_id, "Use `/send <id> <message>`", reply_to_message_id=msgid, parse_mode='Markdown')
return
if len(args) <= 1:
bot.sendMessage(chat_id, "Use `/send <id> <message>`", reply_to_message_id=msgid, parse_mode='Markdown')
return
sendperson = args[0]
args.pop(0)
sendmessage = " ".join(args)
# personplusmessage = after_command
if sendperson.isdigit():
try:
bot.sendMessage(sendperson, sendmessage)
bot.sendMessage(chat_id, "Message sent", reply_to_message_id=msgid)
except:
bot.sendMessage(chat_id, "Send Failed", reply_to_message_id=msgid)
elif sendperson[1:].isdigit():
bot.sendMessage(sendperson, sendmessage)
bot.sendMessage(chat_id, "Message sent")
else:
sendperson = sendperson[1:]
personsql="select telegramid from user where username=%s" % sendperson
cursor.execute(personsql)
# db2.commit()
result = cursor.fetchall()
for row in result:
item = row[0]
# db2.commit()
try:
bot.sendMessage(item, sendmessage)
bot.sendMessage(chat_id, "Message sent", reply_to_message_id=msgid)
except:
bot.sendMessage(chat_id, "Send Failed", reply_to_message_id=msgid)
else:
sendperson = to_user_id
sendmessage = " ".join(args)
if reply_to.forward_from is not None:
sendperson = reply_to.forward_from.id
try:
bot.sendMessage(sendperson, sendmessage)
bot.sendMessage(chat_id, "Message sent", reply_to_message_id=msgid)
except:
bot.sendMessage(chat_id, "Send Failed", reply_to_message_id=msgid)
cursor.close()
@run_async
def search_id_callback(bot, update, args):
if not args:
update.message.reply_text("Please provide an id or username.")
return
inputtext = args[0]
sent = update.message.reply_text("Please wait...")
try:
inputtext = int(inputtext)
except:
pass
r = requests.get(
"http://api.jpwr.ga/bot226774066:AAFPoonQPpn8QmtR99_TPUS-mqwmWdAuJAA/getchat?chat_id={}".format(
inputtext)).json()
msg = ""
msg += "ID: `{}`\n".format(r['result']['id'])
if "first_name" in r['result']:
msg += "First Name: {}\n".format(escape_markdown(r['result']['first_name']))
if "last_name" in r['result']:
msg += "Last Name: {}\n".format(escape_markdown(r['result']['last_name']))
if 'username' in r['result']:
msg += "Username: @{}\n".format(escape_markdown(r['result']['username']))
if "title" in r['result']:
msg += "Title: *{}*\n".format(escape_markdown(r['result']['title']))
if "about" in r['result']:
msg += "About: _{}_".format(escape_markdown(r['result']['about']))
sent.edit_text(msg, parse_mode='Markdown')
def escape_markdown(text):
"""Helper function to escape telegram markup symbols"""
escape_chars = '\*_`\[\]'
return re.sub(r'([%s])' % escape_chars, r'\\\1', text)
def save_message(bot, update):
reply_to_msg = update.message.reply_to_message
try:
reply_to_msg.forward(update.message.from_user.id)
except:
update.message.reply_text("You have not started me in private...")