42

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?

15 Answers 15

63

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

examples:

😁 \xF0\x9F\x98\x81 GRINNING FACE WITH SMILING EYES

😉 \xF0\x9F\x98\x89 WINKING FACE

4
  • Sucesfully used this with a PHP bot. +1 for providing so excellent link with that format emojis codes.
    – user2930576
    Feb 7, 2016 at 3:25
  • 5
    In PHP, remember to use it inside a double quoted string.
    – MTVS
    Feb 18, 2016 at 12:01
  • This is the only way to get emoji's working when calling the API url directly.
    – NVO
    Feb 13, 2018 at 15:58
  • Awesome, should be the accepted answer. This is the updated link: unicode.org/emoji/charts/full-emoji-list.html Sep 25, 2019 at 21:32
22

you need to specify emoji's unicode value.

check here

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.

3
  • 2
    In PHP, remember to use byte codes inside double quoted strings
    – MTVS
    Feb 18, 2016 at 12:02
  • Not all emoji in your link. E.g. there is no index finger up emogi, :point_up: shortcode.
    – Green
    Oct 4, 2016 at 15:27
  • 1
    you can find complete list of emoji here Feb 13, 2017 at 6:17
10

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);
6

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);

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:
2
  • 1
    All ingenious is simple ) I spent a lot of time before I saw your answer ) This is the only solution that worked for me Jul 20, 2020 at 22:22
  • All ingenious is simple ) I spent a lot of time before I saw your answer ) This is the only solution that worked for me Jul 20, 2020 at 22:22
5

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"
4

Real solution is to use https://github.com/spatie/emoji (composer require spatie/emoji) for Emoji codes. Now your code will look like

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.

3

> PHP 7

You just to use the \u and the unicode literal like following:

😀

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

Resource: https://stackoverflow.com/a/33548423/8760592

3

IMPORTANT THING:

No one said it.

You have to use double quote, not single ones. Code below will give you just text: \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);
}

But with double quotes you'll get emoji:

message_to_telegram("\xF0\x9F\x98\x82" . date('Y-m-d H:i:s'));
1

An addition to this answer https://stackoverflow.com/a/31431810/1114926.

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.

1

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.

1

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.

$message = 'Pointing down: 👇';

If you process that into a message (example above is PHP, but I guess it will work in any programming language)

1

You need to json_decode unicode value first, try like this.

json_decode('"\ud83d\ude0a"')
0

Just another choice $curl .... -F text="&#x1F601" -F parse_mode=html .... "https://api.telegram.org/botTOKEN/sendMessage"


will enable parse_mode to html and use HTML hexadecimal (hex) reference(&#x1F601) for unicode U+1F601

0

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. 😎

0

to get smileys and emojis.. you can always reference this emoji documentation for DEVs https://carpedm20.github.io/emoji/

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 18, 2022 at 22:49
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Jul 21, 2022 at 23:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.