dmiseev / laravel-telegram-notification
适用于 Laravel 框架的 Telegram 通知
1.0.1
2018-01-25 23:15 UTC
Requires
- php: >=7.0
- guzzlehttp/guzzle: ^6.2
- illuminate/notifications: *
- illuminate/support: *
Requires (Dev)
- mockery/mockery: ^0.9.5
- orchestra/testbench: 3.4.x-dev
- phpunit/phpunit: 5.*
This package is auto-updated.
Last update: 2024-09-29 05:01:45 UTC
README
此包使得使用 Laravel 5.5 通过 Telegram Bot API 发送 Telegram 通知变得简单。
安装
您可以通过 composer 安装此包
composer require dmiseev/laravel-telegram-notification
设置您的 Telegram 机器人
与 @BotFather 进行对话并生成一个机器人 API 令牌。
然后,配置您的 Telegram 机器人 API 令牌
// config/services.php ... 'telegram' => [ 'token' => env('TELEGRAM_TOKEN', 'YOUR BOT TOKEN HERE') ], ...
使用方法
现在您可以在通知类中的 via()
方法内使用该频道。
use Dmiseev\TelegramNotification\TelegramChannel; use Dmiseev\TelegramNotification\TelegramMessage; use Illuminate\Notifications\Notification; class WithdrawCreate extends Notification { /** * @var Withdraw */ private $withdraw; /** * @var User */ private $user; /** * @param Withdraw $withdraw */ public function __construct(Withdraw $withdraw, User $user) { $this->withdraw = $withdraw; $this->user = $user; } public function via($notifiable) { return [TelegramChannel::class]; } public function toTelegram($notifiable) { return TelegramMessage::create() ->to($this->user->telegram_user_id) ->content("*HI!* \n One of your withdraws has been created!") ->button('View Withdraw', url('/withdraws/' . $this->withdraw->id)); } }
路由消息
您可以通过提供收件人的聊天 ID 到 to($chatId)
方法来发送通知,如上述示例所示,或者添加 routeNotificationForTelegram()
方法到您的可通知模型中
... /** * @return int */ public function routeNotificationForTelegram() { return $this->telegram_user_id; } ...
可用消息方法
to($chatId)
: (integer) 收件人的聊天 ID。content('')
: (string) 通知消息,支持 markdown。有关支持的 markdown 风格的更多信息,请参阅这些 文档。button($text, $url)
: (string) 添加内联 "行动号召" 按钮。您可以添加任意多个,并将它们以两行为单位放置。options([])
: (array) 允许您添加或覆盖sendMessage
有效负载(一个用于内部发送消息的 Telegram 机器人 API 方法)。有关支持的参数的更多信息,请参阅这些 文档。
安全性
如果您发现任何与安全性相关的问题,请通过电子邮件 dmiseev@gmail.com 联系,而不是使用问题跟踪器。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。