Telegram bot in PHP

Content
Registering a bot
Incoming messages
Bot’s Responses
Example of a script

Registering a bot
To register a new bot, you need to write the «bot daddy» @BotFather command /newbot
Telegram bot script

The next message we send is the name for the bot, the word «bot» or «_bot» must be at the end of the name. We will receive a token as a response message:
Telegram bot in PHP

You can also set up a description and an avatar here:

/setname File name
/ setdescription Short description
//getabouttext Description of the bot
/ setuserpic Userpic
You also need to put a «Webhook» so that all messages from Telegram come to the PHP script (https://example.com/bot.php ). to do this, click on the link where the received token and the script address are installed.

https://api.telegram.org/bot/setWebhook?url=https://example.com/bot.php

The response will be
{"ok":true,"result":true,"description":"Webhook was set"}

Incoming messages
The messages are sent as a POST request, with the application/json type. You can get it in PHP as follows:

$data = file_get_contents('php://input');
$data = json_decode($data, true);

PHP
To view the incoming data, they will have to be dumped into a file:

file_put_contents(__DIR__ . '/message.txt', print_r($data, true));

Telegram Request:
Array (
[update_id] => 17584194
[message] => Array (
[message_id] => 26
[from] => Array (
[id] => 123456789
[is_bot] =>
[first_name] => UserName
[language_code] => ru-US
)
[[char] => Array (
[id] => 123456789
[first_name] => UserName
[type] => private
)
[date] => 1541888068
[text] => Hello bot!
)
)

We will get the text of the message:
if (!empty($data['message']['text'])) {
$text = $data['message']['text'];
echo $text;
}

Photo
When sending a photo to the bot, an array of previews is sent to the script, the last element will be the original photo. The maximum file size is 20 MB.

Request from Telegrams:

Array (
[update_id] => 17584194
[message] => Array (
[message_id] => 38
[from] => Array (
[id] => 123456789
[is_bot] =>
[first_name] => UserName
[language_code] => ru-US
)
[chat] => Array (
[id] => 123456789
[first_name] => UserName
[type] => private
)
[date] => 1541924962
[photo] => Array (
[0] => Array (
[file_id] => AgADAgADUqexG7u8OEudBvlhgMzKC1agOQ8ABC6Bx26USA7Mw3gAAgI
[file_size] => 1196
[width] => 51
[height] => 90
)
[1] => Array (
[file_id] => AgttAgADUqoxG7u8OEudBvlhgMzKC1agOQ8ABKwp_3jDPrIlxHgAAgI
[file_size] => 21146
[width] => 180
[height] => 320
)
[2] => Array (
[file_id] => AgADAgADUqyxG7u8OEudBvlhgMzKC1agOQ8ABAN8gJWpUT1MxXgAAgI
[file_size] => 90940
[width] => 449
[height] => 800
)
[3] => Array (
[file_id] => AgADAgADUqouu7u8OEudBvlhgMzKC1agOQ8ABIqVC1nEpbLDwngAAgI
[file_size] => 114363
[width] => 719
[height] => 1280
)
)
)
)

To download a file, you need to send a POST or GET request to receive the image with the file_id parameter at the URL:

https://api.telegram.org/bot/getFile

You will receive information about the file in response:

Array (
[ok] => 1
[result] => Array (
[file_id] => AgADAgADUqoxG5u88E0dBvlhgMzKC1agOQ8ABIqVC1nEpbLDwngAAgI
[file_size] => 114363
[file_path] => photos/file_1.jpg
)
)

Then you can download it from the link:

https://api.telegram.org/file/bot/

In PHP, saving a file to the server can be implemented as follows:

$token = ‘123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11’;

if (!empty($data['message']['photo'])) {
$photo = array_pop($data['message']['photo']);

$ch = curl_init('https://api.telegram.org/bot' . $token . '/getFile');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file_id' => $photo['file_id']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);

$res = json_decode($res, true);
if ($res['ok']) {
$src = 'https://api.telegram.org/file/bot' . $token . '/' . $res['result']['file_path'];
$dest = __DIR__ . '/' . time() . '-' . basename($src);
copy($src, $dest);
}
}

Document
Request from Telegrams:

Array (
[update_id] => 17474201
[message] => Array (
[message_id] => 44
[from] => Array (
[id] => 123456789
[is_bot] =>
[first_name] => UserName
[language_code] => ru-US
)
[chat] => Array (
[id] => 123456789
[first_name] => UserName
[type] => private
)
[date] => 1541925844
[document] => Array (
[file_name] => IMG_7947.JPG
[mime_type] => image/jpeg
[thumb] => Array (
[file_id] => AAQCABMNv_QOAATwQugveIZBldZ3AAIC
[file_size] => 2644
[width] => 67
[height] => 90
)
[file_id] => BQADAgADtQEAAqu9OEhzn2cEz8LpkgI
[file_size] => 1976218
)
)
)

Downloading files follows the same pattern as photos.

if (!empty($data['message']['document'])) {
$file_id = $data['message']['document']['file_id'];

$ch = curl_init('https://api.telegram.org/bot' . $token . '/getFile');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file_id' => $file_id));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);

$res = json_decode($res, true);
if ($res['ok']) {
$src = 'https://api.telegram.org/file/bot' . $token . '/' . $res['result']['file_path'];
$dest = __DIR__ . '/' . time() . '-' . basename($src);
copy($src, $dest);
}
}

Bot’s Responses
Sending a text
$response = array(
'chat_id' => $data['message']['chat']['id'],
'text' => 'Хай!'
);

$ch = curl_init('https://api.telegram.org/bot' . $token . '/sendMessage');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);

Sending an image
$response = array(
'chat_id' => $data['message']['chat']['id'],
'photo' => curl_file_create(__DIR__ . '/image.png')
);

$ch = curl_init('https://api.telegram.org/bot' . $token . '/sendPhoto');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);

