product : use heroku platform
https://yuan-line-chatbot.herokuapp.com/callback
- 前往Line Developer註冊成為Line deeveloper
- Create new provider
- Create a Message API channel
- register some thing
- 完成後,可以看到如下畫面
sudo apt-get install python3-pip
pip3 install line-bot-sdk
sudo apt install python3-django
pip3 install beautifulsoup4
pip3 install requests
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
print("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
if __name__ == "__main__":
app.run()
- 執行LINE Bot應用程式(APP)
python manage.py runserver
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
heroku login -i
heroku create yuan-line-chatbot[你-APP-的名字]
- Procfile
- 假設我們所執行的檔案是app_core web: gunicorn app_core:app –preload
git init
heroku git:remote -a yuan-line-chatbot
# set git remote heroku to https://git.heroku.com/yuan-line-chatbot.git
# git config 檢查
git add .
# git status 檢查
git config user.email "[email protected]"
git config user.name "Chen Yu-An"
git commit -m "v1"
git push heroku master
# remote: Verifying deploy... done.
# To https://git.heroku.com/yuan-line-chatbot.git
# * [new branch] master -> master
- 優化 飲料菜單
- 提供影片功能
將本地服務器暴露到互聯網上,這對於開發和測試非常有用 ngrok http http://localhost:8080
docker build -t my-line-chatbot-app . docker run -d -p 8080:8080 my-line-chatbot-app