6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MessagingAPI LINE絵文字を使ってみよう!

Posted at

はじめに

MessagingAPIでLINEbotを作る過程でLINE絵文字の使い方について記録します。

実装

const TOKEN = "自分のアクセストークン";
const LINE_PUSH_ENDPOINT = "https://api.line.me/v2/bot/message/push";

const HEADERS = {
  "Content-Type": "application/json; chrset=UTF-8",
  "Authorization": "Bearer " + TOKEN //Bearerの後ろの半角スペースを忘れずに!
}

//pushメッセージ用処理
function postMessage() {
  const postData = {
    "to": "自分のuserId",
    "messages": [{
      "type": "text",
      "text": "送信するメッセージ $", //$の部分に絵文字が適用される
      "emojis": [
        {
          "index": "10", //textの先頭からプレースホルダーのindex 今回の場合10
          "productId": "5ac21e6c040ab15980c9b444", //絵文字のproductId
          "emojiId": "002" //絵文字のId
        }
      ]
    }]
  }

  const params = {
    "methods": "POST",
    "headers": HEADERS,
    "payload": JSON.stringify(postData)
  };

  UrlFetchApp.fetch(LINE_PUSH_ENDPOINT, params);
}

emojis.productIdは、のプロダクトIDを、emojis.emojiIdは絵文字下にある番号を記載します。

スクリーンショット 2022-02-27 15.43.47.png
こちらから使用可能な絵文字を確認できます。

emojis.indexは、textプロパティに記載したプレースホルダー$のindex番号を記載します。テキストの先頭を0とするため、今回の例でいうと10となります。
※プレースホルダーとindexの数値が一致しない場合404 Bad requestとなるようです。

複数の絵文字を使いたい場合は、使用する数だけプレースホルダーと絵文字用のオブジェクトを用意すればできます(最大個数:20個)。

使ってみるとこんな感じになります。
絵文字使用例.jpg

まとめ

今回はMessagingAPIでLINE絵文字の使い方についてまとめました。
文字だけでもいいですが、絵文字があると雰囲気も変わるのでぜひ使っていきたいと思います。

参考

6
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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?