Можно ли через форму отправить команду например '/info' и что-бы получить потом ответ тг бот?

Рейтинг: 0Ответов: 1Опубликовано: 02.06.2023

Задаюсь вопросом, я создал бота в тг, сделал отправку сообщения через форму на html, но мне интересно, можно ли через форму отправить команду например '/info' и что-бы получить потом ответ. Как это реализовать?

Ответы

▲ 0Принят
<?php
$TOKEN = "ВАШ ТОКЕН";
$command = '/info';
$data = json_decode(file_get_contents('php://input'), true);
function sendTelegram($method, $response) {
    global $TOKEN;
    $ch = curl_init('https://api.telegram.org/bot' . $TOKEN . '/' . $method);  
    curl_setopt($ch, CURLOPT_POST, 1);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, $response);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    $res = curl_exec($ch);
    curl_close($ch);
    return $res;
}

if (!empty($data['message']['text'])) {
    $text = $data['message']['text'];
    if (mb_stripos($text, $command) !== false) {
      sendTelegram(
              'sendMessage', 
              array(
                  'chat_id' => $data['message']['chat']['id'],
                  'text' => 'Сюда любой текст <b>BOLD</b> <code>CODE</b> и тд',
                  'parse_mode' => "HTML",
                  'disable_web_page_preview' => true,
                  'reply_to_message_id' => $data['message']['message_id']
              )
          );
          exit();
    }
}

Так же через функцию SendTelegram можно отправлять документы, видео, фото заменив sendMessage на sendDocument, sendAnimation, sendAudio, sendVideo, sendVideoNote, sendPhoto

Например:

if (!empty($data['message']['text'])) {
    $text = $data['message']['text'];
    if (mb_stripos($text, $command) !== false) {
      sendTelegram(
              'sendDocument', 
              array(
                  'chat_id' => $data['message']['chat']['id'],
                  'document' => new \CURLFile($file_path)
              )
          );
          exit();
    }
}

Подробнее можно почитать тут https://core.telegram.org/bots/api