QQ频道机器人零基础开发详解(基于QQ官方机器人文档)[第六期]

QQ频道机器人零基础开发详解(基于QQ官方机器人文档)[第六期]

第六期介绍:频道模块之内容管理

在这里插入图片描述


不懂得的也可以来私聊或评论区问哦~
在这里插入图片描述
原力到一千才可以推广,三连啊喂!!!

在这里插入图片描述

创建频道公告

接口

POST /guilds/{guild_id}/announces

功能描述
用于创建频道全局公告。

Content-Type
application/json

参数

字段名类型描述
message_idstring选填,消息 id
channel_idstring选填,子频道 id
announces_typeuint32选填,公告类别 0:成员公告,1:欢迎公告,默认为成员公告
recommend_channelsRecommendChannel 数组选填,推荐子频道列表

返回
返回 Announces 对象。

错误码
详见错误码

Python示例

import requests

guild_id = "你的频道ID"
url = f"https://api.sgroup.qq.com/guilds/{guild_id}/announces"
Authorization = f"QQBot 你的鉴权token"

data = {
  "channel_id": "123456",
  "message_id": "xxxxxx"
}

headers = {
    "Content-Type": "application/json",
    "Authorization": Authorization
}

response = requests.post(url, headers=headers, json=data).json()

print(response)

请求数据包

{
  "channel_id": "123456",
  "message_id": "xxxxxx"
}

响应数据包

{
  "guild_id": "xxxxxx",
  "channel_id": "123456",
  "message_id": "xxxxxx",
  "announces_type": 0,
  "recommend_channels":[]
}

删除频道公告

接口

DELETE /guilds/{guild_id}/announces/{message_id}

功能描述
用于删除频道 guild_id 下指定 message_id 的全局公告。

Content-Type
application/json

返回
成功返回 HTTP 状态码 204

错误码
详见错误码

Python示例

import requests

guild_id = "你的频道ID"
message_id = "要删除的公告ID"
url = f"https://api.sgroup.qq.com/guilds/{guild_id}/announces/{message_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.delete(url, headers=headers)

if response.status_code == 204:
    print("公告删除成功")
else:
    print("删除公告失败,状态码:", response.status_code)

请求数据包

DELETE /guilds/123456/announces/112233

添加精华消息

接口

PUT /channels/{channel_id}/pins/{message_id}

功能描述
用于添加子频道 channel_id 内的精华消息。

Content-Type
application/json

返回
返回 PinsMessage 对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
message_id = "要设置为精华的消息ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/pins/{message_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.put(url, headers=headers).json()

print(response)

请求数据包

PUT /channels/123456/pins/112233

响应数据包

{
  "guild_id": "xxxxxx",
  "channel_id": "xxxxxx",
  "message_ids": ["xxxxx"]
}

删除精华消息

接口

DELETE /channels/{channel_id}/pins/{message_id}

功能描述
用于删除子频道 channel_id 下指定 message_id 的精华消息。

Content-Type
application/json

返回
成功返回 HTTP 状态码 204

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
message_id = "要删除的精华消息ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/pins/{message_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.delete(url, headers=headers)

if response.status_code == 204:
    print("精华消息删除成功")
else:
    print("删除精华消息失败,状态码:", response.status_code)

请求数据包

DELETE /channels/123456/pins/112233

获取精华消息

接口

GET /channels/{channel_id}/pins

功能描述
用于获取子频道 channel_id 内的精华消息。

Content-Type
application/json

返回
返回 PinsMessage 对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/pins"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.get(url, headers=headers).json()

print(response)

响应数据包

{
  "guild_id": "xxxxxx",
  "channel_id": "xxxxxx",
  "message_ids": ["xxxxx"]
}

获取频道日程列表

接口

GET /channels/{channel_id}/schedules

功能描述
用于获取 channel_id 指定的子频道中当天的日程列表。

Content-Type
application/json

