Fake Telegram interaction
In order to avoid sending real messages to Telegram when testing, the Telegraph facade offers a ::fake() method to start registering sent messages internally instead of sending them to Telegram APIs.
use DefStudio\Telegraph\Facades\Telegraph; Telegraph::fake(); Telegraph::message("Hello devs!")->send(); // the message won be actually sent to telegram, but can still be asserted Telegraph::assertSent('Hello devs!');
For a webhook command flow, call Telegraph::fake() before the handler and assert the fake payload after handling. The first-bot tutorial has a complete /start example with a persisted TelegraphBot, a known TelegraphChat, and no real Telegram requests: Build your first Telegram bot with Telegraph.
use DefStudio\Telegraph\Facades\Telegraph; use DefStudio\Telegraph\Telegraph as TelegraphCore; Telegraph::fake(); // Handle the webhook request for /start... Telegraph::assertSentData(TelegraphCore::ENDPOINT_MESSAGE, [ 'chat_id' => '-123456789', 'text' => 'Hello! Telegraph is connected.', ]);
Custom responses
If needed for testing purpose, the ::fake() helper can accept an array of responses to be returned for each endpoint call:
Telegraph::fake([ \DefStudio\Telegraph\Telegraph::ENDPOINT_MESSAGE => ['result' => 'oooook'], ]); $response = Telegraph::message('foo')->send(); //$response will be a Response containing a json body: {"result":"oooook"}
Sent data dump
For debugging purpose, a dump of the sent data can be obtained with:
Telegraph::fake(); // Telegraph requests... Telegraph::dumpSentData();