LINE BOT – Message 的寫法

整理一下這次用到的 message 種類,這樣寫可以跑…

<?php
require_once __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use LINE\Clients\MessagingApi\Configuration;
use LINE\Clients\MessagingApi\Api\MessagingApiApi;
use LINE\Clients\MessagingApi\Model\ReplyMessageRequest;
use LINE\Clients\MessagingApi\Model\TextMessage;
use LINE\Clients\MessagingApi\Model\ImageMessage;
use LINE\Clients\MessagingApi\Model\TemplateMessage;
use LINE\Clients\MessagingApi\Model\ButtonsTemplate;
use LINE\Clients\MessagingApi\Model\PostbackAction;
use LINE\Clients\MessagingApi\Model\URIAction;
use LINE\Clients\MessagingApi\Model\MessageAction;
use LINE\Clients\MessagingApi\Model\CarouselTemplate;
use LINE\Clients\MessagingApi\Model\CarouselColumn;


TextMessage //最簡單的文字訊息

 $message[] = new TextMessage([
        'type' => 'text',
        'text' => $msg
    ]);


ImageMessage //用這個可以傳送圖片

$message[] = new ImageMessage([
        'type' => 'image',
        'originalContentUrl' => $imageUrl,
        'previewImageUrl' => $imageUrl,
    ]);


URIAction //點擊可以打開內建瀏覽器的網頁連結(按鈕)

$url = "https://blog.quantoyo.com/ok.php";
    $action = new URIAction([
        'type' => 'uri',
        'label' => '點擊打開連結',
        'uri' => $url
    ]);

    $message[] = new TemplateMessage([
        'type' => 'template',
        'altText' => '跳通知用的文字',
        'template' => new ButtonsTemplate([
            'type' => 'buttons',
            'title' => '標題文字',
            'text' => '請點擊',
            'actions' => [$action] //可以放好幾個action
        ])
    ]);

MessageAction //點擊可以幫使用者傳送文字的按鈕

    $action = new MessageAction([
        'type' => 'message',
        'label' => '點擊我發送ABC',
        'text' => 'ABC' // 這是會發送的文字
    ]);

    $message[] = new TemplateMessage([
        'type' => 'template',
        'altText' => '這是通知用的文字',
        'template' => new ButtonsTemplate([
            'type' => 'buttons',
            'title' => '這是標題',
            'text' => '這是顯示的內文',
            'actions' => [$action]
        ])
    ]);

PostbackAction // 點擊後,機器人會收到訊息,但是不會讓使用者寫出來。
$tmpPostback = new PostbackAction([
     'type' => 'postback',
     'label' => '顯示的字',
     'data' => '要回傳的字串'
 ]);
 $msgAry[] = $tmpPostback;

$message[] = new TemplateMessage([
      'type' => 'template',
      'altText' => '跳出通知的文字', //40個字以內
      'template' => new ButtonsTemplate([
      'type' => 'buttons',
      'title' => '這是標題',
      'text' => '這是內文',
      'actions' => $msgAry
   ])
]); 

CarouselColumn //在畫面上做一個很多頁的左右滑動的圖文選單

$columnAry = array();
for($i = 0;$i < count($plans); $i++){
    $column = new CarouselColumn();
    $column->setThumbnailImageUrl("圖片網址.png");
    $column->setImageBackgroundColor("#F0F0F0");
    $column->setTitle('第'.$i.'個的標題');
    $column->setText('內文');
    $column->setActions([
        new PostbackAction([
            'type' => 'postback',
            'label' => "按鈕上面的文字",
            'data' => '第'.$i.'個按鈕的第一個選項要回傳的字'
        ])
        ,
        new PostbackAction([
            'type' => 'postback',
            'label' => "按鈕上面的文字",
            'data' => '第'.$i.'個按鈕的第二個選項要回傳的字'
        ])
    ]);
    $columnAry[] = $column;
    if(count($columnAry) == 10){        //Carousel 最多10個
        break;
    }
}
// 建立 Carousel Template
$carousel = new CarouselTemplate();
$carousel->settype('carousel');
$carousel->setColumns($columnAry);

$message[] = new TemplateMessage([
    'type' => 'template',
    'altText' => '通知用的字串',
    'template' => $carousel
]);

發佈留言