Skip to content

Commit

Permalink
feat: 支持创建子频道类型的全局公告 (#31)
Browse files Browse the repository at this point in the history
* fix:添加了撤回消息接口和与公告相关的接口

* feat: 增加表情表态的发送与撤回能力

* feat: 增加表情表态的发送与撤回能力

* feat: 增加表情表态的发送与撤回能力

* feat: 增加设置精华消息API

* feat: 拉取子频道列表附带机器人对子频道权限信息

feat: 增加表情表态的发送与撤回能力

* fix: 修改命名

* fix: 添加精华消息demo

* fix: pinsAPI异步拉取问题先回退

* feat: 拉取子频道列表附带机器人对子频道权限信息

* fix: 移除无用代码

* feat: post_message 的 content 可以为空

* feat: 支持创建子频道类型的全局公告

* feat: 支持创建子频道类型的全局公告

* fix: 修复流水线单测不通过的问题

* feat: 支持创建子频道类型的全局公告

* feat: 支持创建子频道类型的全局公告

* fix: 修复流水线单测不通过的问题

* feat: 支持创建子频道类型的全局公告

Co-authored-by: jichendai <jichendai@tencent.com>
  • Loading branch information
DaiJiChen and jichendai committed Mar 6, 2022
1 parent a16aa31 commit 457ce70
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 15 deletions.
12 changes: 11 additions & 1 deletion examples/demo_announce.py
Expand Up @@ -4,7 +4,12 @@

import qqbot
from qqbot.core.util.yaml_util import YamlUtil
from qqbot.model.announce import CreateAnnounceRequest, CreateChannelAnnounceRequest
from qqbot.model.announce import (
RecommendChannel,
CreateAnnounceRequest,
RecommendChannelRequest,
CreateChannelAnnounceRequest
)

test_config = YamlUtil.read(os.path.join(os.path.dirname(__file__), "config.yaml"))

Expand Down Expand Up @@ -37,6 +42,11 @@ async def _announce_handler(event, message: qqbot.Message):
elif "/删子频道公告" in message.content:
await announce_api.delete_channel_announce(message.channel_id, message_id)

elif "/设置推荐子频道" in message.content:
channel_list = [RecommendChannel(message.channel_id, "introduce")]
request = RecommendChannelRequest(0, channel_list)
await announce_api.post_recommended_channels(message.guild_id, request)


if __name__ == "__main__":
t_token = qqbot.Token(test_config["token"]["appid"], test_config["token"]["token"])
Expand Down
15 changes: 15 additions & 0 deletions qqbot/api.py
Expand Up @@ -11,6 +11,7 @@
Announce,
CreateAnnounceRequest,
CreateChannelAnnounceRequest,
RecommendChannelRequest,
)
from qqbot.model.api_permission import (
APIPermission,
Expand Down Expand Up @@ -682,6 +683,20 @@ def delete_channel_announce(self, channel_id: str, message_id: str):
response = self.http.delete(url)
return response.status_code == HttpStatus.NO_CONTENT

def post_recommended_channels(
self, guild_id: str, request: RecommendChannelRequest
) -> Announce:
"""
创建子频道类型的频道全局公告
:param guild_id: 频道ID
:param request: RecommendChannelRequest 对象
"""
url = get_url(APIConstant.guildAnnounceURI, self.is_sandbox).format(guild_id=guild_id)
request_json = JsonUtil.obj2json_serialize(request)
response = self.http.post(url, request_json)
return json.loads(response.content, object_hook=Announce)


class APIPermissionAPI(APIBase):
"""接口权限接口"""
Expand Down
15 changes: 15 additions & 0 deletions qqbot/async_api.py
Expand Up @@ -14,6 +14,7 @@
CreateAnnounceRequest,
Announce,
CreateChannelAnnounceRequest,
RecommendChannelRequest
)
from qqbot.model.api_permission import (
APIPermission,
Expand Down Expand Up @@ -701,6 +702,20 @@ async def delete_channel_announce(self, channel_id: str, message_id: str):
response = await self.http_async.delete(url)
return response == ""

async def post_recommended_channels(
self, guild_id: str, request: RecommendChannelRequest
) -> Announce:
"""
创建子频道类型的频道全局公告
:param guild_id: 频道ID
:param request: RecommendChannelRequest 对象
"""
url = get_url(APIConstant.guildAnnounceURI, self.is_sandbox).format(guild_id=guild_id)
request_json = JsonUtil.obj2json_serialize(request)
response = await self.http_async.post(url, request_json)
return json.loads(response, object_hook=Announce)


class AsyncAPIPermissionAPI(AsyncAPIBase):
"""接口权限接口"""
Expand Down
15 changes: 15 additions & 0 deletions qqbot/model/announce.py
@@ -1,15 +1,24 @@
# -*- coding: utf-8 -*-
from typing import List


class Announce:
def __init__(self, data=None):
self.guild_id: str = ""
self.channel_id: str = ""
self.message_id: str = ""
self.announces_type: int = 0
self.recommend_channels: List[RecommendChannel] = []
if data:
self.__dict__ = data


class RecommendChannel:
def __init__(self, channel_id: str, introduce: str):
self.channel_id = channel_id
self.introduce = introduce


class CreateAnnounceRequest:
def __init__(self, channel_id: str, message_id: str):
self.channel_id = channel_id
Expand All @@ -19,3 +28,9 @@ def __init__(self, channel_id: str, message_id: str):
class CreateChannelAnnounceRequest:
def __init__(self, message_id: str):
self.message_id = message_id


class RecommendChannelRequest:
def __init__(self, announces_type: int, recommend_channels: List[RecommendChannel]):
self.announces_type = announces_type
self.recommend_channels = recommend_channels
40 changes: 32 additions & 8 deletions tests/test_api.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import unittest

import qqbot
Expand All @@ -9,6 +10,10 @@
ServerError,
)
from qqbot.core.util import logging
from qqbot.model.announce import (
RecommendChannel,
RecommendChannelRequest
)
from qqbot.model.api_permission import (
PermissionDemandToCreate,
APIPermissionDemandIdentify,
Expand Down Expand Up @@ -220,6 +225,16 @@ def test_mute_member(self):
self.assertEqual(True, result)


class AnnounceTestCase(unittest.TestCase):
api = qqbot.AnnouncesAPI(token, IS_SANDBOX)

def test_post_recommend_channel(self):
channel_list = [RecommendChannel(CHANNEL_ID, "introduce")]
request = RecommendChannelRequest(0, channel_list)
result = self.api.post_recommended_channels(GUILD_ID, request)
self.assertEqual(len(channel_list), len(result.recommend_channels))


class APIPermissionTestCase(unittest.TestCase):
api = qqbot.APIPermissionAPI(token, IS_SANDBOX)

Expand Down Expand Up @@ -247,30 +262,39 @@ def test_get_schedules(self):


class APIReactionTestCase(unittest.TestCase):
# 流水线上的机器人没有开通表情表态权限,所以暂用另一个机器人
channel_id = "2568610"
message_id = "088de19cbeb883e7e97110a2e39c0138c70448b6a0839106"
token = qqbot.Token("101989567", "BDwn6fEcIpjdDArVmwmU2QHkhQ3AuKTg")
api = qqbot.ReactionAPI(token, IS_SANDBOX)

def test_put_reaction(self):
result = self.api.put_reaction(CHANNEL_ID, MESSAGE_ID, EmojiType.system, "4")
def test_put_and_delete_reaction(self):
result = self.api.put_reaction(self.channel_id, self.message_id, EmojiType.system, "5")
self.assertEqual(True, result)

def test_delete_reaction(self):
result = self.api.delete_reaction(CHANNEL_ID, MESSAGE_ID, EmojiType.system, "4")
time.sleep(1) # 表情表态操作有频率限制,中间隔一秒

result = self.api.delete_reaction(self.channel_id, self.message_id, EmojiType.system, "5")
self.assertEqual(True, result)


class APIPinsTestCase(unittest.TestCase):
# 流水线上的机器人没有开通精华消息权限,所以暂用另一个机器人
channel_id = "2568610"
message_id = "088de19cbeb883e7e97110a2e39c0138c70448b6a0839106"
token = qqbot.Token("101989567", "BDwn6fEcIpjdDArVmwmU2QHkhQ3AuKTg")
api = qqbot.PinsAPI(token, IS_SANDBOX)

def test_put_pin(self):
result = self.api.put_pin(CHANNEL_ID, MESSAGE_ID)
self.assertTrue(MESSAGE_ID in result.message_ids)
result = self.api.put_pin(self.channel_id, self.message_id)
self.assertTrue(self.message_id in result.message_ids)

def test_delete_pin(self):
result = self.api.delete_pin(CHANNEL_ID, MESSAGE_ID)
result = self.api.delete_pin(self.channel_id, self.message_id)
self.assertEqual(True, result)

def test_get_pins(self):
result = self.api.get_pins(CHANNEL_ID)
result = self.api.get_pins(self.channel_id)
self.assertTrue(len(result.message_ids) >= 0)


Expand Down
33 changes: 27 additions & 6 deletions tests/test_async_api.py
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import time
import unittest

import qqbot
Expand All @@ -10,6 +11,10 @@
ServerError,
)
from qqbot.core.util import logging
from qqbot.model.announce import (
RecommendChannel,
RecommendChannelRequest
)
from qqbot.model.api_permission import (
APIPermissionDemandIdentify,
PermissionDemandToCreate,
Expand Down Expand Up @@ -247,18 +252,29 @@ class MuteTestCase(unittest.TestCase):
loop = asyncio.get_event_loop()

def test_mute_all(self):
option = qqbot.MuteOption(mute_seconds="120")
option = qqbot.MuteOption(mute_seconds="20")
result = self.loop.run_until_complete(self.api.mute_all(GUILD_ID, option))
self.assertEqual(True, result)

def test_mute_member(self):
option = qqbot.MuteOption(mute_seconds="120")
option = qqbot.MuteOption(mute_seconds="20")
result = self.loop.run_until_complete(
self.api.mute_member(GUILD_ID, GUILD_TEST_MEMBER_ID, option)
)
self.assertEqual(True, result)


class AnnounceTestCase(unittest.TestCase):
api = qqbot.AsyncAnnouncesAPI(token, IS_SANDBOX)
loop = asyncio.get_event_loop()

def test_post_recommend_channel(self):
channel_list = [RecommendChannel(CHANNEL_ID, "introduce")]
request = RecommendChannelRequest(0, channel_list)
result = self.loop.run_until_complete(self.api.post_recommended_channels(GUILD_ID, request))
self.assertEqual(len(channel_list), len(result.recommend_channels))


class APIPermissionTestCase(unittest.TestCase):
api = qqbot.AsyncAPIPermissionAPI(token, IS_SANDBOX)
loop = asyncio.get_event_loop()
Expand Down Expand Up @@ -292,18 +308,23 @@ def test_get_schedules(self):


class APIReactionTestCase(unittest.TestCase):
# 流水线上的机器人没有开通表情表态权限,所以暂用另一个机器人
channel_id = "2568610"
message_id = "088de19cbeb883e7e97110a2e39c0138c70448b6a0839106"
token = qqbot.Token("101989567", "BDwn6fEcIpjdDArVmwmU2QHkhQ3AuKTg")
api = qqbot.AsyncReactionAPI(token, IS_SANDBOX)
loop = asyncio.get_event_loop()

def test_put_reaction(self):
def test_delete_reaction(self):
result = self.loop.run_until_complete(
self.api.put_reaction(CHANNEL_ID, MESSAGE_ID, EmojiType.system, "4")
self.api.put_reaction(self.channel_id, self.message_id, EmojiType.system, "4")
)
self.assertEqual(True, result)

def test_delete_reaction(self):
time.sleep(1) # 表情表态操作有频率限制,中间隔一秒

result = self.loop.run_until_complete(
self.api.delete_reaction(CHANNEL_ID, MESSAGE_ID, EmojiType.system, "4")
self.api.delete_reaction(self.channel_id, self.message_id, EmojiType.system, "4")
)
self.assertEqual(True, result)

Expand Down

0 comments on commit 457ce70

Please sign in to comment.