Try   HackMD

AI LINE Bot練功坊-L4 Echo Bot製作

1. 選擇運行環境

開啟並新增檔案

  • 用Visual Studio Code開啟專案資料夾

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 開啟後新增一個附檔名為.py的python檔案

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

選擇上次建立的虛擬環境

  • 點擊右下角切換環境

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 點擊輸入環境路徑

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 繼續點擊即可從檔案系統尋找資料夾

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 尋找上次建立環境的資料夾

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 點進資料夾後尋找Scripts的資料夾並點擊python的執行檔

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 右下角可檢查環境已被更改為上次建立的虛擬環境囉

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

MacOS操作
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 於資料中心-Finder中找到 使用者>{用戶名稱}>{環境名稱資料夾}>bin>python3.XX.X

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. 將其路徑複製下來

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. 於VSCode開啟專案資料夾

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  4. 點擊右下角python套件選擇環境

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  5. 點擊輸入解譯器路徑

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  6. 將複製的路徑貼上

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  7. 即完成環境匯入

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

2. 程式撰寫

  • 點擊進入Line Bot SDK PyPi的官方網站

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 往下滑動後找到官方文件給出的基本用法(Synopsis中Usage的部分)全選並複製

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    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檔案中

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 找到第23、24行,設定自己的Secret和Channel Access Token

    image

在程式中設定自己的Secret

  • 找到Line Dvelopers後台中Basic settings的頁籤

    image

  • 往下滑找到自己的Channel secret並複製

    image

  • 貼上到程式中取代原本的'YOUR_CHANNEL_SCRECT'

    image

在程式中設定自己的Channel Access Token

  • 找到Line Dvelopers後台中Messaging API的頁籤

    image

  • 往下滑找到自己的Channel Access Token並複製

    image

  • 貼上到程式中取代原本的'YOUR_CHANNEL_ACCESS_TOKEN'

    image

3. 啟動Web應用程式

利用Visual Studio Code的偵錯模式來啟動Flask的Web應用程式

  • 點擊左方執行與偵錯(Run and Debug)

    image

  • 建立一個執行的json檔案

    image

  • 選擇Python Debugger

    image

  • 選擇Flask

    image

  • 執行Python Debugger

    image

  • 啟動完成後可看到執行在本地端的伺服器5000port(若沒設定參數預設都為5000)

    image

  • 如果是Mac版本,5000port可能已被占用,此時可手動設定port參數(這裡設置為5001)

    image

  • 執行後可檢查port已變為自己設定的埠號(5001)

    image

4. 設定Webhook

  • 啟動ngrok的執行檔

    image

  • 輸入指令ngrok http 5000(自己設定的埠號)建立連線

    image

  • 複製其中Forwarding的網址

    image

  • 按下Webhook URL的Edit

    image

  • 貼上剛剛複製的網址,先不要按下Update

    image

  • 回到程式中,複製27行route中的/callback

    image

  • 貼上在剛剛的網址後

    image

  • 按下Update

    image

  • 啟用Use webhook

    image

  • 可以按下Verify驗證是否有連接成功

    image

  • 跳出以下視窗後代表成功與本地端連線了!

    image

5. Echo Bot測試

  • 打開Line找到自己的機器人就可以測試是否成功啦!
    image

Reference:https://developers.line.biz/en/docs/messaging-api/building-bot/

Youtube 課程影片

關於我們

image 國立臺北教育大學 教育大數據微學程

先修微課程

🤖 AI LineBot 練功坊系列課程
從入門到精通,學習如何開發並應用 LINE Bot,讓你輕鬆掌握最前沿的聊天機器人技術。

👨‍💻 Python 初學小教室
針對零基礎學員設計,循序漸進地教授 Python 基本語法及實作技巧,幫助你快速上手。

📊 統計學小教室
系統講解統計學理論及其應用,適合想要提升數據分析能力的學習者。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Facebook
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Instagram
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Threads
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
YouTube
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →
Line官方帳號

相關教材連結

◀◀◀ L03 開發環境建置 ◀◀◀
▶▶▶ L05 Webhook Event Type ▶▶▶