参数

字段名类型描述
sinceuint64起始时间戳( ms)

返回
返回 Schedule 对象数组。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/schedules"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.get(url, headers=headers).json()

print(response)

请求数据包

{
  "since": 1642076400000
}

响应数据包

[
  {
    "id": "xxxxxx",
    "name": "上王者",
    "start_timestamp": "1642076400000",
    "end_timestamp": "1642083600000",
    "creator": {
      "user": {
        "id": "xxxxxx",
        "username": "xxxxxx",
        "bot": true
      },
      "nick": "",
      "joined_at": "2022-01-11T10:24:13+08:00"
    },
    "jump_channel_id": "0",
    "remind_type": "0"
  }
]

获取日程详情

接口

GET /channels/{channel_id}/schedules/{schedule_id}

功能描述
获取日程子频道 channel_idschedule_id 指定的的日程的详情。

Content-Type
application/json

返回
返回 Schedule 对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
schedule_id = "日程ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/schedules/{schedule_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.get(url, headers=headers).json()

print(response)

请求数据包

GET /channels/123455/schedules/112233

响应数据包

{
  "id": "112233",
  "name": "上王者",
  "start_timestamp": "1642076400000",
  "end_timestamp": "1642083600000",
  "creator": {
    "user": {
      "id": "xxxxxx",
      "username": "xxxxxx",
      "bot": true
    },
    "nick": "",
    "joined_at": "2022-01-11T10:24:13+08:00"
  },
  "jump_channel_id": "0",
  "remind_type": "0"
}

创建日程

接口

POST /channels/{channel_id}/schedules

功能描述
用于在 channel_id 指定的 日程子频道 下创建一个日程。

Content-Type
application/json

参数

字段名类型描述
scheduleSchedule日程对象,不需要带 id

返回
返回 Schedule 对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/schedules"
Authorization = f"QQBot 你的鉴权token"

data = {
  "schedule": {
    "name": "上王者",
    "start_timestamp": "1642076453000",
    "end_timestamp": "1642083653000",
    "jump_channel_id": "0",
    "remind_type": "0"
  }
}

headers = {
    "Content-Type": "application/json",
    "Authorization": Authorization
}

response = requests.post(url, headers=headers, json=data).json()

print(response)

请求数据包

{
  "schedule": {
    "name": "上王者",
    "start_timestamp": "1642076453000",
    "end_timestamp": "1642083653000",
    "jump_channel_id": "0",
    "remind_type": "0"
  }
}

响应数据包

{
  "id": "xxxxxx",
  "name": "上王者",
  "start_timestamp": "1642076400000",
  "end_timestamp": "1642083600000",
  "creator": {
    "user": {
      "id": "xxxxxx",
      "username": "xxxxxx",
      "bot": true
    },
    "nick": "",
    "joined_at": "2022-01-11T10:24:13+08:00"
  },
  "jump_channel_id": "0",
  "remind_type": "0"
}

修改日程

接口

PATCH /channels/{channel_id}/schedules/{schedule_id}

功能描述
用于修改日程子频道 channel_idschedule_id 指定的日程的详情。

Content-Type
application/json

参数

字段名类型描述
scheduleSchedule日程对象,不需要带 id

返回
返回 Schedule 对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
schedule_id = "日程ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/schedules/{schedule_id}"
Authorization = f"QQBot 你的鉴权token"

data = {
  "schedule": {
    "name": "今晚八点上王者",
    "start_timestamp": "1642076453000",
    "end_timestamp": "1642083653000",
    "jump_channel_id": "0",
    "remind_type": "0"
  }
}

headers = {
    "Content-Type": "application/json",
    "Authorization": Authorization
}

response = requests.patch(url, headers=headers, json=data).json()

print(response)

请求数据包

{
  "schedule": {
    "name": "今晚八点上王者",
    "start_timestamp": "1642076453000",
    "end_timestamp": "1642083653000",
    "jump_channel_id": "0",
    "remind_type": "0"
  }
}

