Skip to content

Getting updates via Webhook

Asrul Hanafi edited this page Dec 27, 2018 · 5 revisions

The first thing you'll have to do is register a webhook with Telegram via the SetWebhook method:

use \React\EventLoop\Factory;
use \unreal4u\TelegramAPI\HttpClientRequestHandler;
use \unreal4u\TelegramAPI\Telegram\Methods\GetUpdates;
use \unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
use \unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Telegram\Methods\SetWebhook;

$loop = Factory::create();

$setWebhook = new SetWebhook();
$setWebhook->url = '[YOUR HTTPS URL]';

$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog->performApiRequest($setWebhook);
$loop->run();

This will leave you prepared to start receiving updates on the chosen URL:

use \unreal4u\TelegramAPI\Telegram\Types\Update;

// Getting POST request body and decoding it from JSON to associative array
$updateData = json_decode(file_get_contents('php://input'), true);

$update = new Update($updateData);

Now $update will contain the actual Update object. Hope that wasn't too difficult :)

More information on this? You can check how I implemented my timeBot. Take however into account that the cited repo is only a playground (for now), so it can happen that things in that repository may or may not work as expected.