itwmw/go-cq-http-sdk

v0.6.1 2023-07-07 07:12 UTC

This package is auto-updated.

Last update: 2024-11-19 04:58:19 UTC


README

go-cqhttp

go-cqhttp-sdk

go-cqhttp项目的PHP版SDK,拥有完整注释和代码提示

license release php-version

文档 · 下载 · 开始使用

安装

composer require itwmw/go-cq-http-sdk

使用示例

$api = new Itwmw\GoCqHttp\Api();
// 发送私聊消息
$api->message->sendMsg('测试消息', 'private', 995645888);

处理上报消息

SDK内置消息的处理:

$server = $api->getServer();

你可以通过中间件模式来处理消息:

use Itwmw\GoCqHttp\Data\Post\BasePostMessage;

$server->addHandler(function(BasePostMessage $message, \Closure $next) {
    // 处理消息
    return $next($message);
})->addHandler(function(BasePostMessage $message, \Closure $next) {
    // 处理消息
    return $next($message);
})->addHandler(function(BasePostMessage $message, \Closure $next) {
    // 处理消息
    return $next($message);
})->addHandler(function(BasePostMessage $message, \Closure $next) {
    // 处理消息
    return $next($message);
});

echo $server->handle();

使用独立的类来处理消息

use Itwmw\GoCqHttp\Data\Post\BasePostMessage;

class MessageHandle
{
    public function __invoke(BasePostMessage $message, \Closure $next)
    {
        // 处理消息
        return $next($message);
    }
}

$server->addHandler(MessageHandle::class);

也支持callable类型的处理器

注册指定类型的处理器

use Itwmw\GoCqHttp\Data\Struct\Enum\PostMessageType;
use Itwmw\GoCqHttp\Data\Post\Message\PrivateMessage;

$server->addMessageListener(PostMessageType::PRIVATE, function (PrivateMessage $message, \Closure $next) {
    // 处理消息
    return $next($message);
});
use Itwmw\GoCqHttp\Data\Struct\Enum\PostNoticeType;
use Itwmw\GoCqHttp\Data\Post\Notice\GroupIncreaseNotice;

$server->addNoticeListener(PostNoticeType::GROUP_INCREASE, function (GroupIncreaseNotice $message, Closure $next) {
    // 处理群成员增加
    return $next($message);
});

同样也支持使用独立的类来处理消息

快捷操作

部分类型的消息支持快捷操作,快捷操作的方法为 response,例:

use Itwmw\GoCqHttp\Data\Post\Message\PrivateMessage;

$server->addMessageListener(PostMessageType::PRIVATE, function (PrivateMessage $message, \Closure $next) {
    if ('再见' === $message->message) {
        return $message->response('bye~');
    }
    return $next($message);
});

支持

Bot账号相关的Api
$api->bot;
好友相关的Api
$api->friend;
消息相关的Api
$api->message;
图片相关的Api
$api->image;
语音相关的Api
$api->record;
处理请求相关的Api
$api->request;
群信息相关的Api
$api->groupInfo;
群设置相关的Api
$api->groupSetting;
群操作相关的Api
$api->groupAction;
群文件相关的Api
$api->groupFile;
Go-CqHttp相关的Api
$api->cq;
CQ 码支持

使用示例

发送:

use Itwmw\GoCqHttp\Api;
use Itwmw\GoCqHttp\CqCode\Tts;

$api = new Api();
$tts = new Tts('你好');
$api->message->sendMsg($tts, user_id: 995645888);

解析接受到的消息:

$msg = "[CQ:share,url=https://www.baidu.com/,title=百度一下,content=百度一下,你就知道,image=https://www.baidu.com/img/bd_logo1.png]";
$share = Share::create($msg);
print_r($share);
//Itwmw\GoCqHttp\CqCode\Share Object
//(
//    [url] => https://www.baidu.com/
//    [title] => 百度一下
//    [content] => 百度一下,你就知道
//    [image] => https://www.baidu.com/img/bd_logo1.png
//)
echo $share;
// [CQ:share,url=https://www.baidu.com/,title=百度一下,content=百度一下,你就知道,image=https://www.baidu.com/img/bd_logo1.png]

关于:

QQ群: 852297017