質問編集履歴

2

コードを修正しました。

2020/09/13 04:06

投稿

raspypy
raspypy

スコア247

test CHANGED
@@ -1 +1 @@
1
- Heroku 急にくなった LINE BOT
1
+ Heroku 動作し
test CHANGED
@@ -8,327 +8,13 @@
8
8
 
9
9
 
10
10
 
11
- **動かなくなったと判断した理由**
12
-
13
- LINE BOTプログラムですので、LINEからメッセージを送ると、決まった返答がくるプログラムですが、
14
-
15
- LINEからメッセージを送信しても、返答が何もありません。(「既読」にはなります。)
16
-
17
11
 
18
12
 
19
13
  ##プログラム内容
20
14
 
21
15
  deployしたプログラムコードを掲載します。
22
16
 
23
- LINE BOTプログラムです。
24
17
 
25
-
26
-
27
- ```python
28
-
29
- # -*- coding: utf-8 -*-
30
-
31
- from __future__ import unicode_literals
32
-
33
- import errno
34
-
35
- import os
36
-
37
- import sys
38
-
39
- import tempfile
40
-
41
- import logging
42
-
43
- from argparse import ArgumentParser
44
-
45
-
46
-
47
- from flask import Flask, request, abort
48
-
49
-
50
-
51
- from linebot import (
52
-
53
- LineBotApi, WebhookHandler
54
-
55
- )
56
-
57
- from linebot.exceptions import (
58
-
59
- InvalidSignatureError
60
-
61
- )
62
-
63
- from linebot.models import (
64
-
65
- MessageEvent, TextMessage, TextSendMessage,
66
-
67
- SourceUser, SourceGroup, SourceRoom,
68
-
69
- TemplateSendMessage, ConfirmTemplate, MessageTemplateAction,
70
-
71
- ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URITemplateAction,
72
-
73
- PostbackTemplateAction, DatetimePickerTemplateAction,
74
-
75
- CarouselTemplate, CarouselColumn, PostbackEvent,
76
-
77
- StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
78
-
79
- ImageMessage, VideoMessage, AudioMessage, FileMessage,
80
-
81
- UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent
82
-
83
- )
84
-
85
-
86
-
87
- import paho.mqtt.client as mqtt
88
-
89
-
90
-
91
- # ブローカーに接続できたときの処理
92
-
93
- def on_connect(client, userdata, flag, rc):
94
-
95
- print("Connected with result code " + str(rc))
96
-
97
-
98
-
99
- # ブローカーが切断したときの処理
100
-
101
- def on_disconnect(client, userdata, flag, rc):
102
-
103
- if rc != 0:
104
-
105
- print("Unexpected disconnection.")
106
-
107
-
108
-
109
- # publishが完了したときの処理
110
-
111
- def on_publish(client, userdata, mid):
112
-
113
- print("publish: {0}".format(mid))
114
-
115
-
116
-
117
-
118
-
119
- app = Flask(__name__)
120
-
121
-
122
-
123
- # ログを標準出力へ。heroku logs --tail で確認するためです。
124
-
125
- # app.logger.info で出力するため、レベルは INFO にする。
126
-
127
- app.logger.addHandler(logging.StreamHandler(sys.stdout))
128
-
129
- app.logger.setLevel(logging.INFO)
130
-
131
-
132
-
133
- #Secret, tokenは、Exportに設定する方がおすすめ(Web公開されてしまうので…)
134
-
135
- line_bot_api = LineBotApi('hogehogehoge')
136
-
137
- handler = WebhookHandler('hoge')
138
-
139
-
140
-
141
- static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
142
-
143
-
144
-
145
- client = mqtt.Client() # クラスのインスタンス(実体)の作成
146
-
147
- client.on_connect = on_connect # 接続時のコールバック関数を登録
148
-
149
- client.on_disconnect = on_disconnect # 切断時のコールバックを登録
150
-
151
- client.on_publish = on_publish # メッセージ送信時のコールバック
152
-
153
-
154
-
155
- client.tls_set("/etc/ssl/certs/ca-certificates.crt")
156
-
157
-
158
-
159
- client.username_pw_set("xxxxx", "hogehoge")
160
-
161
- client.connect("xxxxx.cloudmqtt.com", xxxxx)
162
-
163
-
164
-
165
-
166
-
167
- # function for create tmp dir for download content
168
-
169
- def make_static_tmp_dir():
170
-
171
- try:
172
-
173
- os.makedirs(static_tmp_path)
174
-
175
- except OSError as exc:
176
-
177
- if exc.errno == errno.EEXIST and os.path.isdir(static_tmp_path):
178
-
179
- pass
180
-
181
- else:
182
-
183
- raise
184
-
185
-
186
-
187
-
188
-
189
- @app.route("/callback", methods=['POST'])
190
-
191
- def callback():
192
-
193
- # get X-Line-Signature header value
194
-
195
- signature = request.headers['X-Line-Signature']
196
-
197
-
198
-
199
- # get request body as text
200
-
201
- body = request.get_data(as_text=True)
202
-
203
- app.logger.info("Request body: " + body)
204
-
205
-
206
-
207
- # handle webhook body
208
-
209
- try:
210
-
211
- handler.handle(body, signature)
212
-
213
- except InvalidSignatureError:
214
-
215
- abort(400)
216
-
217
-
218
-
219
- return 'OK'
220
-
221
-
222
-
223
- @handler.add(MessageEvent, message=TextMessage)
224
-
225
- def handle_text_message(event):
226
-
227
- text = event.message.text
228
-
229
- # 通信処理スタート
230
-
231
- client.loop_start() # subはloop_forever()だが,pubはloop_start()で起動だけさせる
232
-
233
- if text == 'password':
234
-
235
- buttons_template = ButtonsTemplate(
236
-
237
- title='test', text='select', actions=[
238
-
239
- PostbackTemplateAction(label='green', data='green'),
240
-
241
- PostbackTemplateAction(label='red', data='red'),
242
-
243
- PostbackTemplateAction(label='blue', data='blue'),
244
-
245
- PostbackTemplateAction(label='yellow', data='yellow'),
246
-
247
- ])
248
-
249
- template_message = TemplateSendMessage(
250
-
251
- alt_text='Buttons alt text', template=buttons_template)
252
-
253
- line_bot_api.reply_message(event.reply_token, template_message)
254
-
255
- else:
256
-
257
- client.publish("system","xxxx") # トピック名とメッセージを決めて送信
258
-
259
- line_bot_api.reply_message(
260
-
261
- #botは停止中
262
-
263
- #event.reply_token, TextSendMessage(text=event.message.text))
264
-
265
- event.reply_token, TextSendMessage(text='正しいパスワードを入力して下さい。'))
266
-
267
-
268
-
269
- @handler.add(PostbackEvent)
270
-
271
- def handle_postback(event):
272
-
273
- # 通信処理スタート
274
-
275
- client.loop_start() # subはloop_forever()だが,pubはloop_start()で起動だけさせる
276
-
277
- if event.postback.data == 'green':
278
-
279
- client.publish("system","green") # トピック名とメッセージを決めて送信
280
-
281
- line_bot_api.reply_message(
282
-
283
- event.reply_token, TextSendMessage(text='green'))
284
-
285
- elif event.postback.data == 'red':
286
-
287
- client.publish("system","red") # トピック名とメッセージを決めて送信
288
-
289
- line_bot_api.reply_message(
290
-
291
- event.reply_token, TextSendMessage(text='red。'))
292
-
293
- elif event.postback.data == 'blue':
294
-
295
- client.publish("system","blue") # トピック名とメッセージを決めて送信
296
-
297
- line_bot_api.reply_message(
298
-
299
- event.reply_token, TextSendMessage(text='blue'))
300
-
301
- elif event.postback.data == 'yellow':
302
-
303
- client.publish("system","yellow") # トピック名とメッセージを決めて送信
304
-
305
- line_bot_api.reply_message(
306
-
307
- event.reply_token, TextSendMessage(text='yellow'))
308
-
309
-
310
-
311
- if __name__ == "__main__":
312
-
313
- arg_parser = ArgumentParser(
314
-
315
- usage='Usage: python ' + __file__ + ' [--port <port>] [--help]'
316
-
317
- )
318
-
319
- arg_parser.add_argument('-p', '--port', type=int, default=int(os.environ.get('PORT', 8000)), help='port')
320
-
321
- arg_parser.add_argument('-d', '--debug', default=False, help='debug')
322
-
323
- arg_parser.add_argument('--host', default='0.0.0.0', help='host')
324
-
325
- options = arg_parser.parse_args()
326
-
327
-
328
-
329
- app.run(debug=options.debug, host=options.host, port=options.port)
330
-
331
- ```
332
18
 
