php 如何使用 Telegram Bot API 发送表情符号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31430587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send Emoji with Telegram Bot API?
提问by Vlmake
I need to send messages containing emoji with my Telegram Bot.
我需要用我的 Telegram Bot 发送包含表情符号的消息。
So I copy/paste emoji code :nine:
for example, in my message text and send it to a user, BUT emoji didn`t work.
所以我复制/粘贴表情符号代码:nine:
,例如,在我的消息文本中并将其发送给用户,但表情符号不起作用。
This is my sample code and function:
这是我的示例代码和功能:
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']);
So, my question is: How can I send emoji by Telegram bot?
所以,我的问题是:如何通过 Telegram bot 发送表情符号?
采纳答案by Mustafa
you need to specify emoji's unicode value.
您需要指定表情符号的 unicode 值。
these are returned by a function as emoji value like u'\U000026C4' which is snowman. although it is in python, you can apply it for php.
这些由一个函数作为表情符号值返回,例如 u'\U000026C4' 是雪人。虽然它是在python中,但您可以将其应用于php。
回答by codeWhisperer
I faced the same issue a few days ago.. The solution is to use Bytes (UTF-8) notation from this table: http://apps.timwhitlock.info/emoji/tables/unicode
几天前我遇到了同样的问题。 解决方案是使用下表中的字节(UTF-8)表示法:http: //apps.timwhitlock.info/emoji/tables/unicode
examples:
例子:
\xF0\x9F\x98\x81 GRINNING FACE WITH SMILING EYES
\xF0\x9F\x98\x81 笑眯眯的笑脸
\xF0\x9F\x98\x89 WINKING FACE
\xF0\x9F\x98\x89 眨眼的脸
回答by Milad Rahimi
You can create it from utf8 bytes.
您可以从 utf8 字节创建它。
See emoji list and their utf8 codes here: http://apps.timwhitlock.info/emoji/tables/unicode
在此处查看表情符号列表及其 utf8 代码:http: //apps.timwhitlock.info/emoji/tables/unicode
Convert utf8 codes to telegram-ready response text with following code:
使用以下代码将 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;
The $emoji can be used in telegram bot response texts.
$emoji 可用于电报机器人响应文本。
回答by Entaah Laah
I am using this code at linux bash and curl command for grinning face
我在 linux bash 和 curl 命令中使用此代码来咧嘴笑
curl -X POST "https://api.telegram.org/botTOKEN/sendMessage" -d "chat_id=ID&text=%F0%9F%98%80&parse_modwarninge=Markdown"
回答by Dima Stefantsov
Real solution is to use https://github.com/spatie/emoji(composer require spatie/emoji
) for Emoji codes. Now your code will look like
真正的解决方案是使用https://github.com/spatie/emoji( composer require spatie/emoji
) 作为 Emoji 代码。现在你的代码看起来像
Emoji::CHARACTER_EYES
or
或者
Emoji::eyes()
This is something you could really use. Unlike writing all the codes manually and having hard time understanding what is it on the first glance.
这是你真正可以使用的东西。不像手动编写所有代码并且第一眼就很难理解它是什么。
回答by Salione
You can just copy emojy from telegram and paste in text to send like
您只需从电报中复制表情符号并粘贴到文本中即可发送
$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);
回答by Jeroen Peiffer
I have been looking for an answer for this for a long time, but could not get it working. my scripting skills are poor and converting php answers to bash proved a challenge.
我一直在寻找这个问题的答案很长一段时间,但无法让它发挥作用。我的脚本技能很差,将 php 答案转换为 bash 被证明是一个挑战。
But, nonetheless I got it working with a most simple solution: I went to telegram desktop messenger, there i send the required emoji ().
但是,尽管如此,我还是使用了一个最简单的解决方案:我去了电报桌面信使,在那里我发送了所需的表情符号 ()。
Than I made a variable: bus=""
比我做了一个变量:bus=""
Now I can use the variable in the curl as: "text=some text $bus"
现在我可以在 curl 中使用变量:“text=some text $bus”
This works great using bash on linux, I suppose it could also work in php.
这在 linux 上使用 bash 效果很好,我想它也可以在 php 中工作。
回答by Green
An addition to this answer https://stackoverflow.com/a/31431810/1114926.
这个答案的补充https://stackoverflow.com/a/31431810/1114926。
The link that Mustafaprovided doesn't represent all emoji. This source is better http://emojipedia.org/??. It has variations of emoji in addition to the major sign.
Mustafa提供的链接并不代表所有表情符号。这个来源更好http://emojipedia.org/??。除了主要标志外,它还有表情符号的变体。
回答by WENPIN1
Just another choice $curl .... -F text="😁" -F parse_mode=html .... "https://api.telegram.org/botTOKEN/sendMessage"
只是另一个选择 $curl .... -F text="