Debug Telegram Bot SDK with webhook in Laravel

Reading this article means you have trouble getting your Telegram bot up and running with a webhook. There are several parts we can check and debug.

This article is the second part in a series of two. In the first part we setup a working Telegram Bot with Laravel and have one working /help command. Check the first article here.

Before we dive into the options, let’s understand the different parts and the steps you took to get it up and running.

You did achieve the following steps:

  • At the Telegram ecosystem you have created your ‘MyPreciousBot’ bot through @BotFather and you have its name and token;
  • Created a Laravel site and added the Telegram Bot SDK package with composer;
  • Configured the bot settings, created a route, and created a custom HelpCommand;
  • You told your PreciousBot at Telegram Ecosystem what the public webhook url is to send messages to;
  • Added your bot to your Telegram client where you can use /help to get a response.

Use NGROK console to see incoming messages

If you have exposed your internal DEV environment with a NGROK tunnel to the outside world you can see incoming messages in the terminal. Like so:

You can see clearly an incoming POST request to the right webhook URL (Good to double check this) but still there is a 500 Internal Server Error. Our complete webhook url at the moment of testing is:

https://0f306a1e.ngrok.io/42yUojv1YQPOssPEpn5i3q6vjdhh7hl7djVWDIAVhFDRMAwZ1tj0Og2v4PWyj4PZ/webhook

Conclusion:

  • Telegram ecosystem knows your webhook and tries to deliver messages to your webhook. So the setwebhook was a success and it seems that there is an internal error at the Laravel site;
  • If the webhook was not set successfully there would not be any HTTP Requests. In that case re-set your webhook URL with Telegram API!

Check the state of the webhook at the Telegram side

Now we know that Telegram is trying to deliver a message to the correct webhook address but it fails to do so. Next we can check if Telegram knows more and how many messages are in queue. We can check this by calling the endpoint getWebhookInfo. You could again create a new GET request in Postman for later use but you could also paste the url in a browser and check the response there. The URL contains the Telegram API address followed by string ‘bot’ and your bot token ending with /getWebhookInfo. Like so:

https://api.telegram.org/bot123456789:ABC_a0bcDefGhijklmno_0pqrSt-uvWXYZA/getWebhookInfo

Replace the bot123456789:ABC_a0bcDefGhijklmno_0pqrSt-uvWXYZA with your bot token and send the request. You will see something like this:

{
    "ok": true,
    "result": {
        "url": "https://0f306a1e.ngrok.io/42yUojv1YQPOssPEpn5i3q6vjdhh7hl7djVWDIAVhFDRMAwZ1tj0Og2v4PWyj4PZ/webhook",
        "has_custom_certificate": false,
        "pending_update_count": 2,
        "last_error_date": 1587324431,
        "last_error_message": "Wrong response from the webhook: 500 Internal Server Error",
        "max_connections": 40
    }
}

In queue there are two more messages and the last error message is error 500. Also at this moment your are able to check if Telegram has the correct webhook url. Think about it when you restart a new ngrok tunnel and the webhook url is pointing to the previous tunnel. This getWebhookInfo() message could show you that your webhook url is incorrect. For our problem this is not the case. The webhook url is still the same and active. Next attempt.

Test your webhook directly

To find out what happens on the Laravel side we could deliver a message to your webhook directly and see what error message comes back. In this method we do not let Telegram send a payload to your webhook but we will drop off a payload right at the gates of Laravel. See Telegram webhook documentation for more examples.

You can even send some garbage to it and see what kind of response you get. We do this again with Postman. Open Postman and create a new POST request ‘Test my supersonic webhook’ and paste in the complete URL of your active webhook. Ours is still:

https://0f306a1e.ngrok.io/42yUojv1YQPOssPEpn5i3q6vjdhh7hl7djVWDIAVhFDRMAwZ1tj0Og2v4PWyj4PZ/webhook

Click on the Body and past in a test payload with a /help command:

{
	"update_id":10000,
	"message":{
		"message_id":17957,
		"from":{
		"id":111111,
		"is_bot":false,
		"first_name":"Test",
		"username":"Test",
		"language_code":"nl"
	},
	"chat":{
		"id":111111,
		"first_name":"Test",
		"username":"Test",
		"type":"private"
	},
	"date":1584112226,
	"text":"/help",
	"entities":[{
		"offset":0,
		"length":5,
		"type":"bot_command"
		}]
	}
}

Replace the id:111111 with your Telegram ID otherwise the HelpCommand does not know where to send the answer to. To check your Telegram ID, visit bot @id_chatbot. Send /start and you will get your id back.

Press Send and voila, we have an error:

In the above example you see some error. These kinds of error’s are also reported in the storage\logs\laravel.log file. We see there is a problem in our HelpCommand.php file at line 31.

We open the file app\Telegram\Commands\HelpCommand.php and scroll down to row 31:

public function handle($arguments)

For Telegram SDK Bot ^3.1 we do not need $arguments anymore. I will remove this and save the file. Remember, this is only an example of what could be wrong.

Now let’s try to resend the payload from Postman.

No more error and status 200. Problem fixed.

I hope I gave you some ideas about how to troubleshoot the webhook connection. Please also check the laravel.log file for internal Laravel error’s. Make sure you did not make any typo’s and double check your bot token and webhook secret string. That is all for now.

If you have more ideas or need help specific on getting the webhook work, please leave a comment below.

Leave a comment

Your email address will not be published. Required fields are marked *