rakshazi / telegram-notify
从多个服务获取通知并发送到Telegram
1.1
2016-11-06 19:52 UTC
Requires
- ext-curl: *
- ext-iconv: *
- ext-imap: *
- ext-json: *
- ext-xml: *
- php-curl-class/php-curl-class: ^7.0
This package is auto-updated.
Last update: 2024-09-13 22:17:16 UTC
README
这个库从多个服务收集数据,将其转换(变换)并发送为Telegram消息。
用法
- 创建Telegram机器人 - 你只需要在这里输入token
- 将机器人添加到任何聊天或直接向其发送消息
- 打开URL
https://api.telegram.org/bot<BOT TOKEN>/getUpdates
(将<BOT TOKEN>
替换为你的机器人token)以获取你的聊天id(-s)。 - 为Telegram Notify库编写配置数组
<?php $config = [ 'token' => '<BOT TOKEN>', 'notifications' => [ [ 'chat_id' => '<YOUR CHAT ID>', 'parse_mode' => 'HTML', //currently supported only 'HTML' 'notify' => [ //List of integrations, you can add multiple accounts for each service and multiple services [ //RSS integration will post you messages with new items in feed 'type' => 'RSS', //Integration type 'source' => 'https://www.teamoctos.com/category/changelog/feed/' //Feed source URL ], [ 'type' => 'Email', //Works with any IMAP server 'source' => [ 'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX', //@link https://secure.php.net/manual/ru/function.imap-open.php 'user' => 'example@gmail.com', // your login 'password' => 'example-password' // your password ] ], [ 'type' => 'Trello', //Get all unread notifications from trello 'source' => [ 'api_key' => 'trello-app-key', //https://trello.com/app-key 'token' => 'trello-user-token' //https://developers.trello.com/authorize ], ] ], ], ], ];
好了!现在你需要创建一个函数,用来检查消息是否已经发送。
我使用mauris/packer(你需要dev-master
版本的exists
函数)作为已发送消息的存储,下面是它的样子
<?php //Arguments: chat id, message text //Must return bool $isSent = function (string $chatId, string $message) { $key = md5($chatId.$message); $sent = new \Packer\Packer('./sent.pack'); if ($sent->exists($key)) { return true; } $sent->write($key, $message); return false; };
非常好!最终的运行脚本应该是这样的(我用于我的私有集成)
<?php require './vendor/autoload.php'; $config = [ 'token' => 'toke', 'notifications' => [ [ 'chat_id' => 'chat_id', 'parse_mode' => 'HTML', 'notify' => [ [ 'type' => 'RSS', 'source' => 'https://www.teamoctos.com/category/changelog/feed/' ], [ 'type' => 'Email', 'source' => [ 'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX', 'user' => 'example@gmail.com', 'password' => 'example-password' ] ], [ 'type' => 'Trello', 'source' => [ 'api_key' => 'key', 'token' => 'token' ], ] ], ], ], ]; $isSent = function (string $chatId, string $message) { $key = md5($chatId.$message); $sent = new \Packer\Packer('./sent.pack'); if ($sent->exists($key)) { return true; } $sent->write($key, $message); return false; }; $notify = new \Rakshazi\TelegramNotify($config, $isSent); $notify->run();