alexsoft / laravel-notifications-telegram
Requires
- php: >=5.6.4
- illuminate/events: ^5.3@dev
- illuminate/notifications: ^5.3@dev
- illuminate/support: ^5.3@dev
Requires (Dev)
- mockery/mockery: ^0.9.5
- phpunit/phpunit: 4.*
Suggests
- laravel-notification-channels/telegram: This package is deprecated. Please use laravel-notification-channels vendor
This package is auto-updated.
Last update: 2022-02-01 13:00:30 UTC
README
请使用https://github.com/laravel-notification-channels/telegram
Laravel 5.3 通知到 Telegram
安装
PHP 5.6.4+ 是必需的。
要获取 Laravel Notifications Telegram 的最新版本,只需使用 Composer 引入项目。
$ composer require alexsoft/laravel-notifications-telegram
或者,如果您愿意,可以手动更新您的 require 块并运行 composer update
。
{ "require": { "alexsoft/laravel-notifications-telegram": "^0.1" } }
您还需要安装 guzzlehttp/guzzle
HTTP 客户端以向 Telegram API 发送请求。
一旦安装了 Laravel Notifications Telegram,您需要注册服务提供者。打开 config/app.php
并将以下内容添加到 providers
键。
Alexsoft\LaravelNotificationsTelegram\ServiceProvider::class
配置
Telegram Bot API 令牌
首先,与 @BotFather 联系并生成一个。然后将其放入 config/services.php
配置文件中。您可以将下面的示例配置复制粘贴以开始使用
'telegram-notifications-bot-token' => [ 'key' => env('TELEGRAM_BOT_API_TOKEN') ]
路由 Telegram 通知
为了将通知发送到 Telegram,您需要指定可通知实体的 Telegram chat_id。为了向库提供正确的 chat_id,您需要在实体上定义 routeNotificationForTelegram
方法。
<?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; } }
用法
via
方法
在通知实体上,只需将 'telegram'
项添加到从 via
方法返回的数组中。
toTelegram
方法
您还应该在通知类上定义 toTelegram
方法。此方法将接收一个 $notifiable
实体,并应返回一个 Alexsoft\LaravelNotificationsTelegram\TelegramMessage
实例。Telegram 消息可以包含文本行以及“行动号召”,就像 Laravel 中可用的邮件通知消息一样。
/** * 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!'); }
成功、信息或错误?
Telegram 通知也支持成功和错误通知。只需调用相应的方法。
/** * 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); }
致谢
许可证
Laravel Notifications Telegram 项目遵循MIT 许可协议 (MIT)。