A
A
Andrey Gotsulyak2018-01-29 19:28:10
PHP
Andrey Gotsulyak, 2018-01-29 19:28:10

Long polling - ready-made solutions?

Hello, I am making a telegram bot and using webhook. But there are a number of problems, and I decided to try Long polling. Are there ready-made solutions with Long polling or good examples. Never worked with Long polling before.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Number_11, 2018-03-09
@Number_11

1. Install composer, then go to the project folder via the command line (cd c://bot - example), execute the code:
"composer require guzzlehttp/guzzle".
2. Create init.php, in this folder, with the content:
<?php
// Includes)
use GuzzleHttp\Client;
include('vendor/autoload.php');
include('telegrambot.php');
//Get data
$telegramApi = new TelegramBot();
// Calculated loop, handler
while (true) {
sleep(2);
$updates = $telegramApi->getUpdates(); // Get the update using the getUpdates method
foreach ($updates as $update){
if (isset($update->message->text)) { // Check Update for text
$text = $update->message->text; // Variable with message text
$chat_id = $update->message->chat->id; // Chat user ID
$first_name = $update->message->chat->first_name; //Username
$username = $update->message->chat->username; //Username of the user
print_r($chat_id);
print_r($username);
if ($text == '/start'){ // If the user is connected for the first time, they will receive a greeting
$telegramApi->sendMessage($chat_id, 'Hi'. ' ' . $first_name . '!'); //Greetings to the User
} else {
$telegramApi->sendMessage($chat_id, $first_name . '! How are you?' ); // Asks how are you
}
}
}
}
3. Create telegramBot.php, in this folder, here is the content:
<?php
// Connecting the library
use GuzzleHttp\Client;
use Telegram\Api;
class TelegramBot
{
protected $token = "BOT_TOKEN";
protected $updateId;
// The function collects the URL
protected function query($method, $params = [])
{
$url = " https://api.telegram.org/bot ";
$url .= $this->token;
$url .= "/" . $method;
if (!empty($params))
{
$url .= "?" . http_build_query($params);
}
$client = new Client([
'base_uri' => $url
]);
$result = $client->request('GET');
return json_decode($result->getBody());
}
// Get updates
public function getUpdates()
{
$response = $this->query('getUpdates', [
'offset' => $this->updateId + 1
]);
if (!empty($response->result)) {
$this->updateId = $response->result[count($response->result) -1]->update_id;
}
return $response->result;
// Send messages
public function sendMessage($chat_id, $text)
{
$response = $this->query('sendMessage',[
'chat_id' => $chat_id,
'text' => $text
]);
return $response;
}
}
4. To run it, run the file in the same place on the command line:
php init.php
In the same place in the console you will see the user's Id and his username)
Here is such an example that is not very stupid, but it works.

M
My joy, 2018-01-29
@t-alexashka

I don’t know what kind of bot you are making that needs long polling, but if you really need it, then use this https://github.com/walkor/phpsocket.io (based on workerman)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question