333
19
 
334
20
 
@@ -342,40 +28,6 @@
342
28
 
343
29
  ```text
344
30
 
345
- 2020-09-07T00:07:40.780756+00:00 heroku[web.1]: State changed from starting to crashed
346
-
347
- 2020-09-07T00:07:40.783097+00:00 heroku[web.1]: State changed from crashed to starting
348
-
349
- 2020-09-07T00:07:42.854094+00:00 heroku[web.1]: Starting process with command `python app.py`
350
-
351
- 2020-09-07T00:07:44.993470+00:00 app[web.1]: Traceback (most recent call last):
352
-
353
- 2020-09-07T00:07:44.993486+00:00 app[web.1]: File "app.py", line 81, in <module>
354
-
355
- 2020-09-07T00:07:44.993495+00:00 app[web.1]: client.connect("tailor.cloudmqtt.com", 21683)
356
-
357
- 2020-09-07T00:07:44.993495+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/paho/mqtt/client.py", line 937, in connect
358
-
359
- 2020-09-07T00:07:44.993651+00:00 app[web.1]: return self.reconnect()
360
-
361
- 2020-09-07T00:07:44.993652+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/paho/mqtt/client.py", line 1071, in reconnect
362
-
363
- 2020-09-07T00:07:44.993854+00:00 app[web.1]: sock = self._create_socket_connection()
364
-
365
- 2020-09-07T00:07:44.993857+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
366
-
367
- 2020-09-07T00:07:44.994573+00:00 app[web.1]: return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
368
-
369
- 2020-09-07T00:07:44.994574+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/socket.py", line 575, in create_connection
370
-
371
- 2020-09-07T00:07:44.994706+00:00 app[web.1]: raise err
372
-
373
- 2020-09-07T00:07:44.994748+00:00 app[web.1]: socket.error: [Errno 111] Connection refused
374
-
375
- 2020-09-07T00:07:45.061209+00:00 heroku[web.1]: Process exited with status 1
376
-
377
- 2020-09-07T00:07:45.107734+00:00 heroku[web.1]: State changed from starting to crashed
378
-
379
31
  2020-09-07T00:30:40.814943+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host=remote-plc.herokuapp.com request_id=1ce21cf8-616b-461e-a524-548ecba183b9 fwd="147.92.150.193" dyno= connect= service= status=503 bytes= protocol=https
380
32
 
381
33
  ```

1

コマンド実行時のログを追加しました。

2020/09/13 04:05

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -28,36 +28,8 @@
28
28
 
29
29
  # -*- coding: utf-8 -*-
30
30
 
31
-
32
-
33
- # Licensed under the Apache License, Version 2.0 (the "License"); you may
34
-
35
- # not use this file except in compliance with the License. You may obtain
36
-
37
- # a copy of the License at
38
-
39
- #
40
-
41
- # http://www.apache.org/licenses/LICENSE-2.0
42
-
43
- #
44
-
45
- # Unless required by applicable law or agreed to in writing, software
46
-
47
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
48
-
49
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
50
-
51
- # License for the specific language governing permissions and limitations
52
-
53
- # under the License.
54
-
55
-
56
-
57
31
  from __future__ import unicode_literals
58
32
 
59
-
60
-
61
33
  import errno
62
34
 
63
35
  import os