响应数据包

{
  "id": "xxxxxx",
  "name": "今晚八点上王者",
  "start_timestamp": "1642076453000",
  "end_timestamp": "1642083653000",
  "creator": {
    "user": {
      "id": "xxxxxx",
      "username": "xxxxxx",
      "bot": true
    },
    "nick": "",
    "joined_at": "2022-01-13T11:02:21+08:00"
  },
  "jump_channel_id": "0",
  "remind_type": "0"
}

删除日程

接口

DELETE /channels/{channel_id}/schedules/{schedule_id}

功能描述
用于删除日程子频道 channel_idschedule_id 指定的日程。

Content-Type
application/json

返回
成功返回 HTTP 状态码 204

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
schedule_id = "日程ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/schedules/{schedule_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.delete(url, headers=headers)

if response.status_code == 204:
    print("日程删除成功")
else:
    print("删除日程失败,状态码:", response.status_code)

请求数据包

DELETE /channels/123456/schedules/112233

音频控制

接口

POST /channels/{channel_id}/audio

功能描述
用于控制子频道 channel_id 下的音频。

  • 音频接口:仅限 音频类机器人 才能使用,如需调用,需联系平台申请权限。

Content-Type
application/json

参数

字段名类型描述
audio_urlstring音频链接
textstring音频中包含的文本
statusuint32音频播放状态

返回
成功返回空对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/audio"
Authorization = f"QQBot 你的鉴权token"

data = {
  "audio_url": "http:/xxxxx.mp3",
  "text": "xxx",
  "status": 0
}

