Search code examples
phputf-8keyboardtelegram-botnon-english

telegram keyboard not showing non English languages chracters?


I'm trying to write a php telegram bot in Persian which is a non English utf8 language. when I try to send plain text in Persian to my client it works perfectly ok

    $array = [
    "chat_id" => $chat_id,
    "text" => $someNonEnglishText
];
$telegram->sendMessage($array);

but when I try to send keyboard data with the code below it just show some question marks like ???????????????? on my keyboard buttons

    $reply_markup = $telegram->replyKeyboardMarkup([
  'keyboard' => $someNonEnglishkeyboard, 
  'resize_keyboard' => true, 
  'one_time_keyboard' => true
   ]);
    $telegram->sendMessage([
  'chat_id' => $updates[0]->getMessage()->getChat()->getId(), 
  'text' => $something,
  'reply_markup' => $reply_markup
]);

an I'm perfectly sure that my bot php file is utf8 by running the following command in terminal file myfile.php whilch I got the answer: surveyBot.php: PHP script, UTF-8 Unicode text

can someone help?

ps:the above code works perfectly fine for English keyboard.


Solution

  • Instead of using specific telegram framework, use following code (just for test purposes). Change chat_id and BOT_TOKEN variables. When this code runs, if you receive healthy keyboard then your framework has problem with utf-8.($telegram belonging class in your code)

    <?php
    
    $chat_id = 123; //replace with your client chat id
    define('BOT_TOKEN','1234:xyz'); //replace with your token
    
    define('BOTAPI','https://api.telegram.org/bot' . BOT_TOKEN .'/');
    $reply_markup = [
        'keyboard'          => [[['text' => 'سلام'], ['text' => 'خدانگهدار']]],
        'resize_keyboard'   => true,
        'one_time_keyboard' => true,
    ];
    $data = [
        'chat_id'      => $chat_id,
        'text'         => 'utf-8 test',
        'reply_markup' => json_encode($reply_markup),
    ];
    $ch = curl_init(BOTAPI . 'sendMessage');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);