PHP
Laravel
linebot

ゴリラと仲良くなる過程で Line Bot API を知った


概要

ゴリラってすごい


成果物

Screenshot_20181013-190841.png


ソース

        foreach ($events as $event) {

$messages = [];
$token = $event->getReplyToken();

switch (true) {
case $event instanceof MessageEvent:
$columns = [];
foreach (range(1, 5) as $id) {
$plusPost = new PostbackTemplateActionBuilder('+', self::PLUS . '?' . $id);
$minusPost = new PostbackTemplateActionBuilder('ー', self::MINUS . '?' . $id);
$columns[] = new CarouselColumnTemplateBuilder(
"ゴリラの $id くん",
'詳細',
'https://wired.jp/wp-content/uploads/2018/01/GettyImages-522585140.jpg',
[$plusPost, $minusPost]
);
}
$carousel = new CarouselTemplateBuilder($columns);
$messages[] = new TemplateMessageBuilder("ゴリラーズ", $carousel);
break;

case $event instanceof PostbackEvent:
[$type, $id] = explode('?', $event->getPostbackData());
$messages[] = new TextMessageBuilder(sprintf(
'ゴリラの %s くんを %s (気持ち)したよ。',
$id,
self::TYPE_STRING_MAP[$type]
));
break;

default:
continue;
}

$multiMessage = new MultiMessageBuilder;
foreach ($messages as $message) {
$multiMessage->add($message);
}

$bot->replyMessage($token, $multiMessage);
}

単一の要素のバブルの集まりがカルーセルみたいです

あとは、サンプルに沿って実装すれば色々できそうです :thumbsup: