alverspb / teletant
Telegram机器人框架
1.5.0
2024-08-19 07:30 UTC
Requires
- php: ^8.2
- ext-json: *
- ext-mbstring: *
- askoldex/formatter: 1.0.3
- guzzlehttp/guzzle: 7.9.2
- psr/log: ^1.1.3
README
作者:@askoldex,@uniqkic
灵感来源:telegraf,irazasyed/telegram-bot-sdk
示例
入门
要求
- PHP 7.2
- Composer
安装
composer require askoldex/teletant
使用方法
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; use Askoldex\Teletant\Bot; use Askoldex\Teletant\Context; use Askoldex\Teletant\Settings; $settings = new Settings('token'); $settings->setHookOnFirstRequest(false); $bot = new Bot($settings);
以长轮询(getUpdates)运行
$bot->polling();
以webhook(监听webhook地址)运行
$bot->listen();
快速示例
制作命令
$bot->onCommand('start', function (Context $ctx) { $ctx->reply('Hello world'); });
Message字段处理器
$bot->onMessage('sticker', function (Context $ctx) { $ctx->reply('Nice sticker!'); });
Update字段处理器
$bot->onUpdate('message', function (Context $ctx) { $ctx->reply('Answer on any message (text, sticker, photo, etc.)'); });
CallbackQuery "data"字段处理器
$bot->onAction('like', function (Context $ctx) { $ctx->reply('You pressed the button with callaback_data=like'); });
在Message文本中查找子串
$bot->onHears('fu*k', function (Context $ctx) { $ctx->reply('Stop! If you continue, you will be banned'); });
或使用数组子串
$bot->onHears(['di*k', 'f**k'], function (Context $ctx) { $ctx->reply('Stop! If you continue, you will be banned'); });
带有参数的命令制作
$bot->onText('/message {user:integer} {message:string}', function (Context $ctx) { $ctx->withVars()->reply("User id: {v-user}\nMessage: {v-message}"); });
无验证类型参数语法:{name}
无验证类型可选参数语法:{name?}
参数语法:{name:validator_name}
可选参数语法:{name:validation_name?}如果您需要>2个空格参数。例如
/msg {a:string} {b:string}. 如果消息文本是"/msg hello world guys",变量值将是:a = hello world,b = guys。
要显式分隔变量,您需要使用框变量
语法:{name:string:box}
示例:/msg {name:string:"} {msg:string:"}
结果您得到具有语法的命令
/msg "{name:string}" "{msg:string}"
示例:/msg "John Smith" "hello world"
变量:name=John Smith,msg=hello world。
框变量可以是可选的,语法:{name:type:box?}
默认验证类型
默认使用验证器"any"({name} == {name:any})