- Как отправить Emoji с помощью Telegram Bot API?
- 15 ответов
- > PHP 7
- How to send Emoji with Telegram Bot API?
- Solution – 1
- Solution – 2
- Solution – 3
- Solution – 4
- Solution – 5
- Solution – 6
- Solution – 7
- Solution – 8
- Solution – 9
- Solution – 10
- Solution – 11
- > PHP 7
- Solution – 12
- Solution – 13
- Solution – 14
- IT NEWS
- Monday, May 8, 2017
- telegram bot keyboard + emoji buttons tutorial
- telegram bot tutorial
- How to make emoji buttons?
Как отправить Emoji с помощью Telegram Bot API?
Мне нужно отправлять сообщения, содержащие эмодзи, с моим ботом Telegram.
Поэтому я копирую / вставляю код Emoji :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?
15 ответов
Вам нужно указать значение Unicode для эмодзи.
они возвращаются функцией как значение Emoji, как U ‘\U000026C4’, который является снеговиком. хотя это в python, вы можете применить его для php.
Я столкнулся с той же проблемой несколько дней назад. Решение заключается в использовании байтов (UTF-8) из этой таблицы: http://apps.timwhitlock.info/emoji/tables/unicode
\ xF0 \ x9F \ x98 \ x81 Усмехаясь лицом с улыбающимися глазами
\xF0\x9F\x98\x89 ВЫИГРЫШНОЕ ЛИЦО
Вы можете создать его из utf8 байтов.
Преобразуйте коды utf8 в готовый к телеграмме текст ответа со следующим кодом:
)@x'; $emoji = preg_replace_callback( $pattern, function ($captures) < return chr(hexdec($captures[1])); >, $utf8Byte ); $telegramResponseText = "Hey user " . $emoji;
$ Emoji можно использовать в текстах ответов ботов-телеграмм.
Вы можете просто скопировать смайлик из телеграммы и вставить текст, чтобы отправить лайк
$bot_url = "https://api.telegram.org/bot$apiToken/"; $url = $bot_url . "[email protected] 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);
Я использую этот код в команде linux bash и curl для улыбающегося лица
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 ( composer require spatie/emoji ) для кодов эмодзи. Теперь ваш код будет выглядеть
Это то, что вы действительно можете использовать. В отличие от написания всех кодов вручную и с трудом понимая, что это на первый взгляд.
> PHP 7
Вы просто используете \u и литерал юникода, как показано ниже:
Вы должны использовать двойные кавычки, а не одинарные. Код ниже даст вам просто текст: \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'));
How to send Emoji with Telegram Bot API?
I need to send messages containing emoji with my 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.
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?
Solution – 1
you need to specify emoji’s unicode value.
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.
Solution – 2
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
😁 xF0x9Fx98x81 GRINNING FACE WITH SMILING EYES
😉 xF0x9Fx98x89 WINKING FACE
Solution – 3
The link that Mustafa provided doesn’t represent all emoji. This source is better http://emojipedia.org/ ☝️. It has variations of emoji in addition to the major sign.
Solution – 4
Real solution is to use https://github.com/spatie/emoji ( composer require spatie/emoji ) for Emoji codes. Now your code will look like
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.
Solution – 5
Then you must convert UTF-8 codes to Telegram-ready response text with the following code:
)@x', function ($captures) < return chr(hexdec($captures[1])); >, $utf8emoji ); return $utf8emoji; > $utf8emoji = 'xF0x9Fx98x81'; $telegramReadyResponse = 'Hi user ' . telegram_emoji($utf8emoji);
Solution – 6
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.
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=”🚌”
Now I can use the variable in the curl as: “text=some text $bus”
This works great using bash on linux, I suppose it could also work in php.
Solution – 7
I am using this code at linux bash and curl command for grinning face
curl -X POST "https://api.telegram.org/botTOKEN/sendMessage" -d "chat_id=ID&text=%F0%9F%98%80&parse_modwarninge=Markdown"
Solution – 8
will enable parse_mode to html and use HTML hexadecimal (hex) reference(😁) for unicode U+1F601
Solution – 9
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 . "[email protected] 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);
another way you can use the name of shap ,for example for home you can use :house:
example:
:phone: :white_check_mark: :factory: :page_with_curl:
Solution – 10
Easiest way is to just copy and past the emoji you need in your code.
But then you will have to make sure your source code files are stored UTF8
I just opened Telegram on my system and copied the ones I needed and never looked back 🙂 Funny thing is that I do not have to work with constants, specific names
or the unicode character value. I can directly see what emoji is used IN MY EDITOR.
If you process that into a message (example above is PHP, but I guess it will work in any programming language)
Solution – 11
> PHP 7
You just to use the u and the unicode literal like following:
Solution – 12
Note that, at least with JavaScript/Node.js, you can just paste the emoticon’s directly into the code and they translate find into the Telegram API. EG. 😎
Solution – 13
You need to json_decode unicode value first, try like this.
Solution – 14
You have to use double quote, not single ones.
Code below will give you just text: xF0x9Fx98x822021-11-21 09:54:25
define('TELEGRAM_TOKEN', '9999999999:FFFFFFFFFFFFraxb6jajm8cDlKPOH-FFFFFFF'); define('TELEGRAM_CHATID', '@my_bot'); message_to_telegram('xF0x9Fx98x82' . 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); >
But with double quotes you’ll get emoji:
message_to_telegram("xF0x9Fx98x82" . date('Y-m-d H:i:s'));
IT NEWS
Programming PHP tutorials for beginners. Technology and product reviews.
Monday, May 8, 2017
telegram bot keyboard + emoji buttons tutorial
telegram bot tutorial
How to make custom location for telegram bot buttons?
You can use this example. New array for next button lines.
$keyboard = array(
‘keyboard’ => array(
array(
«button», «one»,
«two», «three»
),
array(
«Currency»,
«Menu»
),
array(
«1», «2», «3»
),
array(
«4»
)
),
‘resize_keyboard’ => true,
‘one_time_keyboard’ => false
);
$postfields = array(
‘chat_id’ => «$chatid»,
‘text’ => «$reply»,
‘reply_markup’ => json_encode($keyboard)
);
How to make emoji buttons?
Emoji keyboard for Telegram bot PHP script with Json
You can take emoji codes here: http://www.charbase.com/block/miscellaneous-symbols-and-pictographs click on Icon, then copy Java Escape emoji code and insert as button text. array(«Text \ud83d\ude08″, » Text 2 \ud83d\udcaa», «\ud83d\udcf2»)
PHP example with curl telegram API:
$botid = «******BOT_ID»;
$chatid = «*****Telegram Chat_ID»;
$reply = «Working»;
$url = «https://api.telegram.org/bot$botid/sendMessage»;
$keyboard = array(
‘keyboard’ => array(
array(
«button»,
«\ud83d\ude08»,
«\ud83d\udcaa»,
«\ud83d\udcf2»
),
array(
«Currency»,
«Menu»
),
array(
«1», «2», «3»
),
array(
«4»
)
),
‘resize_keyboard’ => true,
‘one_time_keyboard’ => false
);
$postfields = array(
‘chat_id’ => «$chatid»,
‘text’ => «$reply»,
‘reply_markup’ => json_encode($keyboard)
);
$str = str_replace(‘\\\\’, ‘\\’, $postfields);
print_r($str);
if (!$curld = curl_init()) exit;
>
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $str);
curl_setopt($curld, CURLOPT_URL,$url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
also you can just add emoji from standard Mac keyboard as text: