-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
98 lines (93 loc) · 5.28 KB
/
views.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
from django.shortcuts import render
# Create your views here.
from django.conf import settings
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
from django.views.decorators.csrf import csrf_exempt
from linebot import LineBotApi, WebhookParser
from linebot.exceptions import InvalidSignatureError, LineBotApiError
from linebot.models import (MessageEvent, PostbackEvent,
TextSendMessage, TextMessage, AudioSendMessage, LocationSendMessage, TemplateSendMessage, VideoSendMessage, StickerSendMessage, ImageSendMessage,
ButtonsTemplate,
PostbackAction, MessageAction, URIAction)
line_bot_api = LineBotApi(settings.LINE_CHANNEL_ACCESS_TOKEN)
parser = WebhookParser(settings.LINE_CHANNEL_SECRET)
#https://zh.wikipedia.org/wiki/%E8%B7%A8%E7%AB%99%E8%AF%B7%E6%B1%82%E4%BC%AA%E9%80%A0
#忽略CSRF防護
@csrf_exempt
def callback(request):
if request.method == 'POST':
signature = request.META['HTTP_X_LINE_SIGNATURE']
body = request.body.decode('utf-8')
try:
events = parser.parse(body, signature)
except InvalidSignatureError:
return HttpResponseForbidden()
except LineBotApiError:
return HttpResponseBadRequest()
for event in events:
if isinstance(event, PostbackEvent):
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text=str(event.postback.data)))
elif isinstance(event, MessageEvent):
if isinstance(event.message, TextMessage):
mtext = event.message.text
if mtext == '文字':
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text="文字測試"))
elif mtext == '貼圖':
message = StickerSendMessage(
package_id='1', sticker_id='1')
line_bot_api.reply_message(event.reply_token, message)
elif mtext == '圖片':
message = ImageSendMessage(
original_content_url='https://avatars3.githubusercontent.com/u/29451488?s=400&v=4', preview_image_url='https://avatars3.githubusercontent.com/u/29451488?s=400&v=4')
line_bot_api.reply_message(event.reply_token, message)
elif mtext == '聲音':
message = AudioSendMessage(
original_content_url='https://57b33a613b19.ngrok.io/static/linebot_server/hello.m4a', duration=2000)
line_bot_api.reply_message(event.reply_token, message)
elif mtext == '影片':
message = VideoSendMessage(
original_content_url='https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4',
preview_image_url='https://57b33a613b19.ngrok.io/static/linebot_server/video.png')
line_bot_api.reply_message(event.reply_token, message)
elif mtext == '地圖':
message = LocationSendMessage(
'公司', '市政路386號', '24.159162090886316', '120.64025491140256', True)
line_bot_api.reply_message(
event.reply_token, message)
elif mtext == '我要預約':
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text="預約完成!消費共10萬元"))
elif mtext == '傳送訊息':
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text="Hi"))
elif mtext == '預約':
message = TemplateSendMessage(
alt_text='是否要預約一日男友?',
template=ButtonsTemplate(
thumbnail_image_url='https://avatars3.githubusercontent.com/u/29451488?s=400&v=4',
title='是否要預約一日男友?',
text='RayJi',
actions=[
PostbackAction(
label='立即預約',
display_text='我要預約',
data='action=buy&itemid=1'
),
MessageAction(
label='傳送訊息',
text='傳送訊息'
),
URIAction(
label='前往首頁',
uri='https://github.com/RayJi0428/learning/tree/main/line_bot'
)
]
)
)
line_bot_api.reply_message(
event.reply_token, message)
return HttpResponse()
else:
return HttpResponseBadRequest()