apboro/teletant

Telegram机器人框架

1.5.3 2023-03-11 21:17 UTC

This package is not auto-updated.

Last update: 2024-09-23 02:54:34 UTC


README

作者: @askoldex@uniqkic
灵感来源: telegrafirazasyed/telegram-bot-sdk

示例

点击

入门

要求

  1. PHP 8.2
  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.)');
});
回调查询“data”字段处理器 CallbackQuery
$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).
为了明确分隔变量,您需要使用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})