-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcontact-bot.py
72 lines (55 loc) · 2.08 KB
/
contact-bot.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""contact bot"""
import asyncio
import logging
from typing import Optional
from wechaty import Wechaty, Contact
from wechaty_puppet import ScanStatus, ContactType # type: ignore
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
class MyBot(Wechaty):
"""
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""
def __init__(self):
super().__init__()
async def on_login(self, contact: Contact):
print(f'user: {contact} has login')
contacts = await self.Contact.find_all()
log.info('bot #######################')
log.info('bot Contact number: %s', len(contacts))
# official
for i, contact in enumerate(contacts):
if contact.type() == ContactType.CONTACT_TYPE_OFFICIAL:
log.info('Bot Official %d :%s', i, contact)
elif contact.type() == ContactType.CONTACT_TYPE_PERSONAL:
log.info('Bot Personal :%d %s %s',
i, contact.name, contact.contact_id)
# get contact avatar url
max_num = 17
for i, contact in enumerate(contacts[:max_num]):
avatar_url = contact.payload.avatar
log.info(f'Bot Contact: {contact.name} with avatar url'
f' {avatar_url}')
# send msg to someone
for i, contact in enumerate(contacts):
if contact.payload.alias == 'lover':
await contact.say('my love')
async def on_scan(self,
qr_code: str,
status: ScanStatus,
data: Optional[str] = None):
if status == ScanStatus.Waiting:
print("qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
else:
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
bot: Optional[MyBot] = None
async def main():
"""doc"""
# pylint: disable=W0603
global bot
bot = MyBot()
await bot.start()
asyncio.run(main())