use NotificationChannels\Telegram\TelegramUpdates;
// Response is an array of updates.
$updates = TelegramUpdates::create()
// (Optional). Get's the latest update.
// NOTE: All previous updates will be forgotten using this method.
// ->latest()
// (Optional). Limit to 2 updates (By default, updates starting with the earliest unconfirmed update are returned).
->limit(2)
// (Optional). Add more params to the request.
->options([
'timeout' => 0,
])
->get();
if($updates['ok']) {
// Chat ID
$chatId = $updates['result'][0]['message']['chat']['id'];
}
# bootstrap/app.php
// Make sure to create a "config/services.php" file and add the config from the above step.
$app->configure('services');
# Register the notification service providers.
$app->register(Illuminate\Notifications\NotificationServiceProvider::class);
$app->register(NotificationChannels\Telegram\TelegramServiceProvider::class);
use NotificationChannels\Telegram\TelegramMessage;
use Illuminate\Notifications\Notification;
class InvoicePaid extends Notification
{
public function via($notifiable)
{
return ["telegram"];
}
public function toTelegram($notifiable)
{
$url = url('/invoice/' . $notifiable->invoice->id);
return TelegramMessage::create()
// Optional recipient user id.
->to($notifiable->telegram_user_id)
// Markdown supported.
->content("Hello there!")
->line("Your invoice has been *PAID*")
->lineIf($notifiable->amount > 0, "Amount paid: {$notifiable->amount}")
->line("Thank you!")
// (Optional) Blade template for the content.
// ->view('notification', ['url' => $url])
// (Optional) Inline Buttons
->button('View Invoice', $url)
->button('Download Invoice', $url);
// (Optional) Conditional notification.
// Only send if amount is greater than 0. Otherwise, don't send.
// ->sendWhen($notifiable->amount > 0)
// (Optional) Inline Button with Web App
// ->buttonWithWebApp('Open Web App', $url)
// (Optional) Inline Button with callback. You can handle callback in your bot instance
// ->buttonWithCallback('Confirm', 'confirm_invoice ' . $this->invoice->id)
}
}
public function toTelegram($notifiable)
{
return TelegramPoll::create()
->to($notifiable)
->content('Choose an option:')
->keyboard('Button 1')
->keyboard('Button 2');
// ->keyboard('Send your number', requestContact: true)
// ->keyboard('Send your location', requestLocation: true);
}
public function toTelegram($notifiable)
{
return TelegramPoll::create()
->to($chatId)
->question('What is your favorite programming language?')
->choices(['PHP', 'Python', 'JavaScript', 'Java', 'C#']);
}
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('Audio') // Optional Caption
->audio('/path/to/audio.mp3');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id) // Optional
->content('Awesome *bold* text and [inline URL](http://www.example.com/)')
->file('/storage/archive/6029014.jpg', 'photo'); // local photo
// OR using a helper method with or without a remote file.
// ->photo('https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_1MB.jpg');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->to($notifiable->telegram_user_id) // Optional
->content('Did you know we can set a custom filename too?')
->document('https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf', 'sample.pdf');
}
public function toTelegram($notifiable)
{
return TelegramLocation::create()
->latitude('40.6892494')
->longitude('-74.0466891');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->content('Sample *video* notification!')
->video('https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4');
}
public function toTelegram($notifiable)
{
return TelegramFile::create()
->content('Woot! We can send animated gif notifications too!')
->animation('https://sample-videos.com/gif/2.gif');
// Or local file
// ->animation('/path/to/some/animated.gif');
}
/**
* 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());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.