LoginSignup
19
19

More than 5 years have passed since last update.

【PHP】LINE Messaging API の絵文字(emoticon)送信方法

Posted at

以前、LINE Messaging APIでプッシュメッセージしていたときに絵文字(emoticon)も送りたいという話になり、公式ドキュメントを見たが、

以下の絵文字を含むテキストメッセージを送信できます。
 ・ Unicodeで定義された絵文字
 ・ LINEが独自に定義した絵文字。LINE独自の絵文字のUnicodeコードポイント表を参照してください。

という内容でいまいち送信のやり方がわからなかったので、送信方法を備忘録として記載しようと思います。

LINE Messaging APIで使用可能な絵文字(emoticon)

emoticon.png

引用:公式ドキュメント、LINE独自の絵文字のUnicodeコードポイント表

PHPでの送信方法

調べたところ、line-bot-sdk-phpに情報があったので、
試したところプッシュメッセージに絵文字(emoticon)を送信できた。

// 0xを抜いた数字の部分
$code = '100078';
// 16進エンコードされたバイナリ文字列をデコード
$bin = hex2bin(str_repeat('0', 8 - strlen($code)) . $code);
// UTF8へエンコード
$emoticon =  mb_convert_encoding($bin, 'UTF-8', 'UTF-32BE');

//配列などに格納して使う
$text[] =  array("type" => "text","text" => $emoticon);

プッシュメッセージ結果

※ 数字の部分をテキストで送り、絵文字を先程の処理で返信する仕組みを作ってみました。
 下記がその画像になります。

6621.png

返信処理のソース公開(参考程度)

文字だけをピックアップして返信するので、
スタンプとかは、既読スルーします。。。

reply.php
  $httpRequestBody = file_get_contents('php://input');
  $jsonObject = json_decode($httpRequestBody,true);
  $replyToken = $jsonObject["events"][0]["replyToken"];

  //X-Line-Signature Request Headerに入っているSignature検証で、LINE Platformから送信確認
  $hash = hash_hmac('sha256', $httpRequestBody, $this->ChannelSecret, true);
  $signature = base64_encode($hash);
  // 拡張ヘッダからAPI側から設定されているSignatureを取得
  $compSig = $_SERVER['HTTP_X_LINE_SIGNATURE'];

  // LINEからの送信であれば実行
  if ($signature == $compSig) {
    $topType = $jsonObject["events"][0]["type"];// follow,unfollow,message イベントタイプ
    $userId  = $jsonObject["events"][0]["source"]["userId"]; //LINEユーザID
    $groupId  = $jsonObject["events"][0]["source"]["groupId"]; //LINEユーザID

    //メッセージ受信の処理
    if($topType == "message"){
      $type = $jsonObject["events"][0]["message"]["type"];
      $text = $jsonObject["events"][0]["message"]["text"];

      //テキスト受信
      if($type =="text"){

        //絵文字を返信する処理
        $code = $text;
        $bin = hex2bin(str_repeat('0', 8 - strlen($code)) . $code);
        $emoticon =  mb_convert_encoding($bin, 'UTF-8', 'UTF-32BE');

        $textArr[] =  array("type" => "text","text" => $emoticon);
      //テキスト受信
      }
    }//メッセージ受信の処理

    //返信処理
    if(count($textArr) > 0){
      $this->ReplyMessage($replyToken,$textArr);
    }
  }// LINEからの送信であれば実行


/**
* 返信処理
*/
public function ReplyMessage($replyToken,$textArr){
  $ChannelAccessToken = チャンネルアクセストークン;

  $post["replyToken"] = $replyToken;
  $post["messages"] = $textArr;

  $url = "https://api.line.me/v2/bot/message/reply";
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_POST, true);
  curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($post));
  curl_setopt($ch,CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charser=UTF-8',
    'Authorization: Bearer ' . $ChannelAccessToken
  ));
  $res = curl_exec($ch);
  $obj = json_decode($res,true);
  curl_close($ch);
}

19
19
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
19
19