Webhook & Updates
Getting Updates
There are two mutually exclusive ways of receiving updates for your bot — the getUpdates()
method on one hand and Webhooks on the other. Incoming updates are stored on the server until the bot receives them either way, but they will not be kept longer than 24 hours.
Regardless of which option you choose, you will receive JSON-serialized Update objects as a result.
Getting Updates Via Webhook
In order to receive updates via a Webhook, You first need to tell your webhook URL to Telegram. You can use setWebhook($url)
method to specify a url and receive incoming updates via an outgoing webhook.
Whenever there is an update for the bot, Telegram will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, Telegram will give up after a reasonable amount of attempts.
If you'd like to make sure that the Webhook request comes from Telegram, we recommend using a secret path in the URL, e.g. https://www.example.com/<token>
. Since nobody else knows your bot's token, you can be pretty sure it's Telegram who made the request.
See setWebhook docs for a list of supported parameters and other info.
$response = $telegram->setWebhook('https://example.com/<token>/webhook');
// Or if you are supplying a self-signed-certificate
$response = $telegram->setWebhook('https://example.com/<token>/webhook', '/path/to/public_key_certificate.pub');
// Don't forget to setup a POST route in your Laravel Project.
$response = Telegram::setWebhook('https://example.com/<token>/webhook');
// Or if you are supplying a self-signed-certificate
$response = Telegram::setWebhook('https://example.com/<token>/webhook', '/path/to/public_key_certificate.pub');
Notes
You will not be able to receive updates using
getUpdates()
for as long as an outgoing webhook is set up.To use a self-signed certificate, you need to upload your public key certificate using certificate parameter. Please pass the absolute path to the certificate when uploading.
Ports currently supported for Webhooks: 443, 80, 88, 8443.
Once you set the webhook, You can then use the below function to retrieve the updates that are sent to your Webhook URL. The function returns an array of Update
objects.
$updates = $telegram->getWebhookUpdates();
// Put this inside the POST route /<token>/webhook or POST Controller
$updates = Telegram::getWebhookUpdates();
// Example of POST Route:
Route::post('/<token>/webhook', function () {
$updates = Telegram::getWebhookUpdates();
return 'ok';
});
Removing Webhook
To remove a webhook (if it was set before).
$response = $telegram->removeWebhook();
$response = Telegram::removeWebhook();
Getting Updates Manually
You can use getUpdates()
method to receive incoming updates using long polling (wiki). An Array of Update objects is returned.
See getUpdates docs for a list of supported parameters and other info.
$response = $telegram->getUpdates();
$response = Telegram::getUpdates();
Notes
This method will not work if an outgoing webhook is set up.
In order to avoid getting duplicate updates, recalculate offset after each server response.
Updated less than a minute ago