vaizard / glued-lib

Glued 微服务的通用代码。

dev-main 2024-09-17 05:35 UTC

This package is auto-updated.

Last update: 2024-09-17 05:35:37 UTC


README

glued 项目的工具包和依赖抽象库。

通知

notify 类在大多数情况下使用 symfony/notifier 库,但提供了足够的抽象,以方便其他代码在不支持 symfony 的平台上进行集成。

该类需要一个 yaml 配置,其中每个网络的 dst 都是一个管理员的目的地址(接收者地址)。在每个网络(例如 email)中,可以使用多个(发送)通道。目前,如果第一个通道发送失败,该类将尝试通过其他通道继续发送。这种故障转移机制(故意)由于隐私/安全问题,在网络之间不起作用。

对于管理员,使用所有网络上的所有目的地以确保管理员真正收到通知。对于用户,用户可以定义一个或多个始终使用的通知通道和只能由用户选择的额外通道。

配置

要配置通知,请覆盖 defaults.yaml。以下是一个示例

notify:
    network:
        telegram:
            channels:
                - name: Telegram
                  dsn: 'telegram://123456789:yourbotsecret@default?channel='
            dst:
                - '111111111'
                - '222222222'
            config:
                src: '@MyTelegramBot'
        email:
            channels:
                - name: E-mail (smtp)
                  dsn: 'smtp://login:pass@mx1.example.com:587?encryption=starttls'
                - name: E-mail (smtp2)
                  dsn: 'smtp://login:pass@mx2.example.com:587?encryption=starttls'
            config:
                src: 'sender@example.com'
            dst:
                - 'recipient@gmail.com'
                - 'someotheradmin@outlook.com'
        sms:
            channels:
                - name: Twilio
                  dsn: 'twilio://SID:TOKEN@default?from=FROM'
                - name: O2
            config:
                src: '+1123456789'

用法

向管理员发送通知

$this->notify->send(content: 'this is the message', subject: 'this is the optional header (i.e. mail subject', notify_admins: true);

向特定用户发送通知(以下所有通道都将使用)

$users = [
    '45216b0c-32ca-4a48-8307-3466ee81f32e' => [
        'telegram'  => '222333222',
        'email' => 'user@example.com',
    ],
    '8e51819a-4595-4e5c-990c-f27cacb1a2dd' => [
        'telegram' => '2222777777',
    ],
];
$this->notify->to($users)->send('A quick notification only sent to $users');

该类的辅助函数将

$res = $this->notify->getusers();
echo "<br><br>All users to be notified:";
print_r($res); 

$res = $this->notify->getadmins();
echo "<br><br>All admins to be notified:";
print_r($res); 

$res = $this->notify->status();
print_r($res);
echo "<br><br>Current configuration and send queue:";

支持的网络/通道是支持的

目前只支持以下网络/通道

  • TELEGRAM
  • EMAIL (SMTP)

TELEGRAM

转到 https://web.telegram.org/k/#@BotFather 并输入

/newbot         # start setup
glued-dev       # bot name
glued_dev_bot   # bot username

转到 https://web.telegram.org/k/#@RawDataBot 以获取接收者的 chat_id

最终结果可能如下

  • 令牌: 1234567890:asecretstringapproximatelythisloong
  • Uri: https://web.telegram.org/k/#@glued_dev_bot
  • Chat_id: 2244668800

以及一个 DSN 字符串 TELEGRAM_DSN=telegram://1234567890:asecretstringapproximatelythisloong@default?channel=2244668800

要测试上述内容是否工作,请使用以下 curl 命令

TG_TOKEN=secret-token
TG_CHAT_ID=recipients-chat-id
curl -X POST -H "Content-Type:multipart/form-data" -F chat_id=$TG_CHAT_ID -F text="message" "https://api.telegram.org/bot$TG_TOKEN/sendMessage"

EMAIL

要测试,请使用以下 curl 命令

SMTP_HOST='mail.example.com:587'
SMTP_USER='login:pass'
SMTP_FROM='sender@example.com'
SMTP_RCPT='recipient@example.com'

curl --ssl-reqd --url "smtp://$SMTP_HOST" --user "$SMTP_USER" --mail-from "$SMTP_FROM" --mail-rcpt "$SMTP_RCPT" --upload-file file.txt
cat file.txt
From: <test@noreply.com>
To: <pavel@vaizard.org>
Subject: Curl Test