Skip to content

Commit 1cb3205

Browse files
committed
update message-file-box room pattern
1 parent 80d3d6c commit 1cb3205

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

examples/advanced/message-file-bot.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ async def on_message(self, msg: Message):
2929
from_contact = msg.talker()
3030
text = msg.text()
3131
room = msg.room()
32+
if room:
33+
await room.ready()
34+
3235
# send contact-card
33-
print(msg)
3436
if msg.type() == MessageType.MESSAGE_TYPE_CONTACT:
3537
# we can receive the contact-card event, and get the contact from message
3638
contact = await msg.to_contact()
3739

38-
# you can send all file to the specific room when you are at testing stage
39-
room = self.Room.load('room-id')
40-
await room.ready()
41-
4240
if text == 'send card':
4341
# find one of my friend to send to `from_contact`
4442
contacts = await bot.Contact.find_all()
@@ -51,15 +49,18 @@ async def on_message(self, msg: Message):
5149
img = await msg.to_file_box()
5250
# save the image as local file
5351
await img.to_file(f'./{img.name}')
54-
# send image file to the specific room
55-
await room.say(img)
52+
# send image file to the room
53+
if room:
54+
await room.say(img)
5655

5756
elif msg.type() == MessageType.MESSAGE_TYPE_VIDEO:
5857
video = await msg.to_file_box()
5958
# save the video as local file
6059
await video.to_file(f'./{video.name}')
61-
# send video file to the specific room
62-
await room.say(video)
60+
61+
# send video file to the room
62+
if room:
63+
await room.say(video)
6364

6465
elif msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
6566
audio = await msg.to_file_box()

0 commit comments

Comments
 (0)