How to send Emoji with Telegram Bot API?
我需要使用Telegram Bot发送包含表情符号的消息。
因此,例如,我在消息文本中复制/粘贴表情符号代码
这是我的示例代码和功能:
1 2 3 4 5 6 7 8 9 10 11 12 | 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机器人发送表情符号?
我几天前也遇到了同样的问题。
解决方案是使用此表中的字节(UTF-8)表示法:
http://apps.timwhitlock.info/emoji/tables/unicode
例子:
?? \ xF0 \ x9F \ x98 \ x81带有微笑眼睛的研磨面
?? \ xF0 \ x9F \ x98 \ x89眨眼脸
您需要指定表情符号的unicode值。
在这里检查
这些由函数返回,作为表情符号值,例如u'\ U000026C4',即雪人。尽管它在python中,但您可以将其应用于php。
在此处查看表情符号及其UTF-8代码:
http://apps.timwhitlock.info/emoji/tables/unicode
然后,您必须使用以下代码将UTF-8代码转换为电报就绪的响应文本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?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); |
我在linux bash和curl命令上使用此代码咧嘴笑
1 | curl -X POST"https://api.telegram.org/botTOKEN/sendMessage" -d"chat_id=ID&text=%F0%9F%98%80&parse_modwarninge=Markdown" |
真正的解决方案是对https://github.com/spatie/emoji(
1 | Emoji::CHARACTER_EYES |
要么
1 | Emoji::eyes() |
这是您真正可以使用的东西。不同于手动编写所有代码并且一眼就很难理解它是什么。
您可以仅从电报中复制emojy并粘贴文本以进行发送?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $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); |
我一直在寻找答案,但是无法正常工作。我的脚本编写能力很差,将php答案转换为bash证明是一个挑战。
但是,尽管如此,我还是使用了一个最简单的解决方案:
我去电报桌面Messenger,然后在其中发送所需的表情符号(??)。
比我创建一个变量:bus =" ??"
现在,我可以在curl中使用以下变量:" text = some text $ bus"
在Linux上使用bash可以很好地工作,我想它也可以在php中工作。
此答案的补充https://stackoverflow.com/a/31431810/1114926。
Mustafa提供的链接并不代表所有表情符号。此来源更好http://emojipedia.org/。除了主要符号外,它还具有表情符号的变体。
最简单的方法是只复制并粘贴代码中需要的表情符号。
但是,然后您必须确保将源代码文件存储为UTF8
我只是在系统上打开Telegram并复制了所需的文件,再也没有回过头:)有趣的是,我不必使用常量,特定名称
或Unicode字符值。我可以直接看到在我的编辑器中使用了什么表情符号。
1 | $message = 'Pointing down: ??'; |
如果将其处理为消息(上面的示例是PHP,但是我想它可以在任何编程语言中运行)
只是另一个选择
$ curl .... -F text ="&#x1F601" -F parse_mode = html ...."" https://api.telegram.org/botTOKEN/sendMessage"
将启用parse_mode到html并将html十六进制(hex)引用(&#x1F601)用于Unicode U + 1F601