alimousavi/telenotify
Composer 安装命令:
composer require alimousavi/telenotify
包简介
A Laravel package for sending notifications via Telegram.
README 文档
README
TeleNotify is a Laravel notification channel for sending messages via Telegram bots.
Installation
You can install the package via composer:
composer require alimousavi/telenotify
Optionally, you can publish the configuration file:
php artisan vendor:publish --provider="AliMousavi\TeleNotify\TeleNotifyServiceProvider"
This will publish a configuration file named telenotify.php where you can set your Telegram bot API token.
Usage
Step 1: Add Telegram Bot Token
Add your Telegram bot token to your .env file:
TELEGRAM_BOT_API_TOKEN=your-bot-api-token
Step 2: Implement TeleNotifiable Interface
Your notifiable model (e.g., User, Employee) should implement the TeleNotifiable interface and provide a method getTelegramChatId():
use Alimousavi\TeleNotify\Contracts\TeleNotifiableInterface; class User implements TeleNotifiableInterface { // ... public function getTelegramChatId(): string|int|null { return $this->telegram_chat_id; } }
Note: Using TeleNotifiable Trait
If your notifiable model already has a telegram_chat_id property, you can simplify implementation by using the TeleNotifiable trait:
use Alimousavi\TeleNotify\Contracts\TeleNotifiableInterface; use Alimousavi\TeleNotify\Traits\TeleNotifiable; class User implements TeleNotifiableInterface { use TeleNotifiable; // ... }
Using the trait is optional but recommended if your notifiable model includes the telegram_chat_id property to adhere to the TeleNotifiableInterface requirements.
Step 3: Use TelegramChannel in Notifications
In your notification class, use the TelegramChannel::class in the via() method and implement toTelegram() method:
use Illuminate\Notifications\Notification; use Alimousavi\TeleNotify\Channels\TelegramChannel; use Alimousavi\TeleNotify\Contracts\TeleNotifiable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Bus\Queueable; class ExampleNotification extends Notification implements ShouldQueue { use Queueable; public function via($notifiable) { return [TelegramChannel::class]; } public function toTelegram(TeleNotifiable $notifiable) { return [ 'text' => 'Hello, this is a test message from TeleNotify!', ]; } }
I highly recommend implementing ShouldQueue and using the Queueable trait to optimize performance and enhance the responsiveness of your notification system by leveraging Laravel's queueing capabilities.
Step 4: Example Messages
Example 1: Simple Text Message
public function toTelegram(TeleNotifiable $notifiable) { return [ 'text' => 'Hello, this is a simple text message!', ]; }
Example 2: Message with Parse Mode
public function toTelegram(TeleNotifiable $notifiable) { return [ 'text' => 'Hello, this message uses *Markdown* parse mode.', 'parse_mode' => 'Markdown', ]; }
Example 3: Message with Disable Notification
public function toTelegram(TeleNotifiable $notifiable) { return [ 'text' => 'Hello, this message will not trigger a notification.', 'disable_notification' => true, ]; }
For more customization options, refer to Telegram Bot API documentation.
Contributing
Contributions are welcome!
License
This package is open-sourced software licensed under the MIT license.
alimousavi/telenotify 适用场景与选型建议
alimousavi/telenotify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 317 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 alimousavi/telenotify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alimousavi/telenotify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 317
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-28