【问题标题】:How to send Emoji with Telegram Bot API?如何使用 Telegram Bot API 发送表情符号?
【发布时间】:2015-10-04 12:06:42
【问题描述】:

我需要使用我的 Telegram Bot 发送包含表情符号的消息。

所以我复制/粘贴表情符号代码 :nine: 例如,在我的消息文本中并将其发送给用户,但表情符号不起作用。

这是我的示例代码和函数:

function tel_send($key, $t, $c)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/bot" . $key . "/sendMessage");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "cache=" . (time() / rand(1, time() - 100)) . "&text=" . $t . "&chat_id=" . $c);
    $ss = curl_exec($ch);
    curl_close($ch);
    return $ss;
}
tel_send($key, "My number - :nine:", $val['message']['chat']['id']);

所以,我的问题是:如何通过 Telegram 机器人发送表情符号?

【问题讨论】:

    标签: php telegram-bot


    【解决方案1】:

    重要的事情:

    没有人说。

    你必须使用双引号,而不是单引号。 下面的代码只会给你文本:\xF0\x9F\x98\x822021-11-21 09:54:25

    define('TELEGRAM_TOKEN', '9999999999:FFFFFFFFFFFFraxb6jajm8cDlKPOH-FFFFFFF');
    
    define('TELEGRAM_CHATID', '@my_bot');  
    
    message_to_telegram('\xF0\x9F\x98\x82' . date('Y-m-d H:i:s'));
    
    function message_to_telegram($text)
    {
        $ch = curl_init();
        curl_setopt_array(
            $ch,
            array(
                CURLOPT_URL => 'https://api.telegram.org/bot' . TELEGRAM_TOKEN . '/sendMessage',
                CURLOPT_POST => TRUE,
                CURLOPT_RETURNTRANSFER => TRUE,
                CURLOPT_TIMEOUT => 10,
                CURLOPT_POSTFIELDS => array(
                    'chat_id' => TELEGRAM_CHATID,
                    'text' => $text,
                ),
            )
        );
        curl_exec($ch);
    }
    

    但是用双引号你会得到表情符号:

    message_to_telegram("\xF0\x9F\x98\x82" . date('Y-m-d H:i:s'));
    

    【讨论】:

      【解决方案2】:

      你需要先json_decode unicode值,这样试试。

      json_decode('"\ud83d\ude0a"')
      

      【讨论】:

        【解决方案3】:

        请注意,至少使用 JavaScript/Node.js,您可以直接将表情符号粘贴到代码中,然后它们会将 find 转换为 Telegram API。例如。 ?

        【讨论】:

          【解决方案4】:

          > PHP 7

          您只需使用 \u 和 unicode 文字,如下所示:

          ?

          $emojie = "\u{1F600}";
          

          资源:https://stackoverflow.com/a/33548423/8760592

          【讨论】:

            【解决方案5】:

            您可以从电报中复制表情符号并粘贴到文本中以发送?

            $bot_url    = "https://api.telegram.org/bot$apiToken/";
            $url        = $bot_url . "sendPhoto?chat_id=@Your chanel user";
            
            $post_fields = array(
                'chat_id'   => $chat_id,
                'caption'   => 'Test caption ?',
                'photo'     => new CURLFile(realpath("logo.jpg"))
            );
            
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                "Content-Type:multipart/form-data"
            ));
            curl_setopt($ch, CURLOPT_URL, $url); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
            $output = curl_exec($ch);
            

            您可以使用 shap 的名称的另一种方式,例如对于 home,您可以使用 :house: 示例:

            :phone:
            :white_check_mark:
            :factory:
            :page_with_curl:
            

            【讨论】:

            • 一切巧妙都很简单)我花了很多时间才看到你的答案)这是唯一对我有用的解决方案
            • 一切巧妙都很简单)我花了很多时间才看到你的答案)这是唯一对我有用的解决方案
            【解决方案6】:

            最简单的方法是在代码中复制并粘贴您需要的表情符号。 但是您必须确保您的源代码文件以 UTF8 格式存储

            我刚刚在我的系统上打开了 Telegram 并复制了我需要的那些并且从未回头 :) 有趣的是我不必使用常量、特定名称 或 unicode 字符值。我可以直接看到在我的编辑器中使用了什么表情符号。

            $message = 'Pointing down: ?';
            

            如果你把它处理成一条消息(上面的例子是 PHP,但我想它可以在任何编程语言中工作)

            【讨论】:

              【解决方案7】:

              在此处查看表情符号及其 UTF-8 代码: http://apps.timwhitlock.info/emoji/tables/unicode

              然后您必须使用以下代码将 UTF-8 代码转换为 Telegram-ready 响应文本:

              <?php
              
              function telegram_emoji($utf8emoji) {
                  preg_replace_callback(
                      '@\\\x([0-9a-fA-F]{2})@x',
                      function ($captures) {
                          return chr(hexdec($captures[1]));
                      },
                      $utf8emoji
                  );
              
                  return $utf8emoji;
              }
              
              $utf8emoji = '\xF0\x9F\x98\x81';
              
              $telegramReadyResponse = 'Hi user ' . telegram_emoji($utf8emoji);
              

              【讨论】:

                【解决方案8】:

                只是另一种选择 $curl .... -F text="😁" -F parse_mode=html .... "https://api.telegram.org/botTOKEN/sendMessage"


                将启用 parse_mode 到 html 并使用 HTML 十六进制 (hex) 参考 (😁) 为 unicode U+1F601

                【讨论】:

                  【解决方案9】:

                  我在 linux bash 和 curl 命令中使用此代码来笑脸

                  curl -X POST "https://api.telegram.org/botTOKEN/sendMessage" -d "chat_id=ID&text=%F0%9F%98%80&parse_modwarninge=Markdown"
                  

                  【讨论】:

                    【解决方案10】:

                    我一直在寻找这个问题的答案很长时间,但无法让它发挥作用。我的脚本编写能力很差,将 php 答案转换为 bash 被证明是一项挑战。

                    但是,尽管如此,我还是使用了一个最简单的解决方案: 我去了电报桌面信使,在那里我发送了所需的表情符号(?)。

                    比我做了一个变量:bus="?"

                    现在我可以将 curl 中的变量用作:“text=some text $bus”

                    这在 linux 上使用 bash 效果很好,我想它也可以在 php 中使用。

                    【讨论】:

                      【解决方案11】:

                      真正的解决方案是将https://github.com/spatie/emoji (composer require spatie/emoji) 用于表情符号代码。现在你的代码看起来像

                      Emoji::CHARACTER_EYES
                      

                      Emoji::eyes()
                      

                      这是您真正可以使用的东西。不像手动编写所有代码,并且第一眼就很难理解它是什么。

                      【讨论】:

                        【解决方案12】:

                        对此答案的补充https://stackoverflow.com/a/31431810/1114926

                        Mustafa 提供的链接并不代表所有表情符号。这个来源更好http://emojipedia.org/☝️。除了主要标志外,它还有各种表情符号。

                        【讨论】:

                          【解决方案13】:

                          您需要指定表情符号的 unicode 值。

                          check here

                          这些由一个函数作为表情符号值返回,例如 u'\U000026C4',它是雪人。虽然是在python中,但是你可以将它应用到php中。

                          【讨论】:

                          • 在 PHP 中,记得在双引号字符串中使用字节码
                          • 链接中并非所有表情符号。例如。没有index finger up emogi,:point_up: 短代码。
                          • 你可以找到完整的表情符号列表here
                          【解决方案14】:

                          几天前我遇到了同样的问题.. 解决方案是使用此表中的字节 (UTF-8) 表示法: http://apps.timwhitlock.info/emoji/tables/unicode

                          例子:

                          ? \xF0\x9F\x98\x81 笑眯眯的脸

                          ? \xF0\x9F\x98\x89 眨眼的脸

                          【讨论】:

                          • 成功地将其与 PHP 机器人一起使用。 +1 提供了与该格式表情符号代码的如此出色的链接。
                          • 在 PHP 中,请记住在双引号字符串中使用它。
                          • 这是直接调用 API url 时让表情符号工作的唯一方法。
                          • 太棒了,应该是公认的答案。这是更新的链接:unicode.org/emoji/charts/full-emoji-list.html
                          猜你喜欢
                          • 2016-03-29
                          • 1970-01-01
                          • 2019-02-16
                          • 2015-11-24
                          • 2016-07-09
                          • 2015-09-20
                          • 2021-04-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多