jaysondev / teletant-last
Telegram机器人框架
1.0.0
2023-07-15 20:28 UTC
Requires
- php: ^7.4|^8
- ext-json: *
- ext-mbstring: *
- askoldex/formatter: 1.0.3
- guzzlehttp/guzzle: ^7
- psr/log: ^3.0
README
作者:@askoldex,@uniqkic
灵感来源:telegraf,irazasyed/telegram-bot-sdk
示例
入门
要求
- PHP 8.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'); });
消息字段处理器
$bot->onMessage('sticker', function (Context $ctx) { $ctx->reply('Nice sticker!'); });
更新字段处理器
$bot->onUpdate('message', function (Context $ctx) { $ctx->reply('Answer on any message (text, sticker, photo, etc.)'); });
回调查询 "data"字段处理器
$bot->onAction('like', function (Context $ctx) { $ctx->reply('You pressed the button with callaback_data=like'); });
在消息文本中查找子字符串
$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})