I
I
Ivan Noname2018-07-10 13:28:06
PHP
Ivan Noname, 2018-07-10 13:28:06

Why can't I install a webhook when creating a telegram bot?

I want to create a simple bot. Installed on a virtual server https://github.com/TelegramBot/Api
Created a bot in BotFather
Registered a domain Installed Lets Encrypte to be https
I am writing this code:

<?php
header('Content-Type: text/html; charset=utf-8'); 
echo('Отправили заголовки');
require_once("vendor/autoload.php"); 
echo('Подключили Telegram API');
$token = "мой_токен";
$bot = new \TelegramBot\Api\Client($token);

if(!file_exists("registered.trigger")){ 
  /**
   * файл registered.trigger будет создаваться после регистрации бота. 
   * если этого файла нет значит бот не зарегистрирован 
   */
  // URl текущей страницы
  $page_url = "https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  echo($page_url);
  $result = $bot->setWebhook($page_url);
  if($result){
    	file_put_contents("registered.trigger",time()); // создаем файл дабы прекратить повторные регистрации
  }
}

// обязательное. Запуск бота
$bot->command('start', function ($message) use ($bot) {
    $answer = 'Добро пожаловать!';
    $bot->sendMessage($message->getChat()->getId(), $answer);
});

// помощ
$bot->command('help', function ($message) use ($bot) {
    $answer = 'Команды:
/help - помощ';
    $bot->sendMessage($message->getChat()->getId(), $answer);
});

// запускаем обработку
$bot->run();

?>

On this line $result = $bot->setWebhook($page_url); hangs tightly and crashes on timeout
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg, 2018-07-10
@wbsr

server in Russia check the passage of packages to api.telegram.org

I
Ivan Noname, 2018-07-10
@wbsr

And how to check it then?
Like this?

<?
$wait = 1; // wait Timeout In Seconds
$host = 'api.telegram.org';
$ports = [
    'http'  => 80,
    'https' => 443,
    'ftp'   => 21,
];

foreach ($ports as $key => $port) {
    $fp = @fsockopen($host, $port, $errCode, $errStr, $wait);
    echo "Ping $host:$port ($key) ==> ";
    if ($fp) {
        echo 'SUCCESS';
        fclose($fp);
    } else {
        echo "ERROR: $errCode - $errStr";
    }
    echo PHP_EOL;
};
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question