用Visual Studio Code開啟專案資料夾
Learn More →
開啟後新增一個附檔名為.py的python檔案
Learn More →
Learn More →
點擊右下角切換環境
Learn More →
點擊輸入環境路徑
Learn More →
繼續點擊即可從檔案系統尋找資料夾
Learn More →
尋找上次建立環境的資料夾
Learn More →
Learn More →
點進資料夾後尋找Scripts的資料夾並點擊python的執行檔
Learn More →
Learn More →
右下角可檢查環境已被更改為上次建立的虛擬環境囉
Learn More →
於資料中心-Finder中找到 使用者>{用戶名稱}>{環境名稱資料夾}>bin>python3.XX.X
Learn More →
將其路徑複製下來
Learn More →
於VSCode開啟專案資料夾
Learn More →
點擊右下角python套件選擇環境
Learn More →
點擊輸入解譯器路徑
Learn More →
將複製的路徑貼上
Learn More →
即完成環境匯入
Learn More →
點擊進入Line Bot SDK PyPi的官方網站
Learn More →
往下滑動後找到官方文件給出的基本用法(Synopsis中Usage的部分)全選並複製
Learn More →
from flask import Flask, request, abort
from linebot.v3 import (
WebhookHandler
)
from linebot.v3.exceptions import (
InvalidSignatureError
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
from linebot.v3.webhooks import (
MessageEvent,
TextMessageContent
)
app = Flask(__name__)
configuration = Configuration(access_token='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:
app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)
if __name__ == "__main__":
app.run()
貼上在剛剛建立好的python檔案中
Learn More →
找到第23、24行,設定自己的Secret和Channel Access Token
找到Line Dvelopers後台中Basic settings的頁籤
往下滑找到自己的Channel secret並複製
貼上到程式中取代原本的'YOUR_CHANNEL_SCRECT'
找到Line Dvelopers後台中Messaging API的頁籤
往下滑找到自己的Channel Access Token並複製
貼上到程式中取代原本的'YOUR_CHANNEL_ACCESS_TOKEN'
點擊左方執行與偵錯(Run and Debug)
建立一個執行的json檔案
選擇Python Debugger
選擇Flask
執行Python Debugger
啟動完成後可看到執行在本地端的伺服器5000port(若沒設定參數預設都為5000)
如果是Mac版本,5000port可能已被占用,此時可手動設定port參數(這裡設置為5001)
執行後可檢查port已變為自己設定的埠號(5001)
啟動ngrok的執行檔
輸入指令ngrok http 5000(自己設定的埠號)建立連線
複製其中Forwarding的網址
按下Webhook URL的Edit
貼上剛剛複製的網址,先不要按下Update
回到程式中,複製27行route中的/callback
貼上在剛剛的網址後
按下Update
啟用Use webhook
可以按下Verify驗證是否有連接成功
跳出以下視窗後代表成功與本地端連線了!
Reference:https://developers.line.biz/en/docs/messaging-api/building-bot/
國立臺北教育大學 教育大數據微學程
🤖 AI LineBot 練功坊系列課程
從入門到精通,學習如何開發並應用 LINE Bot,讓你輕鬆掌握最前沿的聊天機器人技術。
👨💻 Python 初學小教室
針對零基礎學員設計,循序漸進地教授 Python 基本語法及實作技巧,幫助你快速上手。
📊 統計學小教室
系統講解統計學理論及其應用,適合想要提升數據分析能力的學習者。
AI LINE Bot練功坊-L3 開發環境建置
Jan 10, 2025AI LINE Bot練功坊-L16 Azure AI Language 交談語言理解機器人
Dec 25, 2024AI LINE Bot練功坊-L2 建立LINE官方帳號
Dec 21, 2024AI LINE Bot練功坊-L15 Azure Translator 翻譯機器人
Jul 26, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up