Skip to content

moniang/go-cq-http-sdk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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);
});

已支持Api

$api->bot;
  • 获取登录号信息 getLoginInfo
  • 设置登录号资料 setQqProfile
  • 获取企点账号信息 qiDianGetAccountInfo
  • 获取在线机型 getModelShow
  • 设置在线机型 setModelShow
  • 获取当前账号在线客户端列表 getOnlineClients
$api->friend;
  • 获取陌生人信息 getStrangerInfo
  • 获取好友列表 GetFriendList
  • 获取单向好友列表 getUnidirectionalFriendList
  • 删除好友 deleteFriend
  • 删除单向好友 deleteUnidirectionalFriend
$api->message;
  • 发送私聊消息 sendPrivateMsg
  • 发送群聊消息 sendGroupMsg
  • 发送消息 sendMsg
  • 获取消息 getMsg
  • 撤回消息 deleteMsg
  • 标记消息已读 markMsgAsRead
  • 获取合并转发内容 getForwardMsg
  • 发送合并转发 ( 群聊 ) sendGroupForwardMsg
  • 发送合并转发 ( 好友 ) sendPrivateForwardMsg
  • 获取群消息历史记录 getGroupMsgHistory
$api->image;
  • 获取图片信息 getImage
  • 检查是否可以发送图片 canSendImage
  • 图片 OCR ocrImage
$api->record;
  • 获取语音 getRecord
  • 检查是否可以发送语音 canSendRecord
$api->request;
  • 处理加好友请求 setFriendAddRequest
  • 处理加群请求/邀请 setGroupAddRequest
$api->groupInfo;
  • 获取群信息 getGroupInfo
  • 获取群列表 getGroupList
  • 获取群成员信息 getGroupMemberInfo
  • 获取群成员列表 getGroupMemberList
  • 获取群荣誉信息 getGroupHonorInfo
  • 获取群系统消息 getGroupSystemMsg
  • 获取精华消息列表 getEssenceMsgList
  • 获取群 @全体成员 剩余次数 getGroupAtAllRemain
$api->groupSetting;
  • 设置群名 setGroupName
  • 设置群头像 setGroupPortrait
  • 设置群管理员 setGroupAdmin
  • 设置群名片 ( 群备注 ) setGroupCard
  • 设置群组专属头衔 setGroupSpecialTitle
$api->groupAction;
  • 群单人禁言 setGroupBan
  • 群全员禁言 setGroupWholeBan
  • 群匿名用户禁言 setGroupAnonymousBan
  • 设置精华消息 setEssenceMsg
  • 移出精华消息 deleteEssenceMsg
  • 群打卡 sendGroupSign
  • 群设置匿名 setGroupAnonymous
  • 发送群公告 sendGroupNotice
  • 获取群公告 getGroupNotice
  • 群组踢人 setGroupKick
  • 退出群组 setGroupLeave
$api->groupFile;
  • 上传群文件 uploadGroupFile
  • 删除群文件 deleteGroupFile
  • 创建群文件文件夹 createGroupFileFolder
  • 删除群文件文件夹 deleteGroupFolder
  • 获取群文件系统信息 getGroupFileSystemInfo
  • 获取群根目录文件列表 getGroupRootFiles
  • 获取群子目录文件列表 getGroupFilesByFolder
  • 获取群文件资源链接 getGroupFileUrl
  • 上传私聊文件 uploadPrivateFile
$api->cq;
  • 获取 Cookies getCookies
  • 获取 CSRF Token getCsrfToken
  • 获取 QQ 相关接口凭证 getCredentials
  • 获取版本信息 getVersionInfo
  • 获取状态 getStatus
  • 清理缓存 cleanCache
  • 重载事件过滤器 reloadEventFilter
  • 下载文件到缓存目录 downloadFile
  • 检查链接安全性 checkUrlSafely
  • 获取中文分词 ( 隐藏 API ) getWordSlices
  • 对事件执行快速操作 ( 隐藏 API ) handleQuickOperation

支持的 CQ 码

  • QQ 表情 Itwmw\GoCqHttp\CqCode\Face
  • 语音 Itwmw\GoCqHttp\CqCode\Record
  • 短视频 Itwmw\GoCqHttp\CqCode\Video
  • @某人 Itwmw\GoCqHttp\CqCode\At
  • 链接分享 Itwmw\GoCqHttp\CqCode\Share
  • 音乐分享 Itwmw\GoCqHttp\CqCode\Music
  • 自定义音乐分享 Itwmw\GoCqHttp\CqCode\MusicCustom
  • 图片 Itwmw\GoCqHttp\CqCode\Image
  • 回复 Itwmw\GoCqHttp\CqCode\Reply
  • 红包 Itwmw\GoCqHttp\CqCode\RedBag
  • 戳一戳 Itwmw\GoCqHttp\CqCode\Poke
  • 礼物 Itwmw\GoCqHttp\CqCode\Gift
  • 合并转发 Itwmw\GoCqHttp\CqCode\Forward
  • XML 消息 Itwmw\GoCqHttp\CqCode\Xml
  • JSON 消息 Itwmw\GoCqHttp\CqCode\Json
  • 装逼大图 Itwmw\GoCqHttp\CqCode\CardImage
  • 文本转语音 Itwmw\GoCqHttp\CqCode\Tts

使用示例

发送:

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]

About

go-cqhttp的PHP SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%