Sending a file

$response = array(
'chat_id' => $data['message']['chat']['id'],
'document' => curl_file_create(__DIR__ . '/file.xls')
);

$ch = curl_init('https://api.telegram.org/bot' . $token . '/sendDocument');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_exec($ch);
curl_close($ch);

Example of a script

$photo['file_id'] )
);

$res = json_decode($res, true);
if ($res['ok']) {
$src = 'https://api.telegram.org/file/bot' . TOKEN . '/' . $res['result']['file_path'];
$dest = __DIR__ . '/' . time() . '-' . basename($src);

if (copy($src, $dest)) {
sendTelegram(
'sendMessage',
array(
'chat_id' => $data['message']['chat']['id'],
'text' => 'The photo is saved'
)
);

}
}

exit();
}

// They sent a file.
if (!empty($data['message']['document'])) {
$res = sendTelegram(
'getFile',
array(
'file_id' => $data['message']['document']['file_id'] )
);

$res = json_decode($res, true);
if ($res['ok']) {
$src = 'https://api.telegram.org/file/bot' . TOKEN . '/' . $res['result']['file_path'];
$dest = __DIR__ . '/' . time() . '-' . $data['message']['document']['file_name'];

if (copy($src, $dest)) {
sendTelegram(
'sendMessage',
array(
'chat_id' => $data['message']['chat']['id'],
'text' => 'The file is saved'
)
);
}
}

exit();
}

// Reply to text messages.
if (!empty($data['message']['text'])) {
$text = $data['message']['text'];

if (mb_stripos($text, 'hi') !== false) {
sendTelegram(
'sendMessage',
array(
'chat_id' => $data['message']['chat']['id'],
'text' => 'hi!'
)
);

exit();
}

// Sending a photo.
if (mb_stripos($text, 'photo') !== false) {
sendTelegram(
'sendPhoto',
array(
'chat_id' => $data['message']['chat']['id'],
'photo' => curl_file_create(__DIR__ . '/torin.jpg')
)
);

exit();
}

// Sending a file.
if (mb_stripos($text, 'file') !== false) {
sendTelegram(
'sendDocument',
array(
'chat_id' => $data['message']['chat']['id'],
'document' => curl_file_create(__DIR__ . '/example.xls')
)
);

exit();
}
}

Check Also

Soulbet – Download Software CSGO Case Opening site

Soulbet – Download Software CSGO Case Opening site

Welcome to SoulBet – your ultimate destination for CS:GO gambling! Dive into the adrenaline-fueled world …

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *