Skip to content

Commit 1bc37b2

Browse files
committed
add message file bot
1 parent 76254ab commit 1bc37b2

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,6 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130-
token.txt
130+
token.txt
131+
.idea/
132+
**/logs/

examples/advanced/message-file-bot.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""send contact card to specific contact"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
from typing import Optional, Union
6+
7+
from wechaty_puppet import FileBox, ScanStatus # type: ignore
8+
from wechaty_puppet import MessageType
9+
10+
from wechaty import Wechaty, Contact
11+
from wechaty.user import Message, Room
12+
13+
logging.basicConfig(level=logging.INFO)
14+
log = logging.getLogger(__name__)
15+
16+
17+
class MyBot(Wechaty):
18+
"""
19+
listen wechaty event with inherited functions, which is more friendly for
20+
oop developer
21+
"""
22+
def __init__(self):
23+
super().__init__()
24+
25+
async def on_message(self, msg: Message):
26+
"""
27+
listen for message event
28+
"""
29+
from_contact = msg.talker()
30+
text = msg.text()
31+
room = msg.room()
32+
# send contact-card
33+
print(msg)
34+
if msg.type() == MessageType.MESSAGE_TYPE_CONTACT:
35+
contact = await msg.to_contact()
36+
room = self.Room.load('19961884194@chatroom')
37+
await room.ready()
38+
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}')
49+
50+
await room.say(img)
51+
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')
64+
65+
async def on_login(self, contact: Contact):
66+
"""login event. It will be triggered every time you login"""
67+
log.info(f'user: {contact} has login')
68+
69+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
70+
data: Optional[str] = None):
71+
"""scan event, It will be triggered when you scan the qrcode to login.
72+
And it will not be triggered when you have logined
73+
"""
74+
contact = self.Contact.load(self.contact_id)
75+
await contact.ready()
76+
print(f'user <{contact}> scan status: {status.name} , '
77+
f'qr_code: {qr_code}')
78+
79+
80+
bot: Optional[MyBot] = None
81+
82+
83+
async def main():
84+
"""doc"""
85+
# pylint: disable=W0603
86+
global bot
87+
bot = MyBot()
88+
await bot.start()
89+
90+
91+
asyncio.run(main())

0 commit comments

Comments
 (0)