1. Go to this page and download the library: Download jx3dev/onebot-v11 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jx3dev / onebot-v11 example snippets
use OneBot\Annotation\BotCommand;
use OneBot\Context\CommandContext;
class Ping
{
#[BotCommand('ping')]
public function ping(CommandContext $ctx): void
{
$ctx->reply('pong');
}
}
use OneBot\Permission\Permission;
use OneBot\Contract\PermissionInterface;
#[BotCommand('ban', permission: [Permission::SUPER, Permission::ADMIN])] // 超管或群管
public function ban(BotContext $ctx): void { /* ... */ }
class InWhitelist implements PermissionInterface {
public function check(BotContext $ctx): bool { return true; /* 你的逻辑 */ }
}
#[BotCommand('x', permission: [Permission::SUPER, InWhitelist::class])]
#[BotMessage] // 判个格式就转给命令
public function onAnswer(CommandContext $ctx): void
{
if ($this->isIdiom($ctx->text())) {
$ctx->runCommand('接龙答案', $ctx->text());
}
}
#[BotCommand('天气预报')] // 别名 / 同义词转发
public function alias(CommandContext $ctx): void
{
$ctx->runCommand('天气', $ctx->args()->rest());
}
if ($ctx->hasCommand('天气')) { // 装了某插件才转、没装自己处理:先探避免 warning
$ctx->runCommand('天气', $city);
}
#[BotCommandBefore] // 默认 guarded=true:权限**之后**,只有过了权限的命令才过这里
public function prepare(CommandContext $ctx): void { /* 统一准备;stop()=拦截 */ }
#[BotCommandBefore(guarded: false)] // 权限**之前**、命中就过
public function guard(CommandContext $ctx): void { /* 想拦就 stop() */ }
#[BotCommandAfter] // 命令执行后(handler 抛异常也照跑)
public function done(CommandContext $ctx): void { /* ... */ }
use OneBot\Context\ActionContext;
#[BotActionBefore('send_group_msg')] // 限速拒绝:给 failed 响应,call() 抛 ApiCallException
public function rateLimit(ActionContext $ctx): void
{
if ($this->overLimit($ctx->selfId())) {
$ctx->abort(['status' => 'failed', 'retcode' => 1400, 'message' => 'rate limited']);
}
}
#[BotActionBefore('get_group_member_list')] // 缓存命中:给 ok 响应,call() 直接返回其中 data、不发请求
public function cache(ActionContext $ctx): void
{
if ($hit = $this->cacheGet($ctx->getParams())) {
$ctx->abort(['status' => 'ok', 'retcode' => 0, 'data' => $hit]);
}
}
use OneBot\Context\ExceptionContext;
use OneBot\Exception\ApiTimeoutException;
#[BotException(ApiTimeoutException::class)]
public function onTimeout(ExceptionContext $ctx): void { /* ... */ }
#[BotException] // 兜底全部
public function onAny(ExceptionContext $ctx): void { /* ... */ }
use Hyperf\Framework\Event\AfterWorkerStart;
use OneBot\Dispatch\HandlerRegistry;
class Setup
{
#[BotServerStart]
public function boot(HandlerRegistry $registry): void
{
$registry->addAlias('天气', ['weather', 'tq']); // fork 前加一次,各 Worker 继承
}
#[BotWorkerStart]
public function warm(AfterWorkerStart $e): void
{
if ($e->workerId === 0) { /* 想「只做一次」判 workerId===0 */ }
}
}
#[BotCommand('foo')]
#[BotMiddleware(MyMiddleware::class, args: ['limit' => 5])]
public function foo(CommandContext $ctx): void { /* ... */ }
use OneBot\BotManager;
class Foo
{
public function __construct(private BotManager $bots) {}
public function bar(): void
{
$bot = $this->bots->get('123456789'); // 不在线抛 BotNotFoundException;find() 则返回 null
// ...
}
}