[DEPRECATED]
Laravel 5.3 Notifications to Telegram
, (*1)
Installation
PHP 5.6.4+ is required., (*2)
To get the latest version of Laravel Notifications Telegram, simply require the project using Composer:, (*3)
$ composer require alexsoft/laravel-notifications-telegram
Or you can manually update your require block and run composer update
if you choose so:, (*4)
{
"require": {
"alexsoft/laravel-notifications-telegram": "^0.1"
}
}
You will also need to install guzzlehttp/guzzle
http client to send request to Telegram API., (*5)
Once Laravel Notifications Telegram is installed, you need to register the service provider. Open up config/app.php
and add the following to the providers
key., (*6)
Alexsoft\LaravelNotificationsTelegram\ServiceProvider::class
Configuration
Telegram Bot API Token
First, talk to @BotFather and generate one.
Then put it to config/services.php
configuration file. You may copy the example configuration below to get started:, (*7)
'telegram-notifications-bot-token' => [
'key' => env('TELEGRAM_BOT_API_TOKEN')
]
Routing Telegram notifications
In order to send notifications to telegram, you need specify Telegram chat_id of notifiable entity. To provide library with correct chat id, you need to define routeNotificationForTelegram
method on the entity:, (*8)
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* Route notifications for the Nexmo channel.
*
* @return string
*/
public function routeNotificationForTelegram()
{
return $this->telegram_user_id;
}
}
Usage
via
method
On notification entity just add 'telegram'
item to array that is returned from via
method., (*9)
toTelegram
method
Also you should define toTelegram
method on notifications class. This method will receive a $notifiable
entity and should return a Alexsoft\LaravelNotificationsTelegram\TelegramMessage
instance.
Telegram messages may contain lines of text as well as a "call to action", just like Mail notification messages that are available in Laravel out of the box., (*10)
/**
* Get the telegram representation of the notification.
*
* @param mixed $notifiable
* @return \Alexsoft\LaravelNotificationsTelegram\TelegramMessage
*/
public function toTelegram($notifiable)
{
$url = url('/invoice/' . $this->invoice->id);
return (new TelegramMessage)
->line('One of your invoices has been paid!')
->action('View Invoice', $url)
->line('Thank you for using our application!');
}
Success, info or error?
Telegram notifications also support success and error notifications.
Just call the necessary method., (*11)
/**
* Get the telegram representation of the notification.
*
* @param mixed $notifiable
* @return \Alexsoft\LaravelNotificationsTelegram\TelegramMessage
*/
public function toTelegram($notifiable)
{
$url = url('/invoice/' . $this->invoice->id);
return (new TelegramMessage)
->error() // or success()
->line('Invoice could not be paid!')
->action('View Invoice', $url);
}
Credits
License
Laravel Notifications Telegram is licensed under The MIT License (MIT)., (*12)