Tell me, has anyone encountered in sending a letter using php in telegrams to a specific static user?

Hint how to go? The API is not even special.

5 answers 5

How to go

First here (in English). It describes what bots are, why and how to write.

Then follow the instructions:

  1. in Telegram, start a dialogue with the BotFather bot - use the / newbot command to create a new bot and get a token for it;
  2. Receive messages for your bot in one of two ways:
    • You yourself periodically request updates from Telegram for your bot (“Hey, is there any new messages?”);
    • indicate to the Telegram the URL at which he himself will knock on you as soon as he receives a message for your bot.
  3. When you receive a message from a user, you can send a response, a document, a picture, a video, many more, of which, or just a message .

So far I have written such a simple method for calling the Telegram API methods:

 private function callApi( $method, $params) { $url = sprintf( "https://api.telegram.org/bot%s/%s", Config::get('telegram.token'), $method ); $ch = curl_init(); curl_setopt_array( $ch, array( CURLOPT_URL => $url, CURLOPT_POST => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_FOLLOWLOCATION => FALSE, CURLOPT_HEADER => FALSE, CURLOPT_TIMEOUT => 10, CURLOPT_HTTPHEADER => array( 'Accept-Language: ru,en-us'), CURLOPT_POSTFIELDS => $params, )); $response = curl_exec($ch); return json_decode( $response); } 

For example, having already received the message $data , you can answer it like this:

 $this->callApi( 'sendMessage', array( 'chat_id' => $data->message->chat->id, 'text' => "Здесь сообщение от нашего бота", // 'reply_to_message_id' => $data->message->message_id, )); 

Start by sending the POST request itself through the browser, understand how this works:

 https://api.telegram.org/bot<токен бота>/sendMessage?chat_id=<айди чата, куда хочешь отправить>&text=<text> 

then decompose it into variables in the code and send. Here is the working code, insert only the bot token:

 <?php /** * Telegram Bot access token и URL. */ $access_token = 'xxxxxxxxxxxxxxx'; $api = 'https://api.telegram.org/bot' . $access_token; /** * Задаём основные переменные. */ $output = json_decode(file_get_contents('php://input'), TRUE); $chat_id = $output['message']['chat']['id']; $first_name = $output['message']['chat']['first_name']; $message = $output['message']['text']; switch($message) { case '/test': sendMessage($chat_id, "HELLO"); break; } function sendMessage($chat_id, $message) { file_get_contents($GLOBALS['api'] . '/sendMessage?chat_id=' . $chat_id . '&text=' . urlencode($message)); } 

    Excellent API for them.

     // прочитываем входящую инфо $content = file_get_contents("php://input"); // принимаем JSON строку и преобразуем ее в переменную PHP $update = json_decode($content, true); //получаем ID чата $chatID = $update["message"]["chat"]["id"]; $uID = $update["message"]["from"]["username"] 

      Try this PHP Wrapper and Drupal module using Telegram CLI . PHP wrapper for command line telegram

      • Please do not post replies that consist of just one link. Give a sample code, and add the link to the materials. Often the links become broken and in this case the value of your answer will tend to zero. - Timofei Bondarev

      See akalongman / php-telegram-bot

      https://github.com/akalongman/php-telegram-bot

      • 2
        Please do not post replies that consist of just one link. Give a sample code, and add the link to the materials. Often the links become broken and in this case the value of your answer will tend to zero. - Timofei Bondarev