Line Robot ( 四 ) - Reply Message API (Include reply message object)

言:
    在上一章有提到如何回應使用者訊息,故本文就來探討Line回應訊息及主動發送訊息的功能。
    Line提供了各網路接口(API)提供開發者去使用其服務,其中有Reply Message API以及Push Message API與Send Multicast Message API。

Reply Message API
網址 https://api.line.me/v2/bot/message/reply
請求方法 POST

Request headers
Content-Type application/json
Authorization Bearer {channel access token}
channel access token在Line機器人設定中有Channel access token選項,按下右方按鈕即可產生channel access token。


Request body
屬性名稱 資料型別 註解
replyToken String Reply token received via webhook
messages Array of message objects Messages Max: 5

    Line官方提供了數個Message Object供使用者任意搭配使用,要注意的是每次傳送的Message Object數量最大為五個,接下來開始介紹基本的Message Object

Text message
Property Type Description
type String text
text String Message text. You can include the following emoji: Unicode emojiLINE original emoji
exmaple for text message object
{
    "type": "text",
    "text": "Hello, world"
}

Sticker message
Property Type Description
type String sticker
packageId String For information on package IDs, see the Sticker list
stickerId String For a list of sticker IDs for stickers that can be sent with the Messaging API
exmaple for sticker message object
{
  "type": "sticker",
  "packageId": "1",
  "stickerId": "1"
}
    目前的範例皆以這兩個Message object去介紹,詳細的請參考官方文件的-Message objects,為了測試API是否能正確使用,我們先以Postman進行API測試,如下圖所示

    實測的過程中,由於replyToken的有效時間極短,作者為了要邊截圖邊測試所以失敗了一次(所以在問話中打了兩次"你好"),但在成功後就不要注意這小細節,接著我們就來實作Java code。

首先作者做了一接口用來區分Mesaage Event內的訊息內容
public interface MessageListener{
 public void text(String replyToken, String text);
 public void image(String replyToken, String id);
 public void video(String replyToken, String id);
 public void audio(String replyToken, String id, long duration);
 public void file(String replyToken, String id, String fileName, long fileSize);
 public void location(String replyToken, String title, String address, double latitude, double longitude);
 public void sticker(String replyToken, String packageId, String stickerId);
}

接著在修改第三章的MessageHandler,其中作者額外寫了Message object的封裝物件以及做了Line Reply API的接口物件,相關資訊可以參考 https://github.com/JINHOUNGHUANG/lineRobot/tree/master/message-object
@Component
public class MessageHandler {
 
 @Value("${line.user.channel.token}")
 private String LINE_TOKEN;
 public void doAction(JSONObject event) {
  doAction(this.messageListener, event);
 }
 public void doAction(MessageListener messageListener, JSONObject event) {
  switch (event.getJSONObject("message").getString("type")) {
  case "text":
   messageListener.text(event.getString("replyToken"), event.getJSONObject("message").getString("text"));
   break;
  case "image":
   messageListener.image(event.getString("replyToken"), event.getJSONObject("message").getString("id"));
   break;
  case "video":
   messageListener.video(event.getString("replyToken"), event.getJSONObject("message").getString("id"));   
   break;
  case "audio":
   messageListener.audio(event.getString("replyToken"), event.getJSONObject("message").getString("id"), event.getJSONObject("message").getLong("duration"));
   break;
  case "file":
   messageListener.file(event.getString("replyToken"), event.getJSONObject("message").getString("id"), event.getJSONObject("message").getString("fileName"), event.getJSONObject("message").getLong("fileSize"));
   break;
  case "location":
   messageListener.location(event.getString("replyToken"), event.getJSONObject("message").getString("title"), event.getJSONObject("message").getString("address"), event.getJSONObject("message").getDouble("latitude"), event.getJSONObject("message").getDouble("longitude"));
   break;
  case "sticker":
   messageListener.sticker(event.getString("replyToken"), event.getJSONObject("message").getString("packageId"), event.getJSONObject("message").getString("stickerId"));
   break;
 }
 }
 
 private MessageListener messageListener = new MessageListener() {
  
  @Override
  public void video(String replyToken, String id) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "video");
  }
  
  @Override
  public void text(String replyToken, String text) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "text");
   LineConnector.getInstance().replyMessage(LINE_TOKEN, new ReplyMessage(replyToken, new BaseMessage[] {new TextMessage("看不懂~請在跟我說一次"), new StickerMessage("11538", "51626532")}));
  }
  
  @Override
  public void sticker(String replyToken, String packageId, String stickerId) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "sticker");
   LineConnector.getInstance().replyMessage(LINE_TOKEN, new ReplyMessage(replyToken, new BaseMessage[] {new TextMessage("看不懂~請在跟我說一次"), new StickerMessage("11538", "51626532")}));
  }
  
  @Override
  public void location(String replyToken, String title, String address, double latitude, double longitude) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "location");
  }
  
  @Override
  public void image(String replyToken, String id) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "image");
  }
  
  @Override
  public void file(String replyToken, String id, String fileName, long fileSize) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "file");
  }
  
  @Override
  public void audio(String replyToken, String id, long duration) {
   // TODO Auto-generated method stub
   System.out.printf("%s\t%s\n", replyToken, "file");
  }
 };
}
  作者在class中實作了MessageListener介面並在方法中解析JSON封包並依序排入介面內,然後實際運行後觀測其結果:


留言

這個網誌中的熱門文章

Python-相關係數矩陣實作(python-correlation matrix )

ASP.NET-後端將值傳給javascript

ASP.NET-FileUpload上傳後自動觸發button click(FileUpload upload auto trigger button click)