v2 使用条件(必须) 当前(23.11.16)
- 机器人必须为公域
- 必须有 在QQ群配置 的权限
- 配置完成后,群主可从沙箱群“设置-群机器人”打开机器人列表页添加测试机器人
以下为 必要启动代码
展开查看
import io.github.kloping.qqbot.Starter;
import io.github.kloping.qqbot.api.Intents;
import io.github.kloping.qqbot.api.message.MessageChannelReceiveEvent;
import io.github.kloping.qqbot.api.v2.GroupMessageEvent;
import io.github.kloping.qqbot.entities.ex.Image;
import io.github.kloping.qqbot.entities.ex.MessageAsyncBuilder;
import io.github.kloping.qqbot.entities.qqpd.data.Emoji;
import io.github.kloping.qqbot.impl.BaseConnectedEvent;
import io.github.kloping.qqbot.impl.EventReceiver;
import io.github.kloping.qqbot.impl.ListenerHost;
public class demo {
public static void main(String[] args) {
//==============================================必要↓↓↓↓↓↓↓
Starter starter = new Starter("appid", "token", "secret");
//===================================公域推荐订阅===============↓群聊/好友 事件订阅
starter.getConfig().setCode(Intents.PUBLIC_INTENTS.and(Intents.GROUP_INTENTS));
starter.run();
starter.registerListenerHost(new ListenerHost() {
@EventReceiver
public void onMessage(MessageChannelReceiveEvent event) {
MessageAsyncBuilder builder = new MessageAsyncBuilder();
builder.append("测试发图!");
builder.append(new Image("http://kloping.top/icon.jpg"));
builder.append(Emoji.K歌);
event.send(builder.build());
}
/**
* 因为是公域 所以仅当bot被at时才能触发事件
* @param event
*/
@EventReceiver
public void onMessage(GroupMessageEvent event) {
MessageAsyncBuilder builder = new MessageAsyncBuilder();
builder.append("测试发图!");
//目前仅支持 以url发送图片 https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html#%E5%8F%91%E9%80%81%E5%88%B0%E7%BE%A4%E8%81%8A
builder.append(new Image("http://kloping.top/icon.jpg"));
builder.append(Emoji.K歌);
event.sendMessage(builder.build());
}
});
}
}
选择性配置 # date 2023.11.17 version 1.5.0-L6
-
支持image发送视频/语音 视频:Image(url,2); 语音:Image(url,3); 文件:暂不支持
2024.3.24 version 1.5.1-Beta1 支持非url直链上传
展开查看
public class demo1 {
public static void main(String[] args) {
//设置qq群发送图片时将bytes上传为url以达到适配bytes image
starter.getConfig().setInterceptor0(bytes -> upload(url));
//仅推荐 / 当前可用 不保证未来可用
starter.getConfig().setInterceptor0(bytes -> {
try {
String url = Jsoup.connect("http://bak0.kloping.top:81/upload-img")
.ignoreContentType(true)
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67")
.data("file", "temp.jpg", new ByteArrayInputStream(bytes)).method(Connection.Method.POST).execute().body();
return url;
} catch (IOException e) {
System.err.println("上传失败! "+e.getMessage());
return null;
}
});
}
}