babenkoivan/telegram-notifications

Laravel 的 Telegram 通知

v1.1.0 2021-11-10 08:29 UTC

This package is auto-updated.

Last update: 2024-09-10 14:56:52 UTC


README

Packagist Packagist license

该包提供了一种简单的方法,可以将 Telegram 通知发送到项目中任何 可通知实体。它使用官方的 Telegram Bot API 将您的消息直接发送到用户。您可以发送任何想要的信息:文本、媒体、位置或联系人。

内容

要求

该包已在以下配置上进行了测试

  • PHP 版本 >= 7.3
  • Laravel 框架版本 >= 5.5

安装

要安装该包,您可以使用 composer

composer require babenkoivan/telegram-notifications

如果禁用了包发现,您需要在 config/app.php 文件中注册服务提供者

'providers' => [
    TelegramNotifications\TelegramServiceProvider::class    
]

要复制包设置到 config 目录,请运行

php artisan vendor:publish --provider='TelegramNotifications\TelegramServiceProvider'

现在您可以为应用程序设置一个机器人令牌。如果您还没有创建机器人,您可以使用 BotFather 创建一个新的。有关更多信息,请访问 Bots: An introduction for developers 页面。

让我们继续并假设您有一个令牌。您可以在 .env 文件或 config/telegram.php 文件中配置令牌

TELEGRAM_BOT_TOKEN=335237666:FFF45pYTYm9HkKWByaepSpcKAUWMo2uMF_9

当然,上面的令牌只是一个示例,您需要指定自己的令牌。

<?php

return [
    'bot_token' => '335237666:FFF45pYTYm9HkKWByaepSpcKAUWMo2uMF_9',
];

当然,上面的令牌只是一个示例,您需要指定自己的令牌。

设置您的模型

要通知用户或任何其他可通知实体,您需要使用与您的模型一起使用的 Notifiable 特性,并定义 routeNotificationForTelegram 方法,该方法将返回一个 chat_id

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    // ...

    public function routeNotificationForTelegram()
    {
        return 993344556;
    }
}

此时,您可能会想知道在哪里获取 chat_id。答案是这取决于您!您可以为您的机器人创建一个 webhook 来接收更新并收集聊天 id,或者您可以为特定用户手动指定 id。

要开始,您可以将 Hello! 消息发送到您的机器人,然后通过请求 API 方法 获取消息详情

curl https://api.telegram.org/bot<your_token_here>/getUpdates

您将收到一个 JSON 响应

{
    "ok": true,
    "result": [
        {
            "message": {
                "chat": {
                    "id": 993344556 // this is what we were looking for 
                    // ...
                }
            }
        }
    ]
}

使用示例

如果您 安装了该包配置了模型,您就可以准备发送您的第一个 Telegram 通知了。您可以使用 artisan 命令创建一个新的通知

php artisan make:notification TelegramNotification

再次提醒,TelegramNotification 这里只是一个示例,您可以指定任何您想要的名称。

现在,您可以去 app/Notifications 文件夹,您会看到 TelegramNotification.php 文件。在 via 方法中指定 TelegramChannel::class,并在 toTelegram 方法中初始化一个新的 TelegramMessage 实例

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

use TelegramNotifications\TelegramChannel;
use TelegramNotifications\Messages\TelegramMessage;

class TelegramNotification extends Notification
{
    use Queueable;

    public function via()
    {
        return [TelegramChannel::class];
    }

    public function toTelegram()
    {
        return (new TelegramMessage())->text('Hello, world!');
    }
}

要发送通知,请使用与可通知实体一起的 notify 方法。

假设我们有一个经过身份验证的用户,并希望从路由回调发送消息。我们可以这样做

<?php

use \App\Notifications\TelegramNotification;

Route::post('/', function () {
    Auth::user()->notify(new TelegramNotification());
});

高级使用

您一次可以发送单个消息或消息集合。

单条消息

每个消息类代表您可以发送给用户的信息类型。要发送消息,请从 toTelegram 方法返回必要类型的新的实例

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

use TelegramNotifications\TelegramChannel;
use TelegramNotifications\Messages\TelegramMessage;

class TelegramNotification extends Notification
{
    use Queueable;

    public function via()
    {
        return [TelegramChannel::class];
    }

    public function toTelegram()
    {
        // to set any required or optional field use
        // setter, which name is field name in camelCase
        return (new TelegramMessage())
            ->text('Hello, world!')
            ->disableNotification(true);
    }
}

您也可以向构造函数传递参数,以使其更明确

<?php

new TelegramMessage([
    'text' => 'Hello, world!',
    'disable_notification' => true
]);

以下列出了可用的消息类型。

TelegramMessage

TelegramNotifications\Messages\TelegramMessage

TelegramPhoto

TelegramNotifications\Messages\TelegramPhoto

TelegramAudio

TelegramNotifications\Messages\TelegramAudio

TelegramDocument

TelegramNotifications\Messages\TelegramDocument

TelegramSticker

TelegramNotifications\Messages\TelegramSticker

TelegramVideo

TelegramNotifications\Messages\TelegramVideo

TelegramVoice

TelegramNotifications\Messages\TelegramVoice

TelegramLocation

TelegramNotifications\Messages\TelegramLocation

TelegramVenue

TelegramNotifications\Messages\TelegramVenue

TelegramContact

TelegramNotifications\Messages\TelegramContact

消息集合

您也可以使用 TelegramCollection 发送多个消息,而不是一次性发送一个消息

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;

use TelegramNotifications\TelegramChannel;
use TelegramNotifications\Messages\TelegramCollection;

class TelegramNotification extends Notification
{
    use Queueable;

    public function via()
    {
        return [TelegramChannel::class];
    }

    public function toTelegram()
    {
        return (new TelegramCollection())
            ->message(['text' => 'Hello, world!'])
            ->location(['latitude' => 55.755768, 'longitude' => 37.617671])
            // ...
            ->sticker(['sticker' => 'CAADBQADJwEAAl7ylwK4Q0M5P7UxhQI']);
    }
}

集合中的每个方法都创建相应的消息实例并将其放入集合中。以下列出了可用方法