Skip to content

Commit 6027ac6

Browse files
committed
update mini-program bot
1 parent 9f01dcb commit 6027ac6

File tree

1 file changed

+9
-24
lines changed

1 file changed

+9
-24
lines changed

examples/advanced/mini-program-bot.py

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import asyncio
44
import logging
55
from typing import Optional, Union
6+
import json
7+
from dataclasses import asdict
68

79
from wechaty_puppet import FileBox, ScanStatus # type: ignore
810
from wechaty_puppet import MessageType
@@ -34,33 +36,16 @@ async def on_message(self, msg: Message):
3436

3537
if msg.type() == MessageType.MESSAGE_TYPE_MINI_PROGRAM:
3638
mini_program = await msg.to_mini_program()
37-
await room.say(mini_program)
3839

39-
if text == 'send card':
40-
# find one of my friend
41-
contacts = await bot.Contact.find_all()
42-
if contacts:
43-
# send one of my friend to the talker
44-
await from_contact.say(contacts[0])
45-
print('have sended')
46-
elif msg.type() == MessageType.MESSAGE_TYPE_IMAGE:
47-
img = await msg.to_file_box()
48-
await img.to_file(f'./{img.name}')
40+
# save the mini-program data as string
41+
mini_program_data = asdict(mini_program.payload)
4942

50-
await room.say(img)
43+
# load the min-program
44+
loaded_mini_program = self.MiniProgram.create_from_json(
45+
payload_data=mini_program_data
46+
)
5147

52-
elif msg.type() == MessageType.MESSAGE_TYPE_VIDEO:
53-
video = await msg.to_file_box()
54-
await video.to_file(f'./{video.name}')
55-
56-
await room.say(video)
57-
58-
elif msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
59-
audio = await msg.to_file_box()
60-
# save the audio file as local file
61-
await audio.to_file(f'./{audio.name}')
62-
63-
print('done')
48+
await room.say(loaded_mini_program)
6449

6550
async def on_login(self, contact: Contact):
6651
"""login event. It will be triggered every time you login"""

0 commit comments

Comments
 (0)