iT邦幫忙

2021 iThome 鐵人賽

DAY 14
1
Software Development

Laravel 新手初見 API系列 第 14

Day14-Webhook 實作(三)LINEBot 之 MessageBuilder(I)

大家好~
今天來試著傳送不同類型訊息回覆使用者吧!

以下範例為以如何使用不同的 MessageBuilder 為主,
若對簽名驗證與一些例外處理有興趣,
歡迎參考 Day13 喔~

Text message

範例1:

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $reply = $request['events'][0]['message']['text'];
    $text = new TextMessageBuilder($reply);
    $bot->replyMessage($replyToken, $text);
}

範例2:

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TextMessageBuilder;
use LINE\LINEBot\MessageBuilder\Text\EmojiBuilder;
use LINE\LINEBot\MessageBuilder\Text\EmojiTextBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $reply = $request['events'][0]['message']['text'];
    $emoji1 = new EmojiBuilder(0, '5ac1de17040ab15980c9b438', '095');
    $emoji2 = new EmojiBuilder(1, '5ac1de17040ab15980c9b438', '095');
    $emojiText = new EmojiTextBuilder('$$' . $reply, $emoji1, $emoji2);
    $text = new TextMessageBuilder($emojiText);
    $bot->replyMessage($replyToken, $text);
}

一般文字回覆的使用方式十分容易,
只需將要回覆的 string 放入 TextMessageBuilder 實例中即可。
若想要用 Line Emoji 回覆,
則需要實例化較多個 Class,
還需要知道一些小重點:

  1. 最多可用20個 Line Emoji。
  2. Line Emoji 使用 $ 作為佔位符。
  3. 關於佔位符的 index 計算:
    • 第一個字元位置是以0開始計算。
    • 若 index 位置與 $ 不匹配,會回傳 HTTP 400 Bad request。
  4. 可用的 Line Emoji 可以參考官方文件喔。
  • EmojiBuilder
  • EmojiTextBuilder
    • 如果有多個 Line Emoji,就像範例一樣直接依序放入就行了。
      • $emojiText = new EmojiTextBuilder('$$' . $reply, $emoji1, $emoji2);
  • TextMessageBuilder
    • 如果是一般文字訊息:
      • 直接將 String 帶入就可以了。
    • 如果是包含 Line Emoji:
      • 將 EmojiTextBuilder 的實例帶入。

Sticker message

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\StickerMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $sticker = new StickerMessageBuilder('11537', '52002771');
    $bot->replyMessage($replyToken, $sticker);
}

Image message

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\ImageMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $image = new ImageMessageBuilder(
        'https://cdn-icons-png.flaticon.com/512/3522/3522266.png',
        'https://cdn-icons-png.flaticon.com/512/3522/3522248.png'
    );
    $bot->replyMessage($replyToken, $image);
}


上面範例刻意用兩張不一樣的圖片讓測試結果更好分辨:

  • 圖A為 Preview Image
    • Max file size: 1 MB
  • 圖B為 Original Content
    • Max file size: 10 MB
  • 圖片格式僅接受 JPEG or PNG

Video message

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\VideoMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $video = new VideoMessageBuilder(
        'https://img-9gag-fun.9cache.com/photo/aEpr2ON_460svvp9.mp4',
        'https://img-9gag-fun.9cache.com/photo/abVPMOL_460swp.png'
    );
    $bot->replyMessage($replyToken, $video);
}

  • Preview Image
    • Max file size: 1 MB
    • JPEG or PNG
  • Original Content
    • Max file size: 200 MB
    • mp4

Audio message

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\AudioMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $audio = new AudioMessageBuilder(
        'https://example.com/original.m4a',
        '75000'
    );
    $bot->replyMessage($replyToken, $audio);
}

  • Original Content
    • Max file size: 200 MB
    • 格式僅接受 m4a
  • Duration
    • Audio file 的時間長度
    • 單位為毫秒

Location message

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\LocationMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];
    $location = new LocationMessageBuilder(
        '好想工作室',
        '台南市東區北門路二段16號 L2A',
        '22.999739302983308',
        '120.21290532894172'
    );
    $bot->replyMessage($replyToken, $location);
}
電腦版畫面:

手機版畫面:

  • Title
    • 自定義的標題
    • Max character limit: 100
  • Address
    • 自定義的地址說明
    • Max character limit: 100
  • Latitude
    • 緯度
    • Decimal
  • Longitude
    • 經度
    • Decimal

Title 與 Address 輸入的值不會影響地圖圖釘的位置,
地圖圖釘的位置是以經緯度為主。
另外 Location message 在電腦與手機上呈現的結果會不一樣喔~

MultiMessageBuilder

use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\MultiMessageBuilder;
use LINE\LINEBot\MessageBuilder\VideoMessageBuilder;
use LINE\LINEBot\MessageBuilder\LocationMessageBuilder;

public function reply(Request $request)
{
    $httpClient = new CurlHTTPClient(env('LINE_BOT_CHANNEL_ACCESS_TOKEN'));
    $bot = new LINEBot($httpClient, ['channelSecret' => env('LINE_BOT_CHANNEL_SECRET')]);
    $replyToken = $request['events'][0]['replyToken'];

    $multiMessageBuilder = new MultiMessageBuilder();

    $video = new VideoMessageBuilder(
        'https://img-9gag-fun.9cache.com/photo/aEpr2ON_460svvp9.mp4',
        'https://img-9gag-fun.9cache.com/photo/abVPMOL_460swp.png'
    );
    $multiMessageBuilder->add($video);

    $location = new LocationMessageBuilder(
        '好想工作室',
        '台南市東區北門路二段16號 L2A',
        '22.999739302983308',
        '120.21290532894172'
    );
    $multiMessageBuilder->add($location);

    $response = $bot->replyMessage($replyToken, $multiMessageBuilder);
}

使用 MultiMessageBuilder 就可以不只回傳一筆訊息喔(最多五筆)。

今天就先這樣啦~
Imagemap message、Template message、Flex Message 就明天再來認識吧!
大家明天見!
若文章有任何問題,
還請大家不吝賜教!


上一篇
Day13-Webhook 實作(二)LINEBot 之 Echo bot
下一篇
Day15-Webhook 實作(四)LINEBot 之 MessageBuilder(II)
系列文
Laravel 新手初見 API30

尚未有邦友留言

立即登入留言