serjoga/telegram

Telegram 通知驱动器

0.0.3 2017-08-16 18:55 UTC

This package is not auto-updated.

Last update: 2024-09-21 14:23:51 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

此包使用 Laravel 5.3 和 Telegram Bot API 简化了发送 Telegram 通知的过程。

内容

安装

您可以通过 composer 安装此包

composer require laravel-notification-channels/telegram

您必须安装服务提供者

// config/app.php
'providers' => [
    ...
    NotificationChannels\Telegram\TelegramServiceProvider::class,
],

设置您的 Telegram 机器人

@BotFather 交流并生成一个 Bot API Token。

然后,配置您的 Telegram Bot API Token

// config/services.php
...
'telegram-bot-api' => [
    'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
],
...

用法

现在您可以在 Notification 类的 via() 方法中使用该频道。

use NotificationChannels\Telegram\TelegramChannel;
use NotificationChannels\Telegram\TelegramMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return [TelegramChannel::class];
    }

    public function toTelegram($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return TelegramMessage::create()
            ->to($this->user->telegram_user_id) // Optional.
            ->content("*HELLO!* \n One of your invoices has been paid!") // Markdown supported.
            ->button('View Invoice', $url); // Inline Button
    }
}

以下是上述通知在 Telegram 消息应用中的截图预览

Laravel Telegram Notification Example

路由消息

您可以通过提供收件人的 chat id 给 to($chatId) 方法来发送通知,如上述示例所示,或者在你的 notifiable 模型中添加一个 routeNotificationForTelegram() 方法

...
/**
 * Route notifications for the Telegram channel.
 *
 * @return int
 */
public function routeNotificationForTelegram()
{
    return $this->telegram_user_id;
}
...

可用的消息方法

  • to($chatId): (integer) 收件人的 chat id。
  • content(''): (string) 通知消息,支持 markdown。有关支持的 markdown 样式的更多信息,请参阅这些 文档
  • button($text, $url): (string) 添加一个内联的“操作”按钮。您可以添加任意多个,它们将以两行为单位排列。
  • options([]): (array) 允许您添加或覆盖 sendMessage 负载(用于内部发送消息的 Telegram Bot API 方法)。有关支持的参数的更多信息,请参阅这些 文档

替代方案

对于高级使用,请考虑使用 telegram-bot-sdk

变更日志

有关最近更改的更多信息,请参阅 变更日志

测试

$ composer test

安全

如果您发现任何安全相关的问题,请通过电子邮件syed@lukonet.com报告,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 贡献

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件