headers = {
    "Content-Type": "application/json",
    "Authorization": Authorization
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 204:
    print("音频控制成功")
else:
    print("音频控制失败,状态码:", response.status_code)

请求数据包

{
  "audio_url": "http:/xxxxx.mp3",
  "text": "xxx",
  "status": 0
}

机器人上麦

接口

PUT /channels/{channel_id}/mic

功能描述
机器人在 channel_id 对应的语音子频道上麦。

Content-Type
application/json

返回
成功返回空对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/mic"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.put(url, headers=headers)

if response.status_code == 204:
    print("机器人上麦成功")
else:
    print("机器人上麦失败,状态码:", response.status_code)

请求数据包

{}

机器人下麦

接口

DELETE /channels/{channel_id}/mic

功能描述
机器人在 channel_id 对应的语音子频道下麦。

Content-Type
application/json

返回
成功返回空对象。

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/mic"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.delete(url, headers=headers)

if response.status_code == 204:
    print("机器人下麦成功")
else:
    print("机器人下麦失败,状态码:", response.status_code)

请求数据包

{}

获取帖子列表

☣️关于帖子相关的所有API,公域机器人暂不支持申请,仅私域机器人可用,选择私域机器人后默认开通。开通后需要先将机器人从频道移除,然后重新添加,方可生效。

接口

GET /channels/{channel_id}/threads

功能描述
用于获取子频道下的帖子列表。

Content-Type
application/json

返回

字段名类型描述
threadsThread 数组帖子列表对象
is_finishuint32是否拉取完毕(0:否;1:是)

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/threads"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.get(url, headers=headers).json()

print(response)

响应数据包

{
  "threads": [{
    "guild_id": "75827011639035987",
    "channel_id": "2324603",
    "author_id": "144115218680332809",
    "thread_info": {
      "thread_id": "B_59101362700301001441152186803328090X60-1645416537",
      "title": "帖子标题1",
      "content": "{\"paragraphs\":[{\"elems\":[{\"text\":{\"text\":\"发送消息 | QQ机器人文档\"},\"type\":1}],\"props\":{}},{\"elems\":[{\"text\":{\"text\":\"• 主动消息:发送消息时,未填充msg_id 字段的消息。\"},\"type\":1}],\"props\":{}}]}",
      "date_time": "2022-02-21T12:08:57+08:00"
    }
  }],
  "is_finish": 1
}

获取帖子详情

接口

GET /channels/{channel_id}/threads/{thread_id}

功能描述
用于获取子频道下的帖子详情。

Content-Type
application/json

返回

字段名类型描述
threadThreadInfo帖子详情对象

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
thread_id = "帖子ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/threads/{thread_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.get(url, headers=headers).json()

print(response)

响应数据包

{
  "thread":{
    "guild_id":"75827011639035987",
    "channel_id":"2324603",
    "author_id":"144115218680332809",
    "thread_info":{
      "thread_id":"B_79051362477c03001441152186803328090X60-1645413753",
      "title":"帖子标题",
      "content":"{\"paragraphs\":[{\"elems\":[{\"text\":{\"text\":\"发送消息 | QQ机器人文档\"},\"type\":1}],\"props\":{}},{\"elems\":[{\"text\":{\"text\":\"• 主动消息:发送消息时,未填充msg_id 字段的消息。\"},\"type\":1}],\"props\":{}}]}",
      "date_time":"2022-02-21T11:22:33+08:00"
    }
  }
}

发表帖子

接口

PUT /channels/{channel_id}/threads

功能描述
用于在子频道下发表帖子。

Content-Type
application/json

参数

字段名类型描述
titlestring帖子标题
contentstring帖子内容
formatuint32帖子文本格式

返回

字段名类型描述
task_idstring帖子任务ID
create_timeuint32发帖时间戳,单位:秒

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/threads"
Authorization = f"QQBot 你的鉴权token"

data = {
  "title": "title",
  "content": "<html lang=\"en-US\"><body><a href=\"https://bot.q.qq.com/wiki\" title=\"QQ机器人文档Title\">QQ机器人文档</a>\n<ul><li>主动消息:发送消息时,未填msg_id字段的消息。</li><li>被动消息:发送消息时,填充了msg_id字段的消息。</li></ul></body></html>",
  "format": 2
}

headers = {
    "Content-Type": "application/json",
    "Authorization": Authorization
}

response = requests.put(url, headers=headers, json=data).json()

print(response)

请求数据包

{
  "title": "title",
  "content": "<html lang=\"en-US\"><body><a href=\"https://bot.q.qq.com/wiki\" title=\"QQ机器人文档Title\">QQ机器人文档</a>\n<ul><li>主动消息:发送消息时,未填msg_id字段的消息。</li><li>被动消息:发送消息时,填充了msg_id字段的消息。</li></ul></body></html>",
  "format": 2
}

响应数据包

{
  "task_id": "1645413752912602306",
  "create_time": "1645503180"
}

删除帖子

接口

DELETE /channels/{channel_id}/threads/{thread_id}

功能描述
用于删除指定子频道下的某个帖子。

Content-Type
application/json

返回
成功返回 HTTP 状态码 204

错误码
详见错误码

Python示例

import requests

channel_id = "你的子频道ID"
thread_id = "帖子ID"
url = f"https://api.sgroup.qq.com/channels/{channel_id}/threads/{thread_id}"
Authorization = f"QQBot 你的鉴权token"

headers = {
    "Authorization": Authorization
}

response = requests.delete(url, headers=headers)

if response.status_code == 204:
    print("帖子删除成功")
else:
    print("删除帖子失败,状态码:", response.status_code)

请求数据包

DELETE /channels/123456/threads/112233

致谢和更新

频道模块目前制作完成,内容会在原基础上持续更新,具体更新时间以下面⬇️更新时间为准。
文章持续更新,如果三连支持,速更!!!
请在评论区提出疑惑和建议
上次更新: 9/13/2024, AM
在这里插入图片描述

⬅️第五期:频道模块之接口授权管理和发言管理
➡️第七期:WebSocket方式 (待更新…)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃点李子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值