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.
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.
How to go
First here (in English). It describes what bots are, why and how to write.
Then follow the instructions:
BotFather bot - use the / newbot command to create a new bot and get a token for it;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
See akalongman / php-telegram-bot
Source: https://ru.stackoverflow.com/questions/448750/
All Articles