LINE のチャットボットで、テンプレートメッセージという、Botから選択できるUIがある。
こういうやつ
これを Node.js の SDK を使ってどうやればよいか、調べるのに時間かかったのでメモ。
LINEが公式で出している、 API Reference には、なかったので、ソースコードを読んだ。といっても TypeScript の型定義で事が足りた。
公式リファレンス https://line.github.io/line-bot-sdk-nodejs/pages/api-reference/client.html
ソースコード https://github.com/line/line-bot-sdk-nodejs/blob/b884dbaf0062429085a5ee6b830f526502ffab42/lib/types.ts#L383-L431
import { Client } from '@line/bot-sdk'
const message = {
type: 'template',
altText: 'エラーが発生しました。',
template: {
text,
type: 'confirm',
actions: [
{
type: 'message',
label: 'はい',
text: 'はい',
},
{
type: 'message',
label: 'いいえ',
text: 'いいえ',
},
],
},
}
const client = new Client({
channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
})
client.pushMessage('user_id', message)