以下等を大いに参考にさせて頂き、
LINEBOTを作成中です。
Python を使ってオウム返し「LINE ボット」を作ってみる
しかし、heroku側がLINE APIの環境設定を取得できないため、
ご教授願います。
Python
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 33# 加工済 34channel_secret = os.getenv('channel_secret', None) 35channel_access_token = os.getenv('channel_access_token', None) 36if channel_access_token is None: 37 print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') 38 sys.exit(1) 39if channel_secret is None: 40 print('Specify LINE_CHANNEL_SECRET as environment variable.') 41 sys.exit(1) 42 43line_bot_api = LineBotApi(channel_access_token) 44handler = WebhookHandler(channel_secret) 45 46 47@app.route("/callback", methods=['POST']) 48def callback(): 49 # get X-Line-Signature header value 50 signature = request.headers['X-Line-Signature'] 51 52 # get request body as text 53 body = request.get_data(as_text=True) 54 app.logger.info("Request body: " + body) 55 56 # handle webhook body 57 try: 58 handler.handle(body, signature) 59 except InvalidSignatureError: 60 abort(400) 61 62 return 'OK' 63 64 65@handler.add(MessageEvent, message=TextMessage) 66def message_text(event): 67 line_bot_api.reply_message( 68 event.reply_token, 69 TextSendMessage(text=event.message.text) 70 ) 71 72 73if __name__ == "__main__": 74 port = int(os.getenv('PORT', 5000)) 75 app.run(debug=True, host='0.0.0.0', port=port) 76
Procfile
1web: python app.py
runtimetxt
1python-3.6.5
requirementstxt
1Flask==1.0.2 2line-bot-sdk==1.8.0
以下、実行ログです。
log
12018-08-11T07:09:10.827984+00:00 app[api]: Scaled to web@0:Free by user user@gmail.com 22018-08-11T07:09:19.944704+00:00 app[api]: Scaled to web@1:Free by user user@gmail.com 32018-08-11T07:09:24.352115+00:00 heroku[web.1]: Starting process with command `python app.py` 42018-08-11T07:09:27.351649+00:00 heroku[web.1]: Process exited with status 1 52018-08-11T07:09:27.380863+00:00 heroku[web.1]: State changed from starting to crashed 62018-08-11T07:09:27.382379+00:00 heroku[web.1]: State changed from crashed to starting 72018-08-11T07:09:27.212414+00:00 app[web.1]: Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable. 82018-08-11T07:09:36.227684+00:00 heroku[web.1]: Starting process with command `python app.py` 92018-08-11T07:09:39.964362+00:00 heroku[web.1]: Process exited with status 1 102018-08-11T07:09:40.046458+00:00 heroku[web.1]: State changed from starting to crashed 112018-08-11T07:09:39.841881+00:00 app[web.1]: Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.
環境
Windows10
VMware
ubuntu-18.04
python 3.6.5
Flask 1.0.2
line-bot-sdk 1.8.0
git 2.17.1
どのようにすれば、channel_secret、channel_access_tokenを取得出来るようになるのでしょうか。
コードはあまり関係無いかもしれません。
よろしくお願いいたします。