mrjavadseydi/notif-telegram

Laravel 的 Telegram 通知通道

dev-main 2022-01-20 08:27 UTC

This package is auto-updated.

Last update: 2024-09-20 14:41:15 UTC


README

Join PHP Chat Chat on Telegram Laravel Package Latest Version on Packagist Software License SensioLabsInsight Quality Score Total Downloads

此包通过 Laravel 使用 Telegram Bot API 发送 Telegram 通知变得简单。

内容

安装

您可以通过 composer 安装此包

composer require laravel-notification-channels/telegram

设置您的 Telegram 机器人

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

然后,配置您的 Telegram Bot API Token

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

用法

现在,您可以在通知类的 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()
            // Optional recipient user id.
            ->to($notifiable->telegram_user_id)
            // Markdown supported.
            ->content("Hello there!\nYour invoice has been *PAID*")
            // (Optional) Inline Buttons
            ->button('View Invoice', $url)
            ->button('Download Invoice', $url);
    }
}

以下是 Telegram 消息器上上述通知的截图预览

Laravel Telegram Notification Example

附加照片

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

    return TelegramFile::create()
        ->to($notifiable->telegram_user_id)
        ->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/wp-content/uploads/2017/10/file_example_JPG_1MB.jpg');
}

预览

Laravel Telegram Photo Notification Example

附加文档

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

    return TelegramFile::create()
        ->to($notifiable->telegram_user_id)
        ->content('Did you know we can set a custom filename too?')
        ->document('https://file-examples.com/wp-content/uploads/2017/10/file-sample_150kB.pdf', 'sample.pdf');
}

预览

Laravel Telegram Document Notification Example

附加位置

public function toTelegram($notifiable)
{
    return TelegramLocation::create()
        ->to($notifiable->telegram_user_id)
        ->latitude('40.6892494')
        ->longitude('-74.0466891');
}

预览

Laravel Telegram Location Notification Example

附加视频

public function toTelegram($notifiable)
{
    return TelegramFile::create()
        ->to($notifiable->telegram_user_id)
        ->content('Sample *video* notification!')
        ->video('https://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_480_1_5MG.mp4');
}

预览

Laravel Telegram Video Notification Example

附加 Gif 文件

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');
}

预览

Laravel Telegram Gif Notification Example

路由消息

您可以通过提供接收者的聊天 ID 到 to($chatId) 方法来发送通知,如上例所示,或者添加一个 routeNotificationForTelegram() 方法到您的可通知模型中

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

可用的消息方法

  • to($chatId): (integer) 收件人的聊天 ID。
  • content(''): (string) 通知消息,支持 Markdown。有关支持的 Markdown 样式信息,请查看这些 文档
  • button($text, $url): (string) 添加一个内联“行动按钮”。您可以添加任意多个,并将它们以两行为单位排列。
  • disableNotification(): 静默发送消息。用户将收到通知,但没有声音。
  • options([]): (array) 允许您添加或覆盖 sendMessage 有效负载(用于发送消息的 Telegram Bot API 方法)。有关支持的参数信息,请查看这些 文档

可用的位置方法

  • to($chatId): (integer) 收件人的聊天 ID。
  • latitude($latitude): (float|string) 位置的纬度。
  • longitude($longitude): (float|string) 位置的经度。
  • button($text, $url): (string) 添加一个内联“行动按钮”。您可以添加任意多个,并将它们以两行为单位排列。
  • disableNotification(): 静默发送消息。用户将收到通知,但没有声音。
  • options([]): (array) 允许您添加或覆盖有效负载。

可用的文件方法

  • to($chatId): (integer) 收件人的聊天 ID。
  • content(''): (string) 文件标题,支持 Markdown。有关支持的 Markdown 样式信息,请查看这些 文档
  • file($file, $type, $filename = null): 本地文件路径或远程 URL,$type 的文件(例如:photoaudiodocumentvideoanimationvoicevideo_note_)和可选的文件名及扩展名。例如:sample.pdf。您可以使用辅助方法来简化文件附加操作。
  • photo($file): 用于附加照片的辅助方法。
  • audio($file): 用于附加音频文件的辅助方法(MP3 文件)。
  • document($file, $filename = null): 用于附加文档或任何文件的辅助方法。
  • video($file): 用于附加视频文件的辅助方法。
  • animation($file): 用于附加动画 Gif 文件的辅助方法。
  • voice($file): 用于附加语音笔记的辅助方法(.ogg 文件,使用 OPUS 编码)。
  • videoNote($file): 用于附加视频笔记文件的辅助方法(最长 1 分钟,方形视频)。
  • button($text, $url): (string) 添加一个内联“行动按钮”。您可以添加任意多个,并将它们以两行为单位排列。
  • disableNotification(): 静默发送消息。用户将收到通知,但没有声音。
  • options([]): (array) 允许您添加或覆盖有效负载。

替代方案

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

更新日志

请参阅变更日志获取最近更改的更多信息。

测试

$ composer test

安全

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

贡献

请参阅贡献指南获取详细信息。

致谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。