Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现群聊本地图片【base64编码】上传及其发送 #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions botpy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,27 @@ async def post_group_file(
route = Route("POST", "/v2/groups/{group_openid}/files", group_openid=group_openid)
return await self._http.request(route, json=payload)

async def post_group_base64file(
self,
group_openid: str,
file_type: int,
file_data: str,
srv_send_msg: bool = False,
) -> message.Media:
"""
上传/发送群聊图片

Args:
group_openid (str): 您要将消息发送到的群的 ID
file_type (int): 媒体类型:1 图片png/jpg,2 视频mp4,3 语音silk,4 文件(暂不开放)
file_data (str): 二进制文件的base64编码,可用于代替网络url资源,实现本地上传文件
srv_send_msg (bool): 设置 true 会直接发送消息到目标端,且会占用主动消息频次
"""
payload = locals()
payload.pop("self", None)
route = Route("POST", "/v2/groups/{group_openid}/files", group_openid=group_openid)
return await self._http.request(route, json=payload)

async def post_c2c_file(
self,
openid: str,
Expand Down
10 changes: 10 additions & 0 deletions examples/demo_group_reply_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ async def on_ready(self):
_log.info(f"robot 「{self.robot.name}」 on_ready!")

async def on_group_at_message_create(self, message: GroupMessage):

# 网络url资源
file_url = "" # 这里需要填写上传的资源Url
uploadMedia = await message._api.post_group_file(
group_openid=message.group_openid,
file_type=1, # 文件类型要对应上,具体支持的类型见方法说明
url=file_url # 文件Url
)

# 本地资源/图片
file_base64 = "" # 这里需要填写上传的资源的base64编码
uploadMedia = await message._api.post_group_base64file(
group_openid=message.group_openid,
file_type=1, # 文件类型要对应上,具体支持的类型见方法说明
file_data=file_base64 # 本地二进制文件的base64编码
)

# 资源上传后,会得到Media,用于发送消息
await message._api.post_group_message(
group_openid=message.group_openid,
Expand Down