(3) 把app所需要的檔案放到資料夾中,主要有四個:
requirements.txt --> 需要安裝在python環境中的套件(記得加gunicorn),每次push時會檢查有無心套件需要安裝
runtime.txt --> 指定python版本(用最新的比較保險 吧)
Procfile --> 指定需要運行的py檔
app.py --> 就是設定restful api(flask)的py檔,檔名必須要跟Procfile內容一樣
可在Heroku網頁中看log,方便找api的bug不然根本黑盒子
https://dashboard.heroku.com/apps/你的app名稱/logs
# coding='utf-8'
# 回音機器人app.py
from flask import Flask,jsonify
from flask import request
from flask import abort
from datetime import datetime
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
import requests
import json
app = Flask(__name__)
line_bot_api = LineBotApi('Access token')
handler = WebhookHandler('Channel secret')
@app.route('/callback', methods=['POST'])
def callback():
# get X-Line-Signature header value
try:
signature = request.headers['X-Line-Signature']
except Exception as ex:
app.logger.info("X-Line-Signature error")
status.update({'error-X-Line-Signature':str(ex)})
try:
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
except Exception as ex:
app.logger.info("Request body error")
status.update({'error-request.get_data':str(ex)})
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'Hello, World!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
# 說啥就回啥
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=event.message.text))
return 0
if __name__ == '__main__':
app.run()