質問編集履歴

1

2021/05/20 05:28

投稿

SN____R
SN____R

スコア8

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,105 @@
24
24
 
25
25
 
26
26
 
27
- ```Python
27
+ ```
28
28
 
29
+ from flask import Flask, request, abort
30
+
31
+
32
+
33
+ from linebot import (
34
+
35
+ LineBotApi, WebhookHandler
36
+
37
+ )
38
+
39
+ from linebot.exceptions import (
40
+
41
+ InvalidSignatureError
42
+
43
+ )
44
+
45
+ from linebot.models import (
46
+
47
+ MessageEvent, TextMessage, TextSendMessage,
48
+
49
+ )
50
+
51
+
52
+
53
+ app = Flask(__name__)
54
+
55
+
56
+
57
+ line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
58
+
59
+ handler = WebhookHandler('YOUR_CHANNEL_SECRET')
60
+
61
+
62
+
29
- ソースコード
63
+ #追加した部分
64
+
65
+ @app.route("/")
66
+
67
+ def hello_world():
68
+
69
+ return "hello world!"
70
+
71
+
72
+
73
+ @app.route("/callback", methods=['POST'])
74
+
75
+ def callback():
76
+
77
+ # get X-Line-Signature header value
78
+
79
+ signature = request.headers['X-Line-Signature']
80
+
81
+
82
+
83
+ # get request body as text
84
+
85
+ body = request.get_data(as_text=True)
86
+
87
+ app.logger.info("Request body: " + body)
88
+
89
+
90
+
91
+ # handle webhook body
92
+
93
+ try:
94
+
95
+ handler.handle(body, signature)
96
+
97
+ except InvalidSignatureError:
98
+
99
+ abort(400)
100
+
101
+
102
+
103
+ return 'OK'
104
+
105
+
106
+
107
+
108
+
109
+ @handler.add(MessageEvent, message=TextMessage)
110
+
111
+ def handle_message(event):
112
+
113
+ line_bot_api.reply_message(
114
+
115
+ event.reply_token,
116
+
117
+ TextSendMessage(text=event.message.text))
118
+
119
+
120
+
121
+
122
+
123
+ if __name__ == "__main__":
124
+
125
+ app.run()
30
126
 
31
127
  ```
32
128