Skip to content

Instantly share code, notes, and snippets.

@ford153focus
Created April 20, 2021 15:59
Show Gist options
  • Save ford153focus/782d84db231af536ab6397dc32bdfcac to your computer and use it in GitHub Desktop.
Save ford153focus/782d84db231af536ab6397dc32bdfcac to your computer and use it in GitHub Desktop.
example of talk bot and telegram
<?php
$request = json_decode(file_get_contents('php://input'), true); // Читаем JSON POST запрос со стороны TG
// Проверяем чтобы запрос не был пустым
if (empty($request)) {
exit(json_encode(['response' => ['status' => 2, 'description' => 'Пустой запрос']]));
}
$tgid = $request['message']['from']['id']; // уникальный ID пользователя
$tg_command = trim($request['message']['text']); // сообщение, которое прислал пользователь
$from = '????????????????????????'; // уникальный ключ бота, полученный при регистрации у BotFather
$echo = 'ТЕКСТ ОТВЕТА ПОЛЬЗОВАТЕЛЮ';
$post = [
'chat_id' => $tg_id,
'text' => $echo,
'parse_mode' => 'html',
'disable_notification' => '0'
];
$post['disable_web_page_preview'] = true;
$handle = curl_init("https://api.telegram.org/bot$from/sendMessage");
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($handle, CURLOPT_TIMEOUT, 3);
curl_setopt($handle, CURLOPT_POST, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment