kaivalar/telegram_bot

Telegram机器人服务

0.2 2021-08-25 14:10 UTC

This package is auto-updated.

Last update: 2024-09-25 21:02:39 UTC


README

Telegram机器人服务

TelegramBotService

方法

sendMessageTo($chat_id, $message)

使用此方法向指定的聊天发送消息。

$bot = new TelegramBotService($token);
$result = $bot->sendMessageTo($chat_id, $message);

BotNotificationTemplateProcessor

方法

__construct($templates)

$templates 是一个关联数组,格式如下

[
   'template_name' => "Hello ~name~"
];

其中 ~name~ 是可以通过 renderTemplate 方法渲染的参数

renderTemplate($template_name, $parameters)

返回渲染后的模板

$args 是一个关联数组,格式如下

[
   'name' => 'Jhon'
];

抛出 NoSuchParameterExceptionNoSuchTemplateException

示例

$bot = new TelegramBotService($token);

$templates = [
    'example' => "Hello ~name~"
];
$attributes = [
    'name' => 'Jhon'
];

$templateProcessor = new BotNotificationTemplateProcessor($templates);
$message = $templateProcessor->renderTemplate('example', $attributes);

$result = $bot->sendMessageTo($chat_id, $message);

example.php 中查看完整示例