Skip to content

Latest commit

 

History

History
183 lines (141 loc) · 5.25 KB

readme.md

File metadata and controls

183 lines (141 loc) · 5.25 KB

QQ官方机器人 JavaSDK 开发文档


包目录说明:

  • api 对于整个SDK需要实现的功能的定义并无实现
  • entities QQ频道官方文档数据格式的重写
  • http SDK使用到的http请求工具的定义
  • impl 对于SDK api的基本实现
  • network SDK的网络层
  • utils sdk 的工具类
  • Resource.java 公用资源类
  • Starter.java 启动类
  • Start0 启动类附属

相关部分文档指引


在配置好项目依赖后 即可使用

maven仓库

启动流程

step-1

登录q.qq开发者平台获得appid token 等参数

step0 启动程序

// 启动类新建 一般启动方法 不可接收发送 群聊消息 见v2群文档
Starter starter = new Starter("appid", "token");
// 私域推荐Intents.PRIVATE_INTENTS 公域机器人推荐 Intents.PUBLIC_INTENTS
starter.getConfig().setCode(Intents.PRIVATE_INTENTS.getCode());
// 切换沙箱与正式环境
// starter.getConfig().sandbox();
// 启动
starter.run();

事件注册

step1 接收事件 tips:方法中参数为Event任一子类或实现

starter.registerListenerHost(new ListenerHost(){
    //必须要有该注解 否则将不注册
    @EventReceiver
    public void onEvent(MessageEvent event){
        event.send("Hello World!");
    }
});

消息发送

step2 发送消息 tips: MessageEvent exts Sender

  • 发送文本sender.send("文本");
  • 发送图片
//step1 构造Image
Image image = null;
//图片链接构造
image = new Image(url);
//bytes数据构造
image = new Image(bytes);        
//step2 send
sender.send(image);
  • 发送 markdown

    event.send(new Markdown("custom_template_id")
      //申请的模板 参数填充
      .addParam("key", "value")
      //可选 设置按钮模板 
      .setKeyboard("id"));   
    

其他设置项

日志设置

public class LogDemo {
    public static void main(String[] args) {
        //默认方式
        //日志文件路径 设置为null 时不输出文件
        starter.APPLICATION.logger.setOutFile("./logs/%s.log");
        //日志文件格式
        LoggerImpl.INSTANCE.dfn = new SimpleDateFormat("/yyyy-MM-dd");
    }
}

自定义消息发送

通过http请求达到想要的目的获取bot请求必要的请求头方式

//方法必须在start.run 之后
//频道发送请求必要请求头
starter.APPLICATION.INSTANCE.getContextManager().getContextEntity(Start0.class).getHeaders()
//q群发送请求必要请求头
starter.APPLICATION.INSTANCE.getContextManager().getContextEntity(Start0.class).getHeaders()

//其中主动发送qq群

starter.registerListenerHost(new ListenerHost() {
    @EventReceiver
    public void onEvent(ConnectedEvent event) {
        V2MsgData data = new V2MsgData().setContent("测试主动消息");
        starter.getBot().groupBaseV2.send("groupOpenId", data.toString(), SEND_MESSAGE_HEADERS);
    }
});

img.png

依赖排斥

  • v1.5.0-Beta7 在与com.alibaba.fastjson2:fastjson2 同时引用时会产生大量空指针#20

2025/4/18 v1.5.2-R1 +

支持自定义websocket链接地址

对于webhook已开通且无法再进行websocket开发者而言

可通过webhook转websocket服务继续使用本项目

from @NintyCat

使用代码如下

    //===========================你的自定义地址
    starter.getConfig().setWslink("wss://api.sgroup.qq.com/websocket");
    starter.getConfig().setWebSocketListener(new WebSocketListener() {
        @Override
        public boolean onMessage(WebSocketClient client, String msg) {
            Pack pack = GSON.fromJson(msg, Pack.class);
            // log or syso
            if (pack == null) {
                //TODO
            } else {
                for (OnPackReceive onPackReceive : starter.getWssWorker().getOnPackReceives()) {
                    if (onPackReceive instanceof AuthAndHeartbeat) continue;
                    onPackReceive.onReceive(pack);
                }
            }
            return false;
        }
    });
    start.run();

1.5.2-R2

初步对webhook链接方式的支持

使用该方式链接 其他配置参数将失效

在服务启动后需要将q.qq配置链接设置为 "https://you-domain/webhook0" 路径

确保https地址可访问到项目部署机器 #此类问题 不建议开issue

验证完成后确定配置

  //设置webhook服务端口 默认为0时不开启webhook
  starter.getConfig().setWebhookport(81);