forked from tencent-connect/botpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_c2c_reply_file.py
43 lines (34 loc) · 1.39 KB
/
demo_c2c_reply_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
import asyncio
import os
import botpy
from botpy import logging
from botpy.ext.cog_yaml import read
from botpy.message import C2CMessage
test_config = read(os.path.join(os.path.dirname(__file__), "config.yaml"))
_log = logging.get_logger()
class MyClient(botpy.Client):
async def on_ready(self):
_log.info(f"robot 「{self.robot.name}」 on_ready!")
async def on_c2c_message_create(self, message: C2CMessage):
file_url = "" # 这里需要填写上传的资源Url
uploadMedia = await message._api.post_c2c_file(
openid=message.author.user_openid,
file_type=1, # 文件类型要对应上,具体支持的类型见方法说明
url=file_url # 文件Url
)
# 资源上传后,会得到Media,用于发送消息
await message._api.post_c2c_message(
openid=message.author.user_openid,
msg_type=7, # 7表示富媒体类型
msg_id=message.id,
media=uploadMedia
)
if __name__ == "__main__":
# 通过预设置的类型,设置需要监听的事件通道
# intents = botpy.Intents.none()
# intents.public_messages=True
# 通过kwargs,设置需要监听的事件通道
intents = botpy.Intents(public_messages=True)
client = MyClient(intents=intents)
client.run(appid=test_config["appid"], secret=test_config["secret"])