line-bot-sdk-pythonとherokuで簡単なbotを作っているのですが、フォロー処理を追加すると動いてくれません。
Python3
1# -*- coding: utf-8 -*- 2 3# Licensed under the Apache License, Version 2.0 (the "License"); you may 4# not use this file except in compliance with the License. You may obtain 5# a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations 13# under the License. 14 15import os 16import sys 17from argparse import ArgumentParser 18 19from flask import Flask, request, abort 20from linebot import ( 21 LineBotApi, WebhookHandler 22) 23from linebot.exceptions import ( 24 InvalidSignatureError 25) 26from linebot.models import ( 27 MessageEvent, TextMessage, TextSendMessage, 28) 29 30app = Flask(__name__) 31 32# get channel_secret and channel_access_token from your environment variable 33channel_secret = os.getenv('LINE_CHANNEL_SECRET', None) 34channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None) 35if channel_secret is None: 36 print('Specify LINE_CHANNEL_SECRET as environment variable.') 37 sys.exit(1) 38if channel_access_token is None: 39 print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') 40 sys.exit(1) 41 42line_bot_api = LineBotApi(channel_access_token) 43handler = WebhookHandler(channel_secret) 44 45 46@app.route("/callback", methods=['POST']) 47def callback(): 48 # get X-Line-Signature header value 49 signature = request.headers['X-Line-Signature'] 50 51 # get request body as text 52 body = request.get_data(as_text=True) 53 app.logger.info("Request body: " + body) 54 55 # handle webhook body 56 try: 57 handler.handle(body, signature) 58 except InvalidSignatureError: 59 abort(400) 60 61 return 'OK' 62 63 64 65@handler.add(MessageEvent) 66def message_text(event): 67 TEXT = "Hello!!" 68 line_bot_api.reply_message( 69 event.reply_token, 70 #TextSendMessage(text=event.message.text) 71 TextSendMessage(text=TEXT) 72 ) 73 74# ポート番号の設定 75if __name__ == "__main__": 76# app.run() 77 port = int(os.getenv("PORT", 5000)) 78 app.run(host="0.0.0.0", port=port) 79
テキストメッセージを送信擦るとHello!!と返す簡単なプログラムです。@handler.add(MessageEvent)の前に以下のプログラムを挿入するとエラーになり動いてくれません。
Python3
1@handler.add(FollowEvent) 2def handle_follow(event): 3 TEXT = 'Hello!!!new' 4 line_bot_api.reply_message( 5 event.reply_token, 6 TextSendMessage(text=TEXT))
エラーは以下のとおりです
error
12019-08-03T20:55:08.800232+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=POST path="/callback" host=********.herokuapp.com request_id=******** fwd="***.***.***.**" dyno= connect= service= status=503 bytes= protocol=https
フォロー時の処理を入れないと通常通り動くので謎です。よろしくお願いします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。