LoginSignup
7
2

More than 3 years have passed since last update.

Node.jsでLINE BOTの画像送信を試す

Last updated at Posted at 2019-07-22

画像を送るカンタンなサンプルです。

最近デモで使おうと思うケースが多いけどスニペッドが見つからないことがあるのでメモがてら

ちなみにこの記事は、
LINE BOT -> ユーザーへ画像送信です。
ユーザー -> LINE BOTに画像送信をするのはこちらの記事などを参考にして下さい。

LINE BOTからNode.jsで画像を受け取って保存する

公式ドキュメント

この辺のメッセージオブジェクトの仕様を確認しましょう。

いつもの記事を元にサンプルを書く

1時間でLINE BOTを作るハンズオン

いつもの記事です。
初めての人はまずこちらから。

画像リプライメッセージ

handleEvent関数をまるっと差し替えましょう。
画像は https://protoout.studio のサイトキャプチャです(笑)

server.js
省略

function handleEvent(event) {
  if (event.type !== 'message' || event.message.type !== 'text') {
    return Promise.resolve(null);
  }

  return client.replyMessage(event.replyToken,{
      type: 'image',
      originalContentUrl: 'https://i.gyazo.com/e772c3b48a07716226f7184d7f417cda.png',
      previewImageUrl: 'https://i.gyazo.com/e772c3b48a07716226f7184d7f417cda.png'
   });

}

省略

画像プッシュメッセージ

リプライのように使ってますが、Pushです。
こちらもhandleEvent関数をまるっと差し替えです。

server.js
省略

function handleEvent(event) {
    if (event.type !== 'message' || event.message.type !== 'text') {
        return Promise.resolve(null);
    }

    const userId = `xxxxxxxxxxxxxxxxxxxxxxxx`;

    return client.pushMessage(userId,{
        type: 'image',
        originalContentUrl: 'https://i.gyazo.com/e772c3b48a07716226f7184d7f417cda.png',
        previewImageUrl: 'https://i.gyazo.com/e772c3b48a07716226f7184d7f417cda.png'
    });
}

省略

利用イメージ

何送ってもキャプチャ画像を送ってくれます。

こういうときGyazoは便利ですね。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2