K. Kachalov
K. Kachalov

Reputation: 71

Can't send message with russian symbols with Telegram Bot

I made simple php bot for telegram which reply to me with a message. Code:

<?php
set_time_limit(0);

function SendMessage($message){
    global $website, $chatId;
    file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$message);
}

$botToken = "my token";
$website = "https://api.telegram.org/bot".$botToken;

$content = file_get_contents("php://input");
$update = json_decode($content, TRUE);
$message = $update["message"];

$chatId = $message["chat"]["id"];
$text = $message["text"];

if ($text == "/start") {
    SendMessage("welcome, it's nice to meet you");
} else {
    SendMessage("сори");
}

And everything was alright, but at some point my bot stopped working. I started debug it by deleting everything that works. And i found interesting result.

  1. In my first try bot didn't send anything. Message got both russian and english symbols.
  2. In second i delete russian symbols and it worked.
  3. BUT in third i put one russian word and it also worked.
  4. BUTx2 when i used exact word from first try bot didn't do anything.

images of results to make it clear

Please explain to me how it is possible. And how to solve it.

UPDATE. My script is encoded in UTF-8

Upvotes: 3

Views: 3636

Answers (1)

K. Kachalov
K. Kachalov

Reputation: 71

Thank you for your advices. I solved it by making proper URL-encoding. I just by added urlencode function:

function SendMessage($message){
    global $website, $chatId;

    $message = urlencode($message);

    file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$message);
}

Upvotes: 4

Related Questions