39

我需要使用 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 机器人发送表情符号?

4

14 回答 14

55

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

例子:

\xF0\x9F\x98\x81 笑容满面的笑脸

\xF0\x9F\x98\x89 眨眼

于 2016-01-05T11:09:50.523 回答
20

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

在这里检查

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

于 2015-07-15T13:30:01.840 回答
9

See emojis and their UTF-8 codes here: http://apps.timwhitlock.info/emoji/tables/unicode

Then you must convert UTF-8 codes to Telegram-ready response text with the following code:

<?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);
于 2016-11-21T08:53:32.493 回答
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"
于 2018-09-13T15:43:13.613 回答
4

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

$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:
于 2020-04-28T06:20:24.950 回答
2

真正的解决方案是使用https://github.com/spatie/emoji ( composer require spatie/emoji) 作为 Emoji 代码。现在你的代码看起来像

Emoji::CHARACTER_EYES

或者

Emoji::eyes()

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

于 2016-11-16T18:48:23.077 回答
2

> PHP 7

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

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

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

于 2021-01-11T13:51:43.650 回答
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'));
于 2021-11-21T10:02:53.810 回答
1

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

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

于 2016-10-04T15:44:03.363 回答
1

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

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

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

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

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

于 2017-02-08T21:10:57.517 回答
1

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

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

$message = 'Pointing down: ';

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

于 2020-06-05T09:25:12.610 回答
0

只是另一种选择 $curl .... -F text="

于 2019-12-06T09:16:38.183 回答
0

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

于 2021-06-08T16:24:28.047 回答
0

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

json_decode('"\ud83d\ude0a"')
于 2021-06-10T01:25:24.087 回答