juliuspc / telegram-notification-bot
一个简单的库,通过Telegram机器人API通知用户。
0.1.6
2020-05-10 15:38 UTC
Requires
- php: ^7.0
- ext-json: *
- ext-pcre: *
- ext-pdo: *
- guzzlehttp/guzzle: ~6.0
This package is auto-updated.
Last update: 2024-09-19 22:43:14 UTC
README
此库在Telegram的机器人API之上提供了一个薄层。它让您免于跟踪已知的chat_id
(=已订阅的用户)。仍然需要基本理解机器人API的工作原理。
您可以使用它将RSS源中提取的新闻广播到不同的聊天(频道)。它使用SQLite数据库(其他数据库可能也可以工作,只需更改PDO构造函数中的SQLite DSN)来持久化一些数据。如果机器人被从群聊中移除,收到/stop
命令或被用户阻止,它将从数据库中删除这些chat_ids
。
使用提示
- 安装库
- 通过 Composer 安装:
composer require juliuspc/telegram-notification-bot
- 克隆或下载此存储库,使用
composer install
安装依赖项
- 通过 Composer 安装:
- 获取机器人访问令牌
- 使用此库编写您的代码
手动安装时导入库...
require __DIR__ . '/TelegramBot.php'; use JuliusPC\TelegramBot;
... 或简单地使用Composer
require __DIR__ . '/vendor/autoload.php'; use JuliusPC\TelegramBot;
配置机器人
$dbh = new \PDO('sqlite:/path/to/botdb.sqlite3'); $token = 'YOUR_ACCESS_TOKEN_HERE'; $bot = new TelegramBot($dbh, $token); // adjust the bots defaults to your need. $bot->setWelcomeMessage('Moin!'); $bot->setStopMessage('Ciao.');
通过webhooks获取更新(推荐)
设置webhooks(您只需这样做一次或更改后)
// set webhook $bot->setWebhook('https://example.org/someobscurefolder/webhook.php'); // some other useful stuff // remove webhook $bot->deleteWebhook(); // get infos about configured webhooks echo $bot->getWebhookInfo();
将此放入文件 /someobscurefolder/webhook.php
中(不要这样命名 😉)
// process update from webhook $update = json_decode( file_get_contents("php://input"), true ); echo $bot->processUpdate($update);
通过轮询获取更新
定期运行此操作
// process Updates in case not using webhooks $bot->processUpdates($bot->getUpdates());
发送消息
第二个参数允许您在之后识别发送的消息。
// send broadcast to all users $bot->sendBroadcastMessage('Hello from <b>'.date('H:i').'</b>', '');
编辑和删除消息
您还可以编辑和删除广播消息。为此,您需要传递一个参数来识别发送的消息,并允许库记住chat_id
和message_id
。
以下示例显示了一个广播的自毁消息(编辑和删除命令不需要在同一个TelegramBot实例上执行,因为数据已写入数据库,因此是持久的)
$seconds = 30; echo $bot->sendBroadcastMessage('self destructing in '.$seconds.' seconds', 'some-arbitrary-string') . ' abonnierte Chats'; while($seconds > 0) { sleep(5); $seconds -= 5; $bot->editBroadcastMessage('self destructing in '.$seconds.' seconds', 'some-arbitrary-string'); } echo $bot->deleteBroadcastMessage('some-arbitrary-string');
处理命令
让Telegram客户端知道您的机器人理解哪些命令(再次通过webhooks:您只需这样做一次或更改后)
$commands = [ [ 'command'=>'start', 'description'=>'starts bot' ], [ 'command'=>'stop', 'description'=>'stops bot' ] ]; $bot->setMyCommands(json_encode($commands));
如果您想实现自己的命令,最简单的方法是扩展使用TelegramBot
类。在这个小例子中,我们添加了一个回显消息的命令
class AdvancedBot extends TelegramBot { protected function executeCommand(string $command, array $update) : bool { $id = $update['message']['chat']['id']??''; switch ($command) { case 'echo': $message = \json_encode($update, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); return $this->sendMessage('<pre>'.htmlspecialchars($message).'</pre>', $id); case 'rickroll': return $this->sendMessage('<a href="https://www.youtube.com/watch?v=DLzxrzFCyOs">Very important information</a>', $id); default: // let the parent method deal with commands like /stop /start return parent::executeCommand($command, $update); } } } // of course you need to instantiate and use your new class and not the old one... $bot = new AdvancedBot($db_handle, $token)