大家好~
今天來實作 Imagemap message 與 Template message 吧!
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\ImagemapMessageBuilder;
use LINE\LINEBot\MessageBuilder\Imagemap\BaseSizeBuilder;
use LINE\LINEBot\ImagemapActionBuilder\AreaBuilder;
use LINE\LINEBot\ImagemapActionBuilder\ImagemapMessageActionBuilder;
use LINE\LINEBot\ImagemapActionBuilder\ImagemapUriActionBuilder;
use LINE\LINEBot\MessageBuilder\Imagemap\VideoBuilder;
use LINE\LINEBot\MessageBuilder\Imagemap\ExternalLinkBuilder;
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'];
$videoArea = new AreaBuilder(0, 0, 1040, 780);
$externalLinkBuilder = new ExternalLinkBuilder(
'https://img-9gag-fun.9cache.com/photo/aQowgv2_460svav1.mp4',
'Label!'
);
$videoBuilder = new VideoBuilder(
'https://img-9gag-fun.9cache.com/photo/aQowgv2_460svav1.mp4',
'https://img-9gag-fun.9cache.com/photo/aP3Nxwg_700bwp.png',
$videoArea,
$externalLinkBuilder
);
$messageActionArea = new AreaBuilder(520, 780, 520, 260);
$imagemapMessageActionBuilder = new ImagemapMessageActionBuilder(
'imagemapMessageActionBuilder1',
$messageActionArea
);
$uriActionArea = new AreaBuilder(0, 780, 520, 260);
$imagemapUriActionBuilder = new ImagemapUriActionBuilder(
'https://ithelp.ithome.com.tw/',
$uriActionArea
);
$baseSizeBuilder = new BaseSizeBuilder(1040,1040);
$imagemap = new ImagemapMessageBuilder(
'https://6cf8-2001-b400-e7ac-2d2e-4c5c-37fc-485a-fd5a.ngrok.io/storage/Imagemap',
'This is an imagemap',
$baseSizeBuilder,
[
$imagemapMessageActionBuilder,
$imagemapUriActionBuilder
],
null,
$videoBuilder
);
$bot->replyMessage($replyToken, $imagemap);
}
背板圖片有點跑版了QQ
不過 Imagemap 有的功能上面的範例基本上都有實作到囉~
Template message 有提供四種模板:
七種 Action:
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ButtonTemplateBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraRollTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\CameraTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\DatetimePickerTemplateActionBuilder;
use LINE\LINEBot\TemplateActionBuilder\LocationTemplateActionBuilder;
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'];
$cameraRollTemplateActionBuilder = new CameraRollTemplateActionBuilder('CameraRoll');
$cameraTemplateActionBuilder = new CameraTemplateActionBuilder('CameraTemplate');
$datetimePickerTemplateActionBuilder = new DatetimePickerTemplateActionBuilder(
'DatetimePicker',
'storeId=12345',
'datetime',
Carbon::now()->format('Y-m-d\th:i'),
'2099-12-31t00:00',
'1900-01-01t00:00'
);
$locationTemplateActionBuilder = new LocationTemplateActionBuilder('LocationTemplate');
$buttonTemplateBuilder = new ButtonTemplateBuilder(
'Test Title',
'Test Text',
'https://cdn-icons-png.flaticon.com/512/1581/1581884.png',
[
$cameraRollTemplateActionBuilder,
$cameraTemplateActionBuilder,
$datetimePickerTemplateActionBuilder,
$locationTemplateActionBuilder
]
);
$templateMessageBuilder = new TemplateMessageBuilder(
'This is a buttons template',
$buttonTemplateBuilder
);
$bot->replyMessage($replyToken, $templateMessageBuilder);
}
Buttons template 最多可以使用四種 TemplateActionBuilder,
這個範例使用了以下四種:
位置資訊
功能。使用者傳送後我們收到的 Request 如下圖:use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\ConfirmTemplateBuilder;
use LINE\LINEBot\TemplateActionBuilder\MessageTemplateActionBuilder;
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'];
$messageTemplateActionBuilder1 = new MessageTemplateActionBuilder(
'YES',
'Say Yes!'
);
$messageTemplateActionBuilder2 = new MessageTemplateActionBuilder(
'NO',
'Say No!'
);
$confirmTemplateBuilder = new ConfirmTemplateBuilder(
'YES or NO',
[
$messageTemplateActionBuilder1,
$messageTemplateActionBuilder2
]
);
$templateMessageBuilder = new TemplateMessageBuilder(
'This is a buttons template',
$confirmTemplateBuilder
);
$bot->replyMessage($replyToken, $templateMessageBuilder);
}
Confirm template 會傳送一個固定有兩個按鈕的模板,
然後這邊我們實作的是 Message action,
所以使用者點擊按鈕後會回傳一段文字訊息給我喔~
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselTemplateBuilder;
use LINE\LINEBot\MessageBuilder\TemplateBuilder\CarouselColumnTemplateBuilder;
use LINE\LINEBot\TemplateActionBuilder\PostbackTemplateActionBuilder;
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'];
$postbackTemplateActionBuilder1 = new PostbackTemplateActionBuilder(
'Postback 1',
'storeId=12345',
'Postback 1'
);
$carouselColumnTemplateBuilder1 = new CarouselColumnTemplateBuilder(
'Title 1',
'CarouselColumnTemplateBuilder 1',
'https://cdn-icons-png.flaticon.com/512/3522/3522248.png',
[
$postbackTemplateActionBuilder1
]
);
$postbackTemplateActionBuilder2 = new PostbackTemplateActionBuilder(
'Postback 2',
'storeId=67890',
'Postback 2'
);
$carouselColumnTemplateBuilder2 = new CarouselColumnTemplateBuilder(
'Title 2',
'CarouselColumnTemplateBuilder 2',
'https://cdn-icons-png.flaticon.com/512/3522/3522266.png',
[
$postbackTemplateActionBuilder2
]
);
$carouselTemplateBuilder = new CarouselTemplateBuilder([
$carouselColumnTemplateBuilder1,
$carouselColumnTemplateBuilder2
]);
$templateMessageBuilder = new TemplateMessageBuilder(
'This is a buttons template',
$carouselTemplateBuilder
);
$bot->replyMessage($replyToken, $templateMessageBuilder);
}
Carousel template
每個 columns 最多可以有三個 Action。
這邊範例我們使用的是 Postback action,
使用者觸發後,
回傳的訊息屬於 Postback event,
如下圖:
use LINE\LINEBot\HTTPClient\CurlHTTPClient;
use LINE\LINEBot;
use LINE\LINEBot\MessageBuilder\TemplateMessageBuilder;
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'];
$uriTemplateActionBuilder1 = new UriTemplateActionBuilder(
'Label 1',
'https://ithelp.ithome.com.tw/'
);
$imageCarouselColumnTemplateBuilder1 = new ImageCarouselColumnTemplateBuilder(
'https://cdn-icons-png.flaticon.com/512/3522/3522248.png',
$uriTemplateActionBuilder1
);
$uriTemplateActionBuilder2 = new UriTemplateActionBuilder(
'Label 2',
'https://developers.line.biz/en/'
);
$imageCarouselColumnTemplateBuilder2 = new ImageCarouselColumnTemplateBuilder(
'https://cdn-icons-png.flaticon.com/512/3522/3522266.png',
$uriTemplateActionBuilder2
);
$imageCarouselTemplateBuilder = new ImageCarouselTemplateBuilder([
$imageCarouselColumnTemplateBuilder1,
$imageCarouselColumnTemplateBuilder2
]);
$templateMessageBuilder = new TemplateMessageBuilder(
'This is a buttons template',
$imageCarouselTemplateBuilder
);
$bot->replyMessage($replyToken, $templateMessageBuilder);
}
Image carousel template
每個 columns 只能有一個 Action。
這邊範例我們使用的是 URI action,
使用者觸發後,
會用 Line 預設的瀏覽器開啟指定的 URI。
今天就這樣啦~
還剩下一種 Flex Message 還沒實作到,
明天再繼續吧~
大家明天見!
若文章有任何問題,
還請大家不吝賜教!