Red8812 / hello-line-bot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hello Line Bot

Survey CheckList

  • 從零開始, 建立 Gradle 的 IntelljJ 專案, e.g., Building Java Projects with Gradle

  • 從零開始, 建立 Spring Boot 的 IntelljJ 專案, e.g., Building a RESTful Web Service

  • 從零開始, 建立簡易的 Line Bot, 只做訊息的回應 Ref., 並佈署到 Heroku Ref.

    Heroku 的 Settings > Config Variables 中需要有 LINE_BOT_CHANNEL_SECRET 與 LINE_BOT_CHANNEL_TOKEN 這兩個變數的設定

  • 使用 ngrok 將 Line Bot 的執行佈署到本機

    可使用 ./gradlew bootRun -Dline.bot.channelToken=YOUR_CHANNEL_TOKEN -Dline.bot.channelSecret=YOUR_CHANNEL_SECRET 執行程式

  • 測試 IntelljJ 的 Debug 模式是否可直接使用在 Line Bot 專案上? 可以

  • 測試 ngrok 提供的封包檢視功能

    目前只看到由外部接收到的 Request 與對應的 Response 封包, 並沒看到自己對 Line API Server 發出的封包

  • 查看執行 Line Bot 時, IntelliJ 的 Console 介面顯示的訊息有些什麼意義?

    有紀錄對 Line API 操作的 Request 與 Response 封包訊息

    Request 封包的訊息範例 : --> POST https://api.line.me/v2/bot/message/reply http/1.1 ...... --> END POST (285-byte body)

    Response 封包的訊息範例 : <-- 200 OK https://api.line.me/v2/bot/message/reply (80ms) ...... <-- END HTTP (2-byte body)

  • 測試 Group 裡是否也會 ECHO 其他使用者留的訊息? 會, 但並無法知道使用者的 ID

  • 測試 Line Bot 如何藉由 RESTful API (GET) 主動送訊息給使用者

  • 測試如何將 log4j 或其他 logging framework 導入 Spring Boot 的程式中

  • 測試 Line Bot 如何藉由 RESTful API (POST) 主動送訊息給使用者? 使用 Spring Boot 建立 RESTful Web Service 接收 訊息 後, 再用 Push message API 丟訊息即可

  • Bot 加入 Group 後, 拿到的 Source 會是 Group 類型嗎? YES

  • 重構 HelloWorld 的 Line Bot, 當被加入群組時, 會顯示群組 ID

  • 測試 Messaging API 的各項功能

    RequestMapping 的 Annotation 可同時支援 GET 與 POST, 若使用 postman 做測試時, 除了 Body 使用 form-data 外, Header 也要加上 X-Application-Context=application

  • 測試其他 Messaging API 並重構目前的 HelloWorld 程式

  • 測試 AWS 的自動佈署 (Boxfuse)

    在 Heroku 裡的 LINE_BOT_CHANNEL_TOKEN 與 LINE_BOT_CHANNEL_SECRET 兩個系統變數改設置在 src/main/resources/line-bot.properties 的設定檔中, 但 Boxfuse 的 EC2 佈署需要自行到 AWS 上設定 HTTPS 協定

Our Scenario

  1. Let a bot join a group
  2. Let the bot send any message to the group

How to Achieve that Scenario

  1. Webhooks

    • When an event is triggered, an HTTPS POST request is sent to the webhook URL.
    • An Event : a user adds your account or sends a message
    • The Webhook URL : e.g.,
      https://<YOUR_HEROKU_APP_NAME>.herokuapp.com/callback
      • A Sample Event:
    {
      "events": [
          {
            "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
            "type": "message",
            "timestamp": 1462629479859,
            "source": {
                 "type": "user",
                 "userId": "U206d25c2ea6bd87c17655609a1c37cb8"
             },
             "message": {
                 "id": "325708",
                 "type": "text",
                 "text": "Hello, world"
              }
          }
      ]
    }
    • A Join Event : your account joins a group or talk room.
      • It conains the group's ID source.groupId.
      • A Sample Event:
    {
      "replyToken": "nHuyWiB7yP5Zw52FIkcQobQuGDXCTA",
      "type": "join",
      "timestamp": 1462629479859,
      "source": {
        "type": "group",
        "groupId": "cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      }
    }
  2. Push message

    • Send messages to users, groups, and rooms at any time.
    • Sample Codes :
      • <to> : the ID returned via the webhook event of the source user, group, or room as the ID of the receiver
      • <textMessage> : can be any of send message objects
      TextMessage textMessage = new TextMessage("hello");
      PushMessage pushMessage = new PushMessage(
            "<to>",
            textMessage
      );
    
    
      Response<BotApiResponse> response =
            LineMessagingServiceBuilder
                    .create("<channel access token>")
                    .build()
                    .pushMessage(pushMessage)
                    .execute();
      System.out.println(response.code() + " " + response.message());

Line Official Sites

Online Tutorials

Code Examples

Other Techs

Heroku

Gradle

Spring Boot

ngrok

  • Official Site
  • Official Document
  • 使用方式:
    1. 若 Spring Boot 執行的 Web 程式可用 localhost:8080 開始時, 則在 cmder 執行 ngrok http 8080 後, 便會有 https://<A_RANDOM_NAME>.ngrok.io/ 外部網址可以使用
    2. 可使用此網址 http://localhost:4040/inspect/http 查看對上述網址 Request 的封包

lombok

Boxfuse

New Tech Terms

About


Languages

Language:Java 100.0%