å¨å·è¡ç¨å¼ç¢¼åï¼è¥çºMacçä½æ¥ç³»çµ±ï¼æéTermailãè¥çºWindowsä½æ¥ç³»çµ±ä¸¦ä½¿ç¨Anacondaï¼è«æéAnaconda Promptï¼ä¸¦è¼¸å ¥ä»¥ä¸æ令å®è£Line Botæéä¹å¥ä»¶ï¼
pip3 install line-bot-sdk
ä»ç´¹å¦ä½å°GithubãHerokuãLine Developersä¸åå¹³å°ä¸²æ¥åä¸èµ·ãè«è§çå®æåé¢èª²ç¨å¨è§çæ¬æ¬¡èª²ç¨ï¼
Herokuæéè¦çæ件ï¼è®Herokuç主æ©ç¥éè¦éåæ©å¨äººç主ç¨å¼æ¯åªä¸åãå æ¤æªæ¡app.pyçå稱è¥æåä¿®æ¹ï¼é裡é¢ä¹å¿ é ä¸èµ·ä¿®æ¹ã
web: python app.py
å訴Herokuè¦å®è£åªä¸äºå¥ä»¶ãrequirementçææå°±æ¯å 決æ¢ä»¶ï¼è¦æ建éåæ©å¨äººï¼éè¦ç¨å°çå¥ä»¶ï¼å°±æ¯ä»çå 決æ¢ä»¶ã
line-bot-sdk
bs4
flask
pymongo
datetime
pandas
SnowNLP
emoji
pyshorteners
scipy
æ©å¨äººç主ç¨å¼ï¼æ¬æç« æä¿®æ¹çç¨å¼ç¢¼é½å¨éåæªæ¡ä¸ãæªæ¡å §çæ¹æ³handle_message(event)æ¯ä¸»è¦ç·¨è¼¯åå¡ï¼ææè使ç¨è å°ççé輯é½å¨é裡實ä½ã
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 2 21:16:35 2021
@author: Ivan
çæ¬å±¬æ¼ãè¡é·æ¬é²å¤§ç¨å¼ãææï¼è¥æçåï¼å¯è¯çµ¡ivanyang0606@gmail.com
Line Botè天æ©å¨äºº
第ä¸ç« Line Botç³è«è串æ¥
Line Botæ©å¨äººä¸²æ¥è測試
"""
#è¼å
¥LineBotæéè¦çå¥ä»¶
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import *
app = Flask(__name__)
# å¿
é æ¾ä¸èªå·±çChannel Access Token
line_bot_api = LineBotApi('ä½ èªå·±çtoken')
# å¿
é æ¾ä¸èªå·±çChannel Secret
handler = WebhookHandler('ä½ èªå·±çsecret')
line_bot_api.push_message('ä½ èªå·±çID', TextSendMessage(text='ä½ å¯ä»¥éå§äº'))
# ç£è½ææä¾èª /callback ç Post Request
@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:
abort(400)
return 'OK'
#è¨æ¯å³éåå¡
##### åºæ¬ä¸ç¨å¼ç·¨è¼¯é½å¨éåfunction #####
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
message = TextSendMessage(text=event.message.text)
line_bot_api.reply_message(event.reply_token,message)
#主ç¨å¼
import os
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
æåååç³è«å¥½çæ©å¨äººå°æ¡ä¸ï¼æä¸åãChannel secretãçé¸é ï¼è²¼å°æ¨ååä¸è¼çapp.pyæªæ¡ä¸ï¼ç¬¬17è¡çä½ç½®ã
æ¾å°ãYour user IDãï¼ä¸¦è¤è£½å°app.pyæªæ¡ä¸ï¼ç¬¬19è¡çä½ç½®ã
åæä¸æ¹çé¸é Messaging APIï¼æ¾å°ãChannel access tokenã並æä¸å¾æ¹çIssueï¼å³å¯å¾å°tokenï¼ä¸¦è¤è£½å°app.pyæªæ¡ä¸ï¼ç¬¬15è¡çä½ç½®ã
è«æ³¨æï¼è¥æå¿äººå£«æ¿å°éåtokenï¼ä»å°±å¯ä»¥ææ§æ¨çæ©å¨äººï¼å æ¤è«å¦¥åä¿ç®¡ã
ä¸æ¨£å¨é¸é Messaging APIä¸ï¼æ¾å°ãWebhook settingsãï¼å°æåå¨Herokuä¸ç³è«å¥½çå°æ¡ç¶²åè²¼ä¸ï¼å¾æ¹å¿ é è¦å å ¥ã/callbackãï¼å¦åæç¡æ³å·è¡ã
éåæ¥é©å¾ç°¡å®ï¼éåºååçwebhookå¾ï¼å°ä¸æ¹çuse webhookæéå³å¯ï¼åç¾ç¶ è²ï¼ãçºä½é麼簡å®çåä½ï¼æéæç¨ç«åºä¸åæ¥é©å¢ï¼å çºæ常æææåå¿è¨æä»æéï¼çè³ææåæææéäºï¼ç¶²é éæ°æ´çï¼å被ééäºï¼å æ¤ééå¿ é å¤æ¬¡ç¢ºèªã
éé¨åä¸ä¸å®è¦è¨å®ï¼ä¾ç §æ¨èªå·±çæèå»è¨å®ï¼ä¸éçè å人æ¯å¾è¨åLine幫æåè¨è¨çç½é å復ï¼æ以éåæä¸å®æéæçã
æ¨å¯ä»¥å å ¥èªå·±æ©å¨äººç好åäºï¼ä½æ©å¨äººéæ²æä»»ä½åè½ï¼æ以å¯ä»ä¸¦ä¸ææä»»ä½å復åï¼æè æ¯ç½é å復ï¼ã
å®æéåæ¥é©ï¼æ¨çæ©å¨äººå°±æ´»èµ·ä¾äºï¼å¾ ææå°ç·¨å¯«å¥½çç¨å¼ç¢¼ï¼å ä¸å³å°Githubä¸ï¼åå°GithubèHerokué£æ¥ï¼é²è¡é¨å±¬ã
è«åæå°åä½å辦好çGithubå°æ¡ï¼é»é¸ãUpload filesãï¼ä¸¦å°ååæªæ¡é½æé²å»ã
è«é¸æDeployment methodä¸çGithubï¼ä»¥åæ©å¨äººé½æ¯ä½¿ç¨ç¬¬ä¸åheroku Gitï¼ä»æ¯ä½¿ç¨çµç«¯æ©çæ¹å¼ãç¶åæé樣é¸ææ¯å çºï¼ç¶ægithubè¨ç½®ç§æå°æ¡æ¯è¦ä»è²»çï¼ä½ç¾å¨è®æå è²»çï¼é£ä¹æ建è°å管人ç¨åå½¢åä»é¢çGithub就好ï¼ä¸è¦ä¸ç´èçµç«¯æ©æ交éã
æå°æä¸æ¹ï¼æä¸Deploy Branchå³å¯ä½ç½²äºã
å¦ææ¨çæ©å¨äººæ主åå¯ä½ 說ï¼ãä½ å¯ä»¥éå§äºãï¼é£å°±ä»£è¡¨ãï¼.ä¸å¤§å¹³å°ä¸²æ¥ãçæ´åæ¥é©æ²æåé¡ãé裡就å¯ä»¥ç¼ç¾ï¼ç½é å復é常影é¿ä½¿ç¨è é«é©ï¼å æ¤å»ºè°åå°ã2â7:ç´°ç¯è¨å®ãçé¨åï¼å°å®éæã
作者:楊超霆 行銷搬進大程式 創辦人