-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinebot_hooks.py
More file actions
75 lines (66 loc) · 2.53 KB
/
linebot_hooks.py
File metadata and controls
75 lines (66 loc) · 2.53 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
from flask import request, abort, render_template
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
LineBotApiError, InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
SourceUser, SourceGroup, SourceRoom,
TemplateSendMessage, ConfirmTemplate, MessageAction,
ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
PostbackAction, DatetimePickerAction,
CameraAction, CameraRollAction, LocationAction,
CarouselTemplate, CarouselColumn, PostbackEvent,
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
MemberJoinedEvent, MemberLeftEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, IconComponent, ButtonComponent,
SeparatorComponent, QuickReply, QuickReplyButton,
ImageSendMessage)
import os
CH_ACCESS_TOKEN = os.environ['CH_ACCESS_TOKEN']
CH_SECRET = os.environ['CH_SECRET']
line_bot_api = LineBotApi(CH_ACCESS_TOKEN)
handler = WebhookHandler(CH_SECRET)
def user_text_message_handler(event):
''' Handler for user text message: private environment '''
reply_text = None
return reply_text
def room_text_message_handler(event):
''' Handler for room text message: protected environment '''
reply_text = None
return reply_text
def group_text_message_handler(event):
''' Handler for group text message: public environment '''
reply_text = None
return reply_text
@handler.add(MessageEvent, message=TextMessage)
def text_message_handler(event):
''' Brach to specific text message handler '''
if isinstance(event.source, SourceUser):
reply_text = user_text_message_handler(event)
elif isinstance(event.source, SourceRoom):
reply_text = room_text_message_handler(event)
elif isinstance(event.source, SourceGroup):
reply_text = group_text_message_handler(event)
else:
print('Unknown text message source')
reply_text = None
if reply_text is not None:
if isinstance(reply_text, str):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=reply_text)
)
else:
try:
line_bot_api.reply_message(
event.reply_token, reply_text
)
except:
print('Unknown reply type')
pass