Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

QQ频道bot sdk

License

NotificationsYou must be signed in to change notification settings

chinosk6/QQ-official-guild-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

  • 开发中~

直接使用SDK

  • 参考bot_main.py
  • bot_main.py 同级目录下创建名为config.yaml 的配置文件,填入自己的bot_app_idbot_tokenbot_secret,内容类似下面所示,也可直接使用仓库里的config.example.yaml 文件,然后自己修改后缀名和内容
bot:id:123# 机器人idtoken:456# 机器人tokensecret:789# 机器人secret
importbot_apifrombot_api.modelsimportArk,Embedfrombot_api.utilsimportyaml_utiltoken=yaml_util.read('config.yaml')bot=bot_api.BotApp(token['bot']['id'],token['bot']['token'],token['bot']['secret'],is_sandbox=True,debug=True,api_return_pydantic=True,ignore_at_self=False,# 过滤消息正文中艾特Bot自身的内容, 默认为Falseinters=[bot_api.Intents.GUILDS,bot_api.Intents.AT_MESSAGES,bot_api.Intents.GUILD_MEMBERS])# 事件订阅@bot.receiver(bot_api.structs.Codes.SeverCode.BotGroupAtMessage)# 填入对应的参数实现处理对应事件defget_at_message(chain:bot_api.structs.Message):# 注册一个艾特消息处理器bot.logger(f"收到来自频道:{chain.guild_id} 子频道:{chain.channel_id} "f"内用户:{chain.author.username}({chain.author.id}) 的消息:{chain.content} ({chain.id})")if"你好"inchain.content:bot.api_send_message(chain.channel_id,chain.id,"hello world!")elif"test"inchain.content:bot.api_send_message(chain.channel_id,chain.id,"chieri在哟~")elif"/echo"inchain.content:reply=chain.content[chain.content.find("/echo")+len("/echo"):].strip()bot.api_send_message(chain.channel_id,chain.id,reply)elif"/embed"inchain.content:# 发送embedsend_embed=Embed("标题", ["文本1","文本2","文本3"],image_url=None)bot.api_send_message(chain.channel_id,chain.id,embed=send_embed)elif"/ark"inchain.content:# 发送ark消息, 需要Ark权限send_ark=Ark.LinkWithText("描述","提示信息", [["纯文本1"], ["纯文本2"], ["链接文本1","http://baidu.com"]])bot.api_send_message(chain.channel_id,chain.id,ark=send_ark)bot.start()# 启动bot

关于部分事件官方已改为被动消息说明

事件代号事件描述
GUILD_MEMBER_ADD成员加入
GUILD_MEMBER_UPDATE成员资料变更
GUILD_MEMBER_REMOVE成员被移除
MESSAGE_REACTION_ADD消息添加表情表态
MESSAGE_REACTION_REMOVE消息删除表情表态
FORUM_THREAD_CREATE用户创建主题
FORUM_THREAD_UPDATE用户更新主题
FORUM_THREAD_DELETE用户删除主题
FORUM_POST_CREATE用户创建帖子
FORUM_POST_DELETE用户删除帖子
FORUM_REPLY_CREATE用户回复评论
FORUM_REPLY_DELETE用户回复评论

使用:即将要回复的消息ID(msg_id)替换为以上事件代号即可

这里以添加表情表态为例bot.api_send_message(channel_id,msg_id='MESSAGE_REACTION_ADD',content='ok')

自行组合消息

api_send_message方法提供了极高的自由度。您可以按照本文档提供的方法发送消息, 也可以使用others_parameter参数自行组合。

使用others_parameter, 您需要按照QQ机器人文档 - 发送消息 提供的参数发送消息, 若有此SDK没有支持的消息类型, 您依旧可以自行组合参数进行发送。

下面是发送一条文本+图片消息的例子


  • 一般情况下, 您可以:
bot.api_send_message(chain.channel_id,chain.id,"这是消息","http://您的图片")
  • 您也可以:
bot.api_send_message(chain.channel_id,chain.id,others_parameter={"content":"这是消息","image":"https://您的图片"})

Ark消息说明

  • 注意: 发送Ark消息需要向官方申请Ark权限, 否则无法发送

引用:

frombot_api.modelsimportArk,Embed

发送Embed消息

send_embed=Embed("标题", ["文本1","文本2","文本3"],image_url="http://你的图片")"image_url"参数可选,若没有图片,则不填bot.api_send_message(channel_id,message_id,embed=send_embed)

发送Ark消息

send_ark=Ark.LinkWithText("描述","提示信息",                            [["纯文本1"], ["纯文本2"], ["链接文本1","http://baidu.com"], ["链接文本2","http://google.com"]])bot.api_send_message(channel_id,message_id,ark=send_ark)

目前支持的事件/API

事件

  • 下表中所有事件触发时都会在新线程中执行

  • 事件代号: 注册函数时, 输入对应事件代号, 在触发相应事件时, 所有被注册函数将被调用。

    • 位于: 类bot_api.structs.Codes.SeverCode, 继承自GatewayEventName
  • 传入参数指被注册函数的参数

    • 位于: 类bot_api.structs
事件代号传入参数事件描述
FUNC_CALL_AFTER_BOT_LOAD初始化后的BotAPP类(self)当Bot初始化完成后, 会立刻执行这些函数
AT_MESSAGE_CREATEMessage收到艾特消息
MESSAGE_CREATEMessage收到消息(仅私域机器人可用)
DIRECT_MESSAGE_CREATEMessage收到私聊消息
GUILD_CREATEGuildbot加入频道
GUILD_UPDATEGuild频道信息更新
GUILD_DELETEGuild频道解散/bot被移除
CHANNEL_CREATEChannel子频道被创建
CHANNEL_UPDATEChannel子频道信息变更
CHANNEL_DELETEChannel子频道被删除
GUILD_MEMBER_ADDMemberWithGuildID新用户加入频道
GUILD_MEMBER_UPDATE-TX: 暂无
GUILD_MEMBER_REMOVEMemberWithGuildID用户离开频道
AUDIO_STARTAudioAction音频开始播放
AUDIO_FINISHAudioAction音频结束
AUDIO_ON_MICAudioAction上麦
AUDIO_OFF_MICAudioAction下麦
MESSAGE_REACTION_ADDMessageReaction添加表情表态
MESSAGE_REACTION_REMOVEMessageReaction删除表情表态
THREAD_CREATE暂不支持用户创建主题
THREAD_UPDATE暂不支持用户更新主题
THREAD_DELETE暂不支持用户删除主题
POST_CREATE暂不支持用户创建帖子
POST_DELETE暂不支持用户删除帖子
REPLY_CREATE暂不支持用户回复评论
REPLY_DELETE暂不支持用户回复评论
MESSAGE_AUDIT_PASSMessageAudited消息审核通过
MESSAGE_AUDIT_REJECTMessageAudited消息审核不通过
PUBLIC_MESSAGE_DELETEMessageDelete消息撤回(公域)
MESSAGE_DELETEMessageDelete消息撤回(私域)
DIRECT_MESSAGE_DELETEMessageDelete消息撤回(私聊)
  • 例: 注册一个添加表情表态处理函数
@bot.receiver(bot_api.structs.Codes.SeverCode.MESSAGE_REACTION_ADD)# 填入事件代号defget_at_message(event:bot_api.structs.MessageReaction):# 函数参数类型为上表对应的传入参数pass

API

  • 初始化Bot实例后, 输入bot.api_, 即可根据代码补全进行使用
api_send_message()# 发送频道消息api_create_dms()# 创建私信会话api_send_private_message()# 发送私聊消息api_reply_message()# 回复消息(频道/私聊)api_mute_guild()# 全频道禁言api_mute_member()# 指定用户禁言api_get_self_guilds()# 获取Bot加入的频道列表api_get_self_info()# 获取Bot自身信息api_get_message()# 获取指定消息api_get_guild_channel_list()# 获取频道内子频道列表api_get_channel_info()# 获取子频道信息api_get_guild_user_info()# 获取频道用户信息api_get_guild_info()# 获取频道信息api_get_schedule_list()# 获取子频道日程列表api_get_schedule()# 获取单个日程信息api_schedule_create()# 创建日程api_schedule_change()# 修改日程api_schedule_delete()# 删除日程api_message_recall()# 撤回消息api_guild_roles_list_get()# 获取频道身份组列表api_guild_role_create()# 创建频道身份组api_guild_role_change()# 修改频道身份组api_guild_role_remove()# 删除频道身份组api_guild_role_member_add()# 增加频道身份组成员api_guild_role_member_remove()# 移除频道身份组成员api_announces_create()# 创建频道公告api_announces_remove()# 删除频道公告api_permissions_get_channel()# 获取指定子频道的权限api_permissions_change_channel()# 修改指定子频道的权限api_permissions_get_channel_group()# 获取指定子频道身份组的权限api_permissions_change_channel_group()# 修改指定子频道身份组的权限api_audio_control()# 音频控制api_get_api_permission()# 获取频道可用权限列表api_demand_api_permission()# 创建频道 API 接口权限授权链接api_add_pins()# 添加精华消息api_remove_pins()# 删除精华消息api_get_pins()# 获取精华消息api_send_message_reactions()# 发送表情表态api_send_message_guide()# 发送消息设置引导api_get_guild_message_freq()# 获取频道消息频率设置api_pv_get_member_list()# 仅私域机器人可用 - 取频道成员列表api_pv_kick_member()# 仅私域机器人可用 - 踢出指定成员api_pv_create_channel()# 仅私域机器人可用 - 创建子频道api_pv_change_channel()# 仅私域机器人可用 - 修改子频道信息api_pv_delete_channel()# 仅私域机器人可用 - 删除子频道




作为HTTP API使用

  • 一个人同时更新两套SDK显然是非常困难的。因此HTTP API部分的更新进度会慢于SDK本体(已停更)。欢迎有能之士前来Pr~

  • 参考main.py

importbot_apiimportserverbot=bot_api.BotApp(123456,"你的bot token","你的bot secret",is_sandbox=True,debug=True,api_return_pydantic=True,inters=[bot_api.Intents.GUILDS,bot_api.Intents.AT_MESSAGES,bot_api.Intents.GUILD_MEMBERS])# 事件订阅app=server.BotServer(bot,ip_call="127.0.0.1",port_call=11415,ip_listen="127.0.0.1",port_listen=1988,allow_push=False)# 开始注册事件, 可以选择需要的进行注册app.reg_bot_at_message()# 艾特消息事件app.reg_guild_member_add()# 成员增加事件app.reg_guild_member_remove()# 成员减少事件# 以下事件与onebot差别较大app.reg_guild_create()# Bot加入频道事件app.reg_guild_update()# 频道信息更新事件app.reg_guild_delete()# Bot离开频道/频道被解散事件app.reg_channel_create()# 子频道创建事件app.reg_channel_update()# 子频道信息更新事件app.reg_channel_delete()# 子频道删除事件@app.bot.receiver(bot_api.structs.Codes.SeverCode.image_to_url)# 注册一个图片转url方法defimg_to_url(img_path:str):# 用处: 发送图片时, 使用图片cq码[CQ:image,file=]或[CQ:image,url=]# 装饰器作用为: 解析cq码中图片的路径(网络图片则下载到本地), 将绝对路径传给本函数, 自行操作后, 返回图片url, sdk将使用此url发送图片# 若不注册此方法, 则无法发送图片。print(img_path)return"https://你的图片url"# 注册事件结束app.listening_server_start()# HTTP API 服务器启动app.bot.start()# Bot启动

目前实现的CQ码

CQ码功能备注
[CQ:reply,id=abc123]回复消息被动回复请务必带上此CQ码, 否则会被视为主动推送消息
[CQ:at,qq=123456]艾特用户与官方<@!123456>对应
[CQ:image,file=...]
[CQ:image,url=...]
发送图片发送图片可以使用这两个CQ码
由于API限制, 发送图片仅支持一张, 并且需要自行配置图床(见上方代码示例)
接收到图片时, 目前仅会返回[CQ:image,url=...]

目前实现的API(基本同onebot):

  • 特别注意: 所有用户ID,频道号,子频道号,消息ID字段均为string
接口描述备注
/send_group_msg发送群消息group_id请填写channel_id
auto_escape为true时, 依然会解析[CQ:reply]
/get_group_member_info获取成员信息group_id请填写guild_id
user_id,nickname,role有效
额外增加头像URL:avatar

目前实现的API(与onebot不同)

获取自身信息

  • 接口:/get_self_info
参数类型默认值说明
cacheboolfalse是否使用缓存(可选)
  • 返回参数
参数类型说明
avatarstring头像url
idstringBotID
usernamestringBot名

获取自身加入频道列表

  • 接口:/get_self_guild_list
参数类型默认值说明
cacheboolfalse是否使用缓存(可选)
beforestring-读此id之前的数据(可选,before/after只能二选一)
afterstring-读此id之后的数据(可选,before/after只能二选一)
limitint100每次拉取多少条数据(可选)
  • 返回参数

见:https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channel.html

获取频道信息

  • 接口:/get_guild_info
参数类型默认值说明
guild_idstring-频道id
  • 返回参数

见:https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild.html

获取子频道信息

  • 接口:/get_channel_info
参数类型默认值说明
channel_idstring-子频道id
  • 返回参数

见:https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channel.html

获取子频道列表

  • 接口:/get_channel_list
参数类型默认值说明
guild_idstring-频道id
  • 返回参数

见:https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html

获取指定消息

  • 接口:/get_message
参数类型默认值说明
message_idstring-消息id
channel_idstring-频道id
  • 返回参数

见:https://bot.q.qq.com/wiki/develop/api/openapi/message/get_message_of_id.html


目前实现的Event(基本同onebot)

  • 特别注意: 所有用户ID,频道号,子频道号,消息ID字段均为string

收到艾特消息

成员增加事件

成员减少事件


目前实现的Event(与onebot差别较大)

  • 通用字段
参数类型默认值说明
timeint-消息接收时间戳
self_idstring-自身id
post_typestringnotice上报类型
notice_typestring-通知类型
sub_typestring-通知子类型
user_idstring空字符串触发者ID, 仅在对应事件有值
guild_idstring空字符串触发频道ID, 仅在对应事件有值
channel_idstring空字符串触发子频道ID, 仅在对应事件有值
data--每个事件均不同, 见下方文档

Bot加入频道事件

参数类型
post_typestringnotice
notice_typestringguild_create
sub_typestringguild_create
guild_idstring频道ID
dataGuild见:腾讯机器人文档

频道信息更新事件

参数类型
post_typestringnotice
sub_typestringguild_update
guild_idstring频道ID
dataGuild见:腾讯机器人文档

机器人离开频道/频道被解散事件

参数类型
post_typestringnotice
notice_typestringguild_update
sub_typestringguild_update
guild_idstring频道ID
dataGuild见:腾讯机器人文档

子频道创建事件

参数类型
post_typestringnotice
notice_typestringchannel_create
sub_typestringchannel_create
guild_idstring频道ID
channel_idstring子频道ID
dataChannel见:腾讯机器人文档

子频道信息更新事件

参数类型
post_typestringnotice
notice_typestringchannel_update
sub_typestringchannel_update
guild_idstring频道ID
channel_idstring子频道ID
dataChannel见:腾讯机器人文档

子频道被删除事件

参数类型
post_typestringnotice
notice_typestringchannel_delete
sub_typestringchannel_delete
guild_idstring频道ID
channel_idstring子频道ID
dataChannel见:腾讯机器人文档

About

QQ频道bot sdk

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp