askoldex / teletant
Telegram机器人框架
1.4.8
2022-01-24 17:56 UTC
Requires
- php: ^7.4|^8.0
- ext-json: *
- ext-mbstring: *
- askoldex/formatter: 1.0.3
- guzzlehttp/guzzle: 7.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)。
要显式分隔变量,您需要使用Boxed变量
语法:{name:string:box}
示例:/msg {name:string:"} {msg:string:"}
结果得到语法
/msg "{name:string}" "{msg:string}"
示例:/msg "John Smith" "hello world"
变量:name=John Smith, msg=hello world。
Boxed变量可能是可选的,语法:{name:type:box?}
默认验证类型
默认使用验证器"any"({name} == {name:any})