Skip to content

Commit 037c4d8

Browse files
authored
Merge pull request #22 from wj-Mcat/master
add message file bot
2 parents 62bc67a + 1cb3205 commit 037c4d8

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,8 @@ dmypy.json
127127

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

examples/advanced/message-file-bot.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
if room:
33+
await room.ready()
34+
35+
# send contact-card
36+
if msg.type() == MessageType.MESSAGE_TYPE_CONTACT:
37+
# we can receive the contact-card event, and get the contact from message
38+
contact = await msg.to_contact()
39+
40+
if text == 'send card':
41+
# find one of my friend to send to `from_contact`
42+
contacts = await bot.Contact.find_all()
43+
if contacts:
44+
# send one of my friend to the talker
45+
# !! this interface is not supported now
46+
await from_contact.say(contacts[0])
47+
print('have sended')
48+
elif msg.type() == MessageType.MESSAGE_TYPE_IMAGE:
49+
img = await msg.to_file_box()
50+
# save the image as local file
51+
await img.to_file(f'./{img.name}')
52+
# send image file to the room
53+
if room:
54+
await room.say(img)
55+
56+
elif msg.type() == MessageType.MESSAGE_TYPE_VIDEO:
57+
video = await msg.to_file_box()
58+
# save the video as local file
59+
await video.to_file(f'./{video.name}')
60+
61+
# send video file to the room
62+
if room:
63+
await room.say(video)
64+
65+
elif msg.type() == MessageType.MESSAGE_TYPE_AUDIO:
66+
audio = await msg.to_file_box()
67+
# save the audio file as local file
68+
await audio.to_file(f'./{audio.name}')
69+
# !! we can't send audio to room/contact
70+
71+
print('done')
72+
73+
async def on_login(self, contact: Contact):
74+
"""login event. It will be triggered every time you login"""
75+
log.info(f'user: {contact} has login')
76+
77+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
78+
data: Optional[str] = None):
79+
"""scan event, It will be triggered when you scan the qrcode to login.
80+
And it will not be triggered when you have logined
81+
"""
82+
contact = self.Contact.load(self.contact_id)
83+
await contact.ready()
84+
print(f'user <{contact}> scan status: {status.name} , '
85+
f'qr_code: {qr_code}')
86+
87+
88+
bot: Optional[MyBot] = None
89+
90+
91+
async def main():
92+
"""doc"""
93+
# pylint: disable=W0603
94+
global bot
95+
bot = MyBot()
96+
await bot.start()
97+
98+
99+
asyncio.run(main())

examples/advanced/mini-program-bot.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""send contact card to specific contact"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
from typing import Optional, Union
6+
import json
7+
from dataclasses import asdict
8+
9+
from wechaty_puppet import FileBox, ScanStatus # type: ignore
10+
from wechaty_puppet import MessageType
11+
12+
from wechaty import Wechaty, Contact
13+
from wechaty.user import Message, Room
14+
15+
logging.basicConfig(level=logging.INFO)
16+
log = logging.getLogger(__name__)
17+
18+
19+
class MyBot(Wechaty):
20+
"""
21+
listen wechaty event with inherited functions, which is more friendly for
22+
oop developer
23+
"""
24+
def __init__(self):
25+
super().__init__()
26+
27+
async def on_message(self, msg: Message):
28+
"""
29+
listen for message event
30+
"""
31+
from_contact = msg.talker()
32+
text = msg.text()
33+
34+
room = self.Room.load('19961884194@chatroom')
35+
await room.ready()
36+
37+
if msg.type() == MessageType.MESSAGE_TYPE_MINI_PROGRAM:
38+
mini_program = await msg.to_mini_program()
39+
40+
# save the mini-program data as string
41+
mini_program_data = asdict(mini_program.payload)
42+
43+
# load the min-program
44+
loaded_mini_program = self.MiniProgram.create_from_json(
45+
payload_data=mini_program_data
46+
)
47+
48+
await room.say(loaded_mini_program)
49+
50+
async def on_login(self, contact: Contact):
51+
"""login event. It will be triggered every time you login"""
52+
log.info(f'user: {contact} has login')
53+
54+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
55+
data: Optional[str] = None):
56+
"""scan event, It will be triggered when you scan the qrcode to login.
57+
And it will not be triggered when you have logined
58+
"""
59+
contact = self.Contact.load(self.contact_id)
60+
await contact.ready()
61+
print(f'user <{contact}> scan status: {status.name} , '
62+
f'qr_code: {qr_code}')
63+
64+
65+
bot: Optional[MyBot] = None
66+
67+
68+
async def main():
69+
"""doc"""
70+
# pylint: disable=W0603
71+
global bot
72+
bot = MyBot()
73+
await bot.start()
74+
75+
76+
asyncio.run(main())

examples/ding-dong-bot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def on_message(msg: Message):
3939
await msg.say(file_box)
4040

4141

42-
async def on_scan(qrcode: str, status: int):
42+
async def on_scan(qrcode: str, status: int, data):
4343
"""
4444
Scan Handler for the Bot
4545
"""

0 commit comments

Comments
 (0)