Message typeを使いこなし表現力豊かなbotにする
Echo botではTextタイプを利用したサンプルコードを紹介しましたが、
メディアファイルの送受信
botから画像や動画などのメディアファイルを送信するためには、
リスト2はアップロードしたメディアファイルをMessaging APIでユーザーに送信するサンプルです。preview_は動画中から生成したサムネイル画像以外も指定できますが、
my $messages = LINE::Bot::API::Builder::SendMessage->new(
)->add_image(
  image_url => 'https://example.com/file.jpg',
  preview_url => 'https://example.com/thumbnail.jpg',    ―(1)
)->add_audio(
  audio_url => 'https://example.com/file.m4a',
  duration => 361_000,    ―(2)
)->add_image(
  video_url => 'https://example.com/video.mp4',
  preview_url => 'https://example.com/thumbnail.jpg',    ―(3)
);LINEサーバからダウンロードする
ユーザーから画像、
リスト3は、get_メソッドを利用してLINEサーバからダウンロードします。
if ($event->is_image_message || $event->is_audio_message
      || $event->is_video_message) {
  my $ret = $bot->get_message_content(
                    $event->message_id);    ―(1)
  if ($ret->is_success) {
    my $filename = $ret->fh->filename;
    open my $fh, '<', $file or die "$!: $file";    ―(2)
    ... # ファイル処理
  }
}スタンプの送受信
リスト4はスタンプの送受信を行うコードです。ただし、package_とsticker_は非公開となっているため、
# 受信時    ―(1)
if ($event->is_sticker_message) {
  say $event->package_id;
  say $event->sticker_id;
}
# 送信時
my $messages = LINE::Bot::API::Builder::SendMessage->new(
)->add_sticker(
  package_id => '1',
  sticker_id => '2',
);位置情報の送受信
リスト5は緯度経度情報の送受信のコードです。たとえば
# 受信時
if ($event->is_location_message) {
  say $event->title;
  say $event->address;
  say $event->latitude;
  say $event->longitude;
}
# 送信時    ―(1)
my $messages = LINE::Bot::API::Builder::SendMessage->new(
)->add_location(
  title => 'LINE Corporation.',
  address => 'Hikarie Shibuya-ku Tokyo 151-0002',
  latitude => 35.6591,
  longitude => 139.7040,
); 
Template messageでリッチな操作体験を提供
ボタンや画像の配置などが定義されたデザインテンプレートを利用して、
Buttonsタイプで画像とボタンを組み合わせたUIを作る
図2のButtonsタイプでは、alt_はTemplate message非対応端末で表示されるテキストを設定します。add_メソッドはユーザーがボタンを押したときにtextの内容を自動的にチャットメッセージとしてユーザーが発言するボタンを表示します。add_メソッドはurlで指定したURLをブラウザで開くボタンを表示します。なお、$bot->reply_などで直接送信ができないので、
 
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_メソッドで登録していきます。
 
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)
}Confirmタイプで選択式の確認ダイアログを提供する
図4のConfirmタイプは、
 
my $confirm = LINE::Bot::API::Builder::TemplateMessage
->new_confirm(
  alt_text => 'this is a confirm template',
  text => 'confirm',
)->add_postback_action(    ―(1)
  label => 'postback',
  data => 'postback data',
)->add_message_action(
  label => 'message',
  text => 'message',
);Postbackを使ってチャットのログを汚さずにbotにメッセージを送る
リスト8では、add_メソッドを使いました。これはadd_と似た機能を提供するのですが、add_とは違いチャットメッセージに発言のログを残さずにWebhook URLにイベントを通知できるボタンを表示します。このボタンをユーザーが押したときに、
if ($event->is_postback_event) {
  say $event->postback_data;
}そのほかのお勧め機能
本節では、
LINE Beaconによる現実世界との連携
Messaging APIには、
LINE Simple Beacon Specを実装することにより、
Social REST APIを使ったWebサイト連携
Social REST APIを利用することで、
 
LINE Notifyで簡単な通知の送信
LINE Notifyとは、
Furl->new->post(
    'https://notify-api.line.me/api/notify',
[ 'Authorization' => "Bearer $TOKEN" ],
[ 'message' => 'こんにちは!' ]
);すべてのLINE連携をMessaging APIで作ろうとすると開発コストが増大するので、
まとめ
本稿では、
さて、
本誌最新号をチェック!
WEB+DB PRESS Vol.130
2022年8月24日発売
B5判/
定価1,628円
ISBN978-4-297-13000-8
- 特集1
 イミュータブルデータモデルで始める
 実践データモデリング
 業務の複雑さをシンプルに表現!
- 特集2
 いまはじめるFlutter
 iOS/Android両対応アプリを開発してみよう 
- 特集3
 作って学ぶWeb3
 ブロックチェーン、スマートコントラクト、 NFT 


