2
votes

I develop simple telegram bot with php and host it on koding but telegram doesn't trigger my webhook; why ?!

how to debug webhook?

below code is my bot.

require_once "vendor/autoload.php";

use Telegram\Bot\Api;

$telegram=new Api('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');

if (isset($_GET['hook'])){
    $msg=$telegram->setWebhook([
        "url" => "https://umkk50a5b157.mohsenti.koding.io/phpBot/index.php",
        "certificate" => "/etc/apache2/ssl/apache.crt"
    ]);
    echo json_encode($msg)."<br>";
    echo "setup webhook ok";
    exit;
}

if(isset($_GET['uhook'])){
    $telegram->removeWebhook();
    echo "remove webhook ok";
    exit;
}

if(isset($_GET['debug'])){
    $fp=fopen('debug.txt','r');
    while($read=fread($fp,4096))
        echo $read;
    fclose($fp);
    echo "debug end";
    exit;
}

$updates=$telegram->getWebhookUpdates();

$fp=fopen('debug.txt','w');
fwrite($fp,"hello");
fwrite($fp,json_encode($updates));
fclose($fp);

foreach($updates as $update) {
    $telegram->sendMessage([
        'chat_id' => $update->getMessage()->getChat()->getId(),
        'text' => "bot received : " . $update->getMessage()->getText()
    ]);
}

I save request message to a file and send debug request to bot php file to read what's sent by telegram and hook to set webhook and uhook to unset hook.

thank you

1

1 Answers

1
votes
  1. Install ZEND DEBUGGER
  2. Install ZEND Studio
  3. Add ?debug_host=127.0.0.1&start_debug=1&debug_port=10137&debug_stop=1 to your Telegram Hook
  4. Send message via telegram - your Zend Studio will automatically stop for DEBUG

Testet with IIS!!!

Example

https://elron.elropro.com/xxxx/telegramhook.php?debug_host=127.0.0.1&start_debug=1&debug_port=10137&debug_stop=1