大家好~
今天來試著傳送不同類型訊息回覆使用者吧!
以下範例為以如何使用不同的 MessageBuilder 為主,
若對簽名驗證與一些例外處理有興趣,
歡迎參考 Day13 喔~
範例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,
還需要知道一些小重點:
$
作為佔位符。$
不匹配,會回傳 HTTP 400 Bad request。$emojiText = new EmojiTextBuilder('$$' . $reply, $emoji1, $emoji2)
;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);
}
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);
}
上面範例刻意用兩張不一樣的圖片讓測試結果更好分辨:
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);
}
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);
}
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 與 Address 輸入的值不會影響地圖圖釘的位置,
地圖圖釘的位置是以經緯度為主。
另外 Location message 在電腦與手機上呈現的結果會不一樣喔~
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 就明天再來認識吧!
大家明天見!
若文章有任何問題,
還請大家不吝賜教!