-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbisbot.py
More file actions
782 lines (636 loc) · 23.4 KB
/
bisbot.py
File metadata and controls
782 lines (636 loc) · 23.4 KB
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
import errno
import os
import sys
import tempfile
import random
from gtts import gTTS
from imdb import IMDb
from argparse import ArgumentParser
from urllib.parse import quote
from kbbi import KBBI
from urbandictionary_top import udtop
from googletrans import Translator
import requests
import wikipedia
import json
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError, LineBotApiError
)
from linebot.models import *
translator = Translator()
wiki_settings = {}
save_file = {}
ytlist = ["https://youtu.be/nBteO-bU78Y", "https://youtu.be/Xrcvig0f92U","https://youtu.be/M276QojLU-s",
"https://youtu.be/I1qo9ZxoF04", "https://youtu.be/KF322xXDffg", "https://youtu.be/a1vNFJMpIUo"]
app = Flask(__name__)
line_bot_api = LineBotApi('CzSF4tZw1pR4X9i8Y6s580+0p7ebCpqq9MpoA98z8zsAl1ObHL+/Bmsk0t6BRk2+W9bxrNQMsUDsiEFcwr3nF7lVx644o8HwAXfr7mMfVhyXPC88CoNZKZxETv+WLa0L/gZoHA3YMc9KFINKeeoP+gdB04t89/1O/w1cDnyilFU=')
handler = WebhookHandler('f69e514026a91f8ba1e5e3c7934eca35')
static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
# function for create tmp dir for download content
def make_static_tmp_dir():
try:
os.makedirs(static_tmp_path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(static_tmp_path):
pass
else:
raise
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
text=event.message.text
if isinstance(event.source, SourceGroup):
subject = line_bot_api.get_group_member_profile(event.source.group_id,
event.source.user_id)
set_id = event.source.group_id
elif isinstance(event.source, SourceRoom):
subject = line_bot_api.get_room_member_profile(event.source.room_id,
event.source.user_id)
set_id = event.source.room_id
else:
subject = line_bot_api.get_profile(event.source.user_id)
set_id = event.source.user_id
def split(text):
return text.split(' ', 1)[-1]
def force_safe(text):
return text.replace('http','https',1)
def wolframs(query):
wolfram_appid = ('83L4JP-TWUV8VV7J7')
url = 'https://api.wolframalpha.com/v2/simple?i={}&appid={}'
return url.format(quote(query), wolfram_appid)
def wolfram(query):
wolfram_appid = ('83L4JP-KWW62H4Y96')
url = 'https://api.wolframalpha.com/v2/result?i={}&appid={}'
return requests.get(url.format(quote(query), wolfram_appid)).text
def trans(word):
sc = 'id'
to = 'en'
if word[0:].lower().strip().startswith('sc='):
sc = word.split(', ', 1)[0]
sc = sc.split('sc=', 1)[-1]
word = word.split(', ', 1)[1]
if word[0:].lower().strip().startswith('to='):
to = word.split(', ', 1)[0]
to = to.split('to=', 1)[-1]
word = word.split(', ', 1)[1]
if word[0:].lower().strip().startswith('sc='):
sc = word.split(', ', 1)[0]
sc = sc.split('sc=', 1)[-1]
word = word.split(', ', 1)[1]
return translator.translate(word, src=sc, dest=to).text
def wiki_get(keyword, set_id, trim=True):
try:
wikipedia.set_lang(wiki_settings[set_id])
except KeyError:
wikipedia.set_lang('en')
try:
result = wikipedia.summary(keyword)
except wikipedia.exceptions.DisambiguationError:
articles = wikipedia.search(keyword)
result = "{} disambiguation:".format(keyword)
for item in articles:
result += "\n{}".format(item)
except wikipedia.exceptions.PageError:
result = "{} not found!".format(keyword)
else:
if trim:
result = result[:2000]
if not result.endswith('.'):
result = result[:result.rfind('.')+1]
return result
def wiki_lang(lang, set_id):
langs_dict = wikipedia.languages()
if lang in langs_dict.keys():
wiki_settings[set_id] = lang
return ("Language has been changed to {} successfully."
.format(langs_dict[lang]))
return ("{} not available!\n"
"See meta.wikimedia.org/wiki/List_of_Wikipedias for "
"a list of available languages, and use the prefix "
"in the Wiki column to set the language."
.format(lang))
def find_kbbi(keyword, ex=False):
try:
entry = KBBI(keyword)
except KBBI.TidakDitemukan as e:
result = str(e)
else:
result = "Definisi {}:\n".format(keyword)
if ex:
result += '\n'.join(entry.arti_contoh)
else:
result += str(entry)
return result
def urban(keyword, ex=True):
try:
entry = udtop(keyword)
except (TypeError, AttributeError, udtop.TermNotFound) :
result = "{} definition not found in urbandictionary.".format(keyword)
else:
result = "{} definition:\n".format(keyword)
if ex:
result += str(entry)
else:
result += entry.definition
return result
def fdetect(url) :
http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
key = "lM4feUDrJJYyKm8s4WvmxgWlVpZE0iNw"
secret = "Y5L_l2a87UAtKeFzL-FD8K3C5OwtAbfA"
attributes="age,gender,ethnicity,beauty"
#add emotion later in atributes
resp = requests.post(http_url,
data = {
'api_key': key,
'api_secret': secret,
'image_url': url,
'return_attributes': attributes}
)
r = resp.json()
if (len(r) == 3 ) :
return ("Face not detected")
dict1 = r['faces']
if ((len(dict1) > 1) or (len(dict1) < 1)):
return ("Please send picture with one face only")
else:
dict2 = dict1[0]['attributes']['gender']['value']
dict3 = dict1[0]['attributes']['age']['value']
dict4 = dict1[0]['attributes']['beauty']
if (dict2 == 'Male') :
dict4 = dict4['male_score']
else:
dict4 = dict4['female_score']
dict5 = dict1[0]['attributes']['ethnicity']['value']
#dict6 = dict1[0]['attributes']['emotion'] add emotion later
return("Gender : " + str(dict2) + "\n" +
"Age : " + str(dict3) + "\n" +
"Beauty : " + str(round(dict4, 2)) + "\n" +
"Ethnicity : " + str(dict5))
def pt(city) :
url = "https://time.siswadi.com/pray/?address={}"
r = requests.get(url.format(city))
data = r.json()
dict1 = data['data']
dict2 = data['location']
dict3 = data['time']
return (str(dict2['address']) + " prayer time " + str(dict3['date']) + "\n"
"Fajr : " + str(dict1['Fajr']) + "\n"
"Sunrise : " + str(dict1['Sunrise']) + "\n"
"Dhuhr : " + str(dict1['Dhuhr']) + "\n"
"Asr : " + str(dict1['Asr']) + "\n"
"Maghrib : " + str(dict1['Maghrib']) + "\n"
"Isha : " + str(dict1['Isha']))
def ig(username) :
url = "https://www.instagram.com/{}"
r = requests.get(url.format(username))
if r.status_code == 404:
return ("Unavailable")
html = r.text
jsondata = html.split("window._sharedData = ")[1].split(";</script>")[0]
data = json.loads(jsondata)
dict1 = data['entry_data']['ProfilePage'][0]['graphql']['user']
return ("User : @"+ dict1['username'] + "\n" + "Name : " + dict1['full_name'] + "\n" +
"Following : " + str(dict1['edge_follow']['count']) + "\n" +
"Followers : " + str(dict1['edge_followed_by']['count']) + "\n" +
"Bio : " + dict1['biography'])
def igs(username) :
url = "https://www.instagram.com/{}"
r = requests.get(url.format(username))
html = r.text
jsondata = html.split("window._sharedData = ")[1].split(";</script>")[0]
data = json.loads(jsondata)
dict1 = data['entry_data']['ProfilePage'][0]['graphql']['user']
return (dict1['profile_pic_url_hd'])
def picg(uri) :
url = uri
r = requests.get(url)
if r.status_code == 404:
return ("Unavailable")
html = r.text
jsondata = html.split("""<script type="application/ld+json">""")[1].split("</script>")[0]
data = json.loads(jsondata)
dict1 = data['caption']
dict1 = dict1[:1900]
data2 = html.split("""og:title" content=\"""")[1].split(":")[0]
return(data2 + " : \n" + dict1 + "\n...")
def picgs(uri) :
url = uri
r = requests.get(url)
html = r.text
data = html.split("""og:image" content=\"""")[1].split("\"")[0]
return (data)
def vigs(uri) :
url = uri
r = requests.get(url)
html = r.text
data = html.split("""og:video" content=\"""")[1].split("\"")[0]
return (data)
def tts(word):
la ='en'
ext = 'mp3'
if word[0:].lower().strip().startswith('la='):
la = word.split(', ', 1)[0]
la = la.split('la=', 1)[-1]
word = word.split(', ', 1)[1]
speech = gTTS(text=word, lang=la)
with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix='mp3-', delete=False) as tf:
speech.write_to_fp(tf)
tempfile_path = tf.name
dist_path = tempfile_path + '.' + ext
dist_name = os.path.basename(dist_path)
os.rename(tempfile_path, dist_path)
url = request.host_url + os.path.join('static', 'tmp', dist_name)
return force_safe(url)
def mreview(movie) :
ia = IMDb()
movies = ia.search_movie(movie)
ID = movies[0].movieID
data = ia.get_movie(ID)
#data.infoset2keys (get all info)
cast = ''
i = 0
while (i != 4) :
cast = cast + "\n" + str(data['cast'][i]) + " as " + str(data['cast'][i].notes)
i += 1
duration = str(data['runtimes']).split("'")[1]
year = str(data['year'])
plot = str(data['plot'][0].split("::")[0])
rate = str(data['rating'])
return ("Title : " + str(movies[0]) + "\n"
"Rating : " + rate + "\n"
"Duration : " + duration + " minutes\n"
"Year : " + year + "\n"
"Main Casts : " + cast + "\n"
"Plot : \n" + str(plot))
def imdbpic(movie) :
ia = IMDb()
movies = ia.search_movie(movie)
ID = movies[0].movieID
data = ia.get_movie(ID)
return (str(data['cover url']))
def ox(keyword):
oxdict_appid = ('7dff6c56')
oxdict_key = ('41b55bba54078e9fb9f587f1b978121f')
word = quote(keyword)
url = ('https://od-api.oxforddictionaries.com:443/api/v1/entries/en/{}'.format(word))
req = requests.get(url, headers={'app_id': oxdict_appid, 'app_key': oxdict_key})
if "No entry available" in req.text:
return 'No entry available for "{}".'.format(word)
req = req.json()
result = ''
i = 0
for each_result in req['results']:
for each_lexEntry in each_result['lexicalEntries']:
for each_entry in each_lexEntry['entries']:
for each_sense in each_entry['senses']:
if 'crossReferenceMarkers' in each_sense:
search = 'crossReferenceMarkers'
else:
search = 'definitions'
for each_def in each_sense[search]:
i += 1
result += '\n{}. {}'.format(i, each_def)
if i == 1:
result = 'Definition of {}:\n'.format(keyword) + result[4:]
else:
result = 'Definitions of {}:'.format(keyword) + result
return result
if text == '/help':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Full command bisbot pwrlvl9k : \n"
"/leave, /kbbi, /wolfram, /wolframs,\n"
"/trans, /wiki, /wikilang, /urban,\n"
"/on, /off, /tts, /stalkig, /photoig,\n"
"/videoig, /imdb, /pt, /fdetect, /ox"))
elif text == '/on':
save_file[set_id] = True
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Save file on"))
elif text == '/off':
save_file[set_id] = False
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Save file off"))
elif text == '/music':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(random.choice(ytlist)))
elif text == '/kbbi':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get meaning of a word from https://kbbi.kemdikbud.go.id/ \n"
"command /kbbi {word}"))
elif text == '/urban':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get meaning of a word from https://www.urbandictionary.com/ \n"
"command /urban {word}"))
elif text == '/ox':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get meaning of a word from https://www.oxforddictionaries.com/ \n"
"command /ox {word}"))
elif text == '/imdb':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get movie review from https://www.imdb.com/ \n"
"command /imdb {movie}"))
elif text == '/fdetect':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get face detection of photo url \n"
"command /fdetect {url}"))
elif text == '/wolfram':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("use https://www.wolframalpha.com/ features, and give the result "
"in simple text \n"
"command /wolfram {input}"))
elif text == '/wolframs':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("use https://www.wolframalpha.com/ features, and give the result "
"in image \n"
"command /wolframs {input}"))
elif text == '/stalkig':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get simple description of instagram account and profile picture \n"
"command /stalkig {username}"))
elif text == '/photoig':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get photo and description of instagram post \n"
"command /photoig {post link}"))
elif text == '/videoig':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get video and description of instagram post \n"
"command /videoig {post link}"))
elif text == '/trans':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get translation from https://translate.google.com/ \n"
"command /trans sc={language code}, to={language code}, {text}"))
elif text == '/tts':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get audio file about how a word pronounced in a language \n"
"command /tts la={language code}, {text}"))
elif text == '/wiki':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get https://www.wikipedia.org/ description of something \n"
"command /wiki {text}"))
elif text == '/wikilang':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("change the language of wikipedia description \n"
"command /wikilang {language code}"))
elif text == '/pt':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("get prayer time of your city \n"
"command /pt {city}"))
elif text == 'Stalk':
if isinstance(event.source, SourceGroup):
try:
profile = line_bot_api.get_group_member_profile(event.source.group_id, event.source.user_id)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Display name: " + profile.display_name + "\n" +
"Profile picture: " + profile.picture_url + "\n" +
"User_ID: " + profile.user_id))
except LineBotApiError:
pass
elif isinstance(event.source, SourceRoom):
try:
profile = line_bot_api.get_room_member_profile(event.source.room_id, event.source.user_id)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Display name: " + profile.display_name + "\n" +
"Profile picture: " + profile.picture_url + "\n" +
"User_ID: " + profile.user_id))
except LineBotApiError:
pass
else:
try:
profile = line_bot_api.get_profile(event.source.user_id)
line_bot_api.reply_message(
event.reply_token,
TextSendMessage("Display name: " + profile.display_name + "\n" +
"Profile picture: " + profile.picture_url + "\n" +
"User_ID: " + profile.user_id))
except LineBotApiError:
pass
elif text == '/leave':
if isinstance(event.source, SourceGroup):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Bye.'))
line_bot_api.leave_group(event.source.group_id)
elif isinstance(event.source, SourceRoom):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Bye.'))
line_bot_api.leave_room(event.source.room_id)
else:
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('Gabisa dodol.'))
elif text=='/wolfram':
line_bot_api.reply_message(
event.reply_token,
TextSendMessage('command /wolfram {input}'))
elif text[0:].lower().strip().startswith('/ox '):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(ox(split(text))))
elif text[0:].lower().strip().startswith('/pt ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(pt(split(text))))
elif text[0:].lower().strip().startswith('/fdetect ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(fdetect(split(text))))
elif text[0:].lower().strip().startswith('/tts ') :
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(tts(split(text))),
AudioSendMessage(original_content_url=tts(split(text)), duration=60000)
])
elif text[0:].lower().strip().startswith('/videoig '):
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(picg(split(text))),
VideoSendMessage(original_content_url= vigs(split(text)),
preview_image_url= picgs(split(text)))
])
elif text[0:].lower().strip().startswith('/photoig '):
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(picg(split(text))),
ImageSendMessage(original_content_url= picgs(split(text)),
preview_image_url= picgs(split(text)))
])
elif text[0:].lower().strip().startswith('/stalkig '):
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(ig(split(text))),
ImageSendMessage(original_content_url= igs(split(text)),
preview_image_url= igs(split(text)))
])
elif text[0:].lower().strip().startswith('/imdb '):
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(mreview(split(text))),
ImageSendMessage(original_content_url= imdbpic(split(text)),
preview_image_url= imdbpic(split(text)))
])
elif text[0:].lower().strip().startswith('/wolfram '):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(wolfram(split(text))))
elif text[0:].lower().strip().startswith('/kbbi '):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(find_kbbi(split(text))))
elif text[0:].lower().strip().startswith('/wolframs '):
line_bot_api.reply_message(
event.reply_token,
ImageSendMessage(original_content_url= wolframs(split(text)),
preview_image_url= wolframs(split(text))))
elif text[0:].lower().strip().startswith('/echo ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(split(text)))
elif text[0:].lower().strip().startswith('/trans ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(trans(split(text))))
elif text[0:].lower().strip().startswith('/urban '):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(urban(split(text))))
elif text[0:].lower().strip().startswith('/wiki ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(wiki_get(split(text), set_id=set_id)))
elif text[0:].lower().strip().startswith('/wikilang ') :
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(wiki_lang(split(text), set_id=set_id)))
@handler.add(MessageEvent, message=StickerMessage)
def handle_sticker_message(event):
line_bot_api.reply_message(
event.reply_token,
StickerSendMessage(
package_id=event.message.package_id,
sticker_id=event.message.sticker_id)
)
# Other Message Type
@handler.add(MessageEvent, message=(ImageMessage, VideoMessage, AudioMessage))
def handle_content_message(event):
if isinstance(event.source, SourceGroup):
subject = line_bot_api.get_group_member_profile(event.source.group_id,
event.source.user_id)
set_id = event.source.group_id
elif isinstance(event.source, SourceRoom):
subject = line_bot_api.get_room_member_profile(event.source.room_id,
event.source.user_id)
set_id = event.source.room_id
else:
subject = line_bot_api.get_profile(event.source.user_id)
set_id = event.source.user_id
try :
if save_file[set_id] :
if isinstance(event.message, ImageMessage):
ext = 'jpg'
elif isinstance(event.message, VideoMessage):
ext = 'mp4'
elif isinstance(event.message, AudioMessage):
ext = 'm4a'
else:
return
message_content = line_bot_api.get_message_content(event.message.id)
with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix=ext + '-', delete=False) as tf:
for chunk in message_content.iter_content():
tf.write(chunk)
tempfile_path = tf.name
dist_path = tempfile_path + '.' + ext
dist_name = os.path.basename(dist_path)
os.rename(tempfile_path, dist_path)
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text='Save content.'),
TextSendMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
])
except KeyError:
save_file[set_id] = False
@handler.add(MessageEvent, message=FileMessage)
def handle_file_message(event):
if isinstance(event.source, SourceGroup):
subject = line_bot_api.get_group_member_profile(event.source.group_id,
event.source.user_id)
set_id = event.source.group_id
elif isinstance(event.source, SourceRoom):
subject = line_bot_api.get_room_member_profile(event.source.room_id,
event.source.user_id)
set_id = event.source.room_id
else:
subject = line_bot_api.get_profile(event.source.user_id)
set_id = event.source.user_id
try :
if save_file[set_id] :
message_content = line_bot_api.get_message_content(event.message.id)
with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix='file-', delete=False) as tf:
for chunk in message_content.iter_content():
tf.write(chunk)
tempfile_path = tf.name
dist_path = tempfile_path + '-' + event.message.file_name
dist_name = os.path.basename(dist_path)
os.rename(tempfile_path, dist_path)
line_bot_api.reply_message(
event.reply_token, [
TextSendMessage(text='Save file.'),
TextSendMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
])
except KeyError:
save_file[set_id] = False
@handler.add(MessageEvent, message=LocationMessage)
def handle_location_message(event):
line_bot_api.reply_message(
event.reply_token,
LocationSendMessage(
title=event.message.title, address=event.message.address,
latitude=event.message.latitude, longitude=event.message.longitude
)
)
@handler.add(JoinEvent)
def handle_join(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='Barista is here... ' + event.source.type + ' apa ini?'))
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
make_static_tmp_dir()
app.run(host='0.0.0.0', port=port)