use NotificationChannels\Telegram\TelegramUpdates;
// Response is an array of updates.
$updates = TelegramUpdates::create()
// (Optional) Get the latest update.
// NOTE: All previous updates will be forgotten using this method.
// ->latest()
// (Optional) Limit to 2 updates. By default, updates start with the earliest unconfirmed update.
->limit(2)
// (Optional) Add more request parameters.
->options([
'timeout' => 0,
])
->get();
if ($updates['ok']) {
// Chat ID
$chatId = $updates['result'][0]['message']['chat']['id'];
}
public function toTelegram($notifiable)
{
return TelegramMessage::create()
->to($notifiable->telegram_user_id)
->content('Choose an option:')
->keyboard('Button 1')
->keyboard('Button 2');
}
TelegramMessage::create()
->content('Please share your phone number or location')
->keyboard('Send your number', requestContact: true)
->keyboard('Send your location', requestLocation: true);
use NotificationChannels\Telegram\TelegramDice;
public function toTelegram($notifiable)
{
return TelegramDice::create()
->to($notifiable->telegram_user_id)
->emoji('🎯');
}
public function toTelegram($notifiable)
{
return TelegramPoll::create()
->to($notifiable->telegram_user_id)
->question('Which is your favorite Laravel Notification Channel?')
->choices(['Telegram', 'Facebook', 'Slack']);
// Other fluent helpers are also available:
// ->description('Voting closes in an hour')
// ->quiz(0)
// ->explanation('Telegram, of course!')
// ->isAnonymous(false)
// ->allowsMultipleAnswers()
// ->allowsRevoting()
// ->shuffleOptions()
// ->allowAddingOptions()
// ->hideResultsUntilCloses()
// ->membersOnly()
// ->countryCodes(['US', 'GB'])
// ->openPeriod(600)
// ->closeDate(now()->addHour()->getTimestamp())
// ->media(['type' => 'photo', 'media' => $url])
}
use NotificationChannels\Telegram\TelegramRichMessage;
public function toTelegram($notifiable)
{
return TelegramRichMessage::create()
->to($notifiable->telegram_user_id)
->markdown(<<<'MD'
# Invoice #1234 Paid
Thanks for your payment, we've credited your account.
[](tg://photo?id=receipt)
MD)
->media('receipt', ['type' => 'photo', 'media' => 'https://example.com/receipt.jpg'])
->button('View Invoice', url('/invoice/1234'));
// Or render a Blade view as the HTML content instead:
// ->view('invoices.telegram', ['invoice' => $notifiable->invoice])
}
use NotificationChannels\Telegram\TelegramRichMessageDraft;
$draft = TelegramRichMessageDraft::create()
->to($chatId)
->draftId($chatId) // Any non-zero integer, stable for this stream.
->canStop() // Optional: show a button that stops the generation.
->keepOnStop(); // Optional: keep the partial draft around when it's stopped.
$buffer = '';
foreach ($stream as $chunk) {
$buffer .= $chunk;
// Replaces the content of the draft with the answer so far.
$draft->markdown($buffer)->send();
}
// Turns the draft into a permanent message via `sendRichMessage`.
$draft->markdown($buffer)->finalize();
public function toTelegram($notifiable)
{
return TelegramContact::create()
->to($notifiable->telegram_user_id) // Optional
->firstName('John')
->lastName('Doe') // Optional
->phoneNumber('00000000');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id) // Optional
->content('Awesome *bold* text and [inline URL](http://www.example.com/)')
->showCaptionAboveMedia()
->file('/storage/archive/6029014.jpg', 'photo'); // local photo
}
use NotificationChannels\Telegram\Enums\FileType;
TelegramFile::create()
->url('https://samples-files.com/samples/images/jpg/1280-720-sample.jpg', FileType::Photo);
TelegramFile::create()
->fileId('AgACAgQAAxkDAAIBLGV3', FileType::Photo);
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id) // Optional
->content('Here is your PDF document')
->document('https://samples-files.com/samples/documents/pdf/sample-1-small-size.pdf');
}
$contents = file_get_contents('https://samples-files.com/samples/documents/pdf/sample-1-small-size.pdf');
TelegramFile::create()
->content('Did you know we can set a custom filename too?')
->document($contents, 'sample.pdf');
TelegramFile::create()
->document('Hello Text Document Content', 'hello.txt');
public function toTelegram($notifiable)
{
return TelegramLocation::create()
->to($notifiable->telegram_user_id)
->latitude('40.6892494')
->longitude('-74.0466891');
}
public function toTelegram($notifiable)
{
return TelegramLocation::create()
->to($notifiable->telegram_user_id)
->latitude('40.6892494')
->longitude('-74.0466891')
->horizontalAccuracy(25)
->livePeriod(300)
->heading(180)
->proximityAlertRadius(50);
}
public function toTelegram($notifiable)
{
return TelegramVenue::create()
->to($notifiable->telegram_user_id)
->latitude('38.8951')
->longitude('-77.0364')
->title('Grand Palace')
->address('Bangkok, Thailand');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id)
->content('Sample *video* notification!')
->video('https://samples-files.com/samples/video/mp4/sample2-720x480.mp4');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id)
->content('Woot! We can send animated gif notifications too!')
->animation('https://disk.sample.cat/samples/gif/sample-2.gif');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id)
->sticker(storage_path('telegram/AnimatedSticker.tgs'));
}
use NotificationChannels\Telegram\TelegramMediaGroup;
public function toTelegram($notifiable)
{
return TelegramMediaGroup::create()
->to($notifiable->telegram_user_id)
->photo('https://example.com/one.jpg', 'First image')
->photo('https://example.com/two.jpg');
}
/**
* Route notifications for the Telegram channel.
*
* @return int
*/
public function routeNotificationForTelegram()
{
return $this->telegram_user_id;
}
use Illuminate\Notifications\Events\NotificationFailed;
class HandleNotificationFailure
{
public function handle(NotificationFailed $event)
{
// $event->notification: The notification instance.
// $event->notifiable: The notifiable entity who received the notification.
// $event->channel: The channel name.
// $event->data: The data needed to process this failure.
if ($event->channel !== 'telegram') {
return;
}
// Log the error / notify administrator or disable notification channel for the user, etc.
\Log::error('Telegram notification failed', [
'chat_id' => $event->data['to'],
'error' => $event->data['exception']->getMessage(),
'request' => $event->data['request']
]);
}
}
public function toTelegram($notifiable)
{
return TelegramMessage::create()
->content('Hello!')
->onError(function ($data) {
\Log::error('Failed to send Telegram notification', [
'chat_id' => $data['to'],
'error' => $data['exception']->getMessage()
]);
});
}
use Illuminate\Support\Facades\Notification;
Notification::route('telegram', 'TELEGRAM_CHAT_ID')
->notify(new InvoicePaid($invoice));
use Illuminate\Support\Facades\Notification;
// Recipients can be an array of chat IDs or collection of notifiable entities.
Notification::send($recipients, new InvoicePaid());
use NotificationChannels\Telegram\Facades\Telegram;
Telegram::sendChatAction([
'chat_id' => $chatId,
'action' => 'typing',
]);