Template messageでリッチな操作体験を提供
ボタンや画像の配置などが定義されたデザインテンプレートを利用して,
Buttonsタイプで画像とボタンを組み合わせたUIを作る
図2のButtonsタイプでは,alt_
はTemplate message非対応端末で表示されるテキストを設定します。add_
メソッドはユーザーがボタンを押したときにtext
の内容を自動的にチャットメッセージとしてユーザーが発言するボタンを表示します。add_
メソッドはurl
で指定したURLをブラウザで開くボタンを表示します。なお,$bot->reply_
などで直接送信ができないので,
リスト6 Buttonsタイプのメッセージ構築
my $buttons = LINE::Bot::API::Builder::TemplateMessage
->new_buttons(
alt_text => 'this is a buttons template', ―(1)
image_url => 'https://example.com/bot/image.jpg',
title => 'buttons',
text => 'description',
)->add_message_action( ―(2)
label => 'message',
text => 'message',
)->add_uri_action( ―(3)
label => 'uri',
uri => 'http://example.com/',
);
my $messages = LINE::Bot::API::Builder::SendMessage
->new() ―(4)
->add_template($buttons->build);
$bot->reply_message($reply_token, $messages->build);
Carouselタイプを使って複数個のテンプレートを横に並べる
図3のCarouselタイプは,new_
メソッドでCarouselのビルダオブジェクトを作成し,add_
メソッドで登録していきます。
リスト7 Carouselタイプのメッセージ構築
my $carousel = LINE::Bot::API::Builder::TemplateMessage
->new_carousel( ―(1)
alt_text => 'this is a carousel template',
);
for my $i (1..5) {
my $col = LINE::Bot::API::Builder::TemplateMessage::Column
->new(
image_url => 'https://example.com/bot/item.jpg',
title => "carousel $i",
text => "description $i",
)->add_message_action(
label => 'message',
text => 'message',
);
$carousel->add_column($col->build); ―(2)
}