如何使用Telegram Bot API发送表情符号?

Vlm*_*ake 26 php telegram-bot

我需要用我的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']);
Run Code Online (Sandbox Code Playgroud)

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

cod*_*rer 34

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

例子:

\xF0\x9F\x98\x81用微笑眼睛磨牙

\xF0\x9F\x98\x89 WINKING FACE

  • 在PHP中,请记住在双引号字符串中使用它. (5认同)

Mus*_*afa 13

你需要指定表情符号的unicode值.

检查一下

这些是作为表情符号值返回的函数,如u'\ U000026C4',这是雪人.虽然它是在python中,你可以将其应用于php.

  • 在 PHP 中,记住在双引号字符串内使用字节码 (2认同)

Mil*_*lad 8

您可以从utf8字节创建它.

请在此处查看表情符号列表及其utf8代码:http: //apps.timwhitlock.info/emoji/tables/unicode

使用以下代码将utf8代码转换为电报就绪响应文本:

<?php

$EmojiUtf8Byte = '\xF0\x9F\x98\x81';

$pattern = '@\\\x([0-9a-fA-F]{2})@x';
$emoji = preg_replace_callback(
  $pattern,
  function ($captures) {
    return chr(hexdec($captures[1]));
  },
  $utf8Byte
);

$telegramResponseText = "Hey user " . $emoji;
Run Code Online (Sandbox Code Playgroud)

$ emoji可用于电报机器人响应文本.


Sul*_*lly 7

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

$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);
Run Code Online (Sandbox Code Playgroud)

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

:phone:
:white_check_mark:
:factory:
:page_with_curl:
Run Code Online (Sandbox Code Playgroud)


Ent*_*aah 5

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

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