Skip to content

Commit bf971ad

Browse files
committed
Merge branch 'master' of github.com:wechaty/python-wechaty-getting-started
2 parents 08a7ae9 + 3859690 commit bf971ad

File tree

11 files changed

+455
-7
lines changed

11 files changed

+455
-7
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.4.11
1+
0.4.14

examples/advanced/__init__.py

Whitespace-only changes.

examples/advanced/busy-bot.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""doc"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
from typing import Optional
6+
7+
from wechaty_puppet import ScanStatus # type: ignore
8+
9+
from wechaty import Wechaty, Contact
10+
from wechaty.user import Message
11+
12+
logging.basicConfig(level=logging.INFO)
13+
log = logging.getLogger(__name__)
14+
15+
16+
class MyBot(Wechaty):
17+
"""
18+
listen wechaty event with inherited functions, which is more friendly for
19+
oop developer
20+
"""
21+
def __init__(self):
22+
super().__init__()
23+
self.busy = False
24+
self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back."
25+
26+
async def on_message(self, msg: Message):
27+
"""
28+
listen for message event
29+
"""
30+
from_contact = msg.talker()
31+
text = msg.text()
32+
room = msg.room()
33+
to = msg.to()
34+
if room is None and to.contact_id == self.contact_id:
35+
# say msg to the bot
36+
if text == '#status':
37+
msg = 'busy' if self.busy else 'free'
38+
await from_contact.say(
39+
f'My status: {msg}')
40+
await from_contact.say(self.auto_reply_comment)
41+
elif text == '#free':
42+
await from_contact.say('auto reply stopped.')
43+
elif text == '#busy':
44+
self.busy= True
45+
await from_contact.say('auto reply enabled')
46+
else:
47+
await from_contact.say(self.auto_reply_comment)
48+
49+
50+
async def on_login(self, contact: Contact):
51+
print(f'user: {contact} has login')
52+
53+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
54+
data: Optional[str] = None):
55+
contact = self.Contact.load(self.contact_id)
56+
print(f'user <{contact}> scan status: {status.name} , '
57+
f'qr_code: {qr_code}')
58+
59+
60+
bot: Optional[MyBot] = None
61+
62+
63+
async def main():
64+
"""doc"""
65+
# pylint: disable=W0603
66+
global bot
67+
bot = MyBot()
68+
await bot.start()
69+
70+
71+
asyncio.run(main())

examples/advanced/scheduler-bot.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""doc"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
from typing import Optional, Union
6+
7+
from wechaty_puppet import ScanStatus # type: ignore
8+
9+
from wechaty import Wechaty, Contact, Room
10+
from wechaty.user import Message
11+
12+
from apscheduler.schedulers.asyncio import AsyncIOScheduler
13+
14+
logging.basicConfig(level=logging.INFO)
15+
log = logging.getLogger(__name__)
16+
17+
18+
class MyBot(Wechaty):
19+
"""
20+
listen wechaty event with inherited functions, which is more friendly for
21+
oop developer
22+
"""
23+
def __init__(self):
24+
super().__init__()
25+
self.busy = False
26+
self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back."
27+
28+
async def message(msg: Message):
29+
"""back on message"""
30+
from_contact = msg.talker()
31+
text = msg.text()
32+
room = msg.room()
33+
if text == '#ding':
34+
conversation: Union[
35+
Room, Contact] = from_contact if room is None else room
36+
await conversation.ready()
37+
await conversation.say('dong')
38+
39+
async def on_login(self, contact: Contact):
40+
print(f'user: {contact} has login')
41+
42+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
43+
data: Optional[str] = None):
44+
contact = self.Contact.load(self.contact_id)
45+
print(f'user <{contact}> scan status: {status.name} , '
46+
f'qr_code: {qr_code}')
47+
48+
49+
bot: Optional[MyBot] = None
50+
51+
52+
async def tick(bot: Wechaty):
53+
room = bot.Room.load('room-id')
54+
await room.ready()
55+
from datetime import datetime
56+
await room.say(f'say ding automatically, {datetime.now()}')
57+
await room.say('ding')
58+
59+
60+
async def main():
61+
"""doc"""
62+
# pylint: disable=W0603
63+
global bot
64+
bot = MyBot()
65+
66+
scheduler = AsyncIOScheduler()
67+
scheduler.add_job(tick, 'interval', seconds=10, args=[bot])
68+
69+
scheduler.start()
70+
await bot.start()
71+
72+
73+
asyncio.run(main())

examples/basic/__init__.py

Whitespace-only changes.

examples/basic/contact-bot.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""contact bot"""
2+
import asyncio
3+
import logging
4+
from typing import Optional
5+
6+
from wechaty_puppet import ScanStatus, ContactType # type: ignore
7+
8+
from wechaty import Wechaty, Contact
9+
10+
logging.basicConfig(level=logging.INFO)
11+
log = logging.getLogger(__name__)
12+
13+
14+
class MyBot(Wechaty):
15+
"""
16+
listen wechaty event with inherited functions, which is more friendly for
17+
oop developer
18+
"""
19+
def __init__(self):
20+
super().__init__()
21+
22+
async def on_login(self, contact: Contact):
23+
print(f'user: {contact} has login')
24+
contacts = await self.Contact.find_all()
25+
log.info('bot #######################')
26+
log.info('bot Contact number: %s', len(contacts))
27+
28+
# official
29+
for i, contact in enumerate(contacts):
30+
if contact.type() == ContactType.CONTACT_TYPE_OFFICIAL:
31+
log.info('Bot Official %d :%s', i, contact)
32+
elif contact.type() == ContactType.CONTACT_TYPE_PERSONAL:
33+
log.info('Bot Personal :%d %s %s',
34+
i, contact.name, contact.contact_id)
35+
36+
# get contact avatar url
37+
max_num = 17
38+
for i, contact in enumerate(contacts[:max_num]):
39+
avatar_url = contact.payload.avatar
40+
log.info(f'Bot Contact: {contact.name} with avatar url'
41+
f' {avatar_url}')
42+
43+
# send msg to someone
44+
45+
for i, contact in enumerate(contacts):
46+
if contact.payload.alias == 'lover':
47+
await contact.say('my love')
48+
49+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
50+
data: Optional[str] = None):
51+
contact = self.Contact.load(self.contact_id)
52+
print(f'user <{contact}> scan status: {status.name} , '
53+
f'qr_code: {qr_code}')
54+
55+
56+
bot: Optional[MyBot] = None
57+
58+
59+
async def main():
60+
"""doc"""
61+
# pylint: disable=W0603
62+
global bot
63+
bot = MyBot()
64+
await bot.start()
65+
66+
67+
asyncio.run(main())

examples/basic/ding-dong-bot-oop.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""doc"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
import os
6+
from typing import Optional, Union
7+
8+
from wechaty_puppet import FileBox, ScanStatus # type: ignore
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 text == '#ding':
33+
conversation: Union[
34+
Room, Contact] = from_contact if room is None else room
35+
await conversation.ready()
36+
await conversation.say('dong')
37+
file_box = FileBox.from_url(
38+
'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
39+
'u=1116676390,2305043183&fm=26&gp=0.jpg',
40+
name='ding-dong.jpg')
41+
await conversation.say(file_box)
42+
43+
async def on_login(self, contact: Contact):
44+
print(f'user: {contact} has login')
45+
46+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
47+
data: Optional[str] = None):
48+
contact = self.Contact.load(self.contact_id)
49+
print(f'user <{contact}> scan status: {status.name} , '
50+
f'qr_code: {qr_code}')
51+
52+
53+
bot: Optional[MyBot] = None
54+
55+
56+
async def main():
57+
"""doc"""
58+
# pylint: disable=W0603
59+
global bot
60+
bot = MyBot()
61+
await bot.start()
62+
63+
64+
asyncio.run(main())

examples/ding-dong-bot.py

100644100755
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
"""
22
Python Wechaty - https://github.com/wechaty/python-wechaty
3-
43
Authors: Huan LI (李卓桓) <https://github.com/huan>
54
Jingjing WU (吴京京) <https://github.com/wj-Mcat>
6-
75
2020 @ Copyright Wechaty Contributors <https://github.com/wechaty>
8-
96
Licensed under the Apache License, Version 2.0 (the 'License');
107
you may not use this file except in compliance with the License.
118
You may obtain a copy of the License at
12-
139
http://www.apache.org/licenses/LICENSE-2.0
14-
1510
Unless required by applicable law or agreed to in writing, software
1611
distributed under the License is distributed on an 'AS IS' BASIS,
1712
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import asyncio
2+
import logging
3+
from typing import Optional, Union
4+
5+
from wechaty_puppet import PuppetOptions, FileBox # type: ignore
6+
from wechaty_puppet_hostie import HostiePuppet # type: ignore
7+
8+
from wechaty import Wechaty, Contact
9+
from wechaty.user import Message, Room
10+
11+
from .tencentaiplat import TencentAI
12+
13+
logging.basicConfig(
14+
level=logging.INFO,
15+
format='%(asctime)s %(levelname)s %(filename)s <%(funcName)s> %(message)s',
16+
datefmt='%Y-%m-%d %H:%M:%S'
17+
)
18+
log = logging.getLogger('DingDongBot')
19+
20+
chat_friend: list = []
21+
22+
async def message(msg: Message):
23+
"""back on message"""
24+
from_contact = msg.talker()
25+
text = msg.text()
26+
room = msg.room()
27+
conversation: Union[
28+
Room, Contact] = from_contact if room is None else room
29+
30+
global chat_friend
31+
32+
if "#关闭闲聊" == text:
33+
try:
34+
chat_friend.remove(conversation)
35+
except Exception as e:
36+
return
37+
await conversation.ready()
38+
await conversation.say('好的,有需要随时叫我')
39+
return
40+
41+
elif "#开启闲聊" == text:
42+
chat_friend.append(conversation)
43+
await conversation.ready()
44+
await conversation.say('闲聊功能开启成功!现在你可以和我聊天啦!')
45+
return
46+
47+
if conversation in chat_friend:
48+
data = TencentAI(text)
49+
await conversation.ready()
50+
await conversation.say(data)
51+
return
52+
53+
if text == '#ding':
54+
await conversation.ready()
55+
await conversation.say('dong')
56+
57+
file_box = FileBox.from_url(
58+
'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
59+
'u=1116676390,2305043183&fm=26&gp=0.jpg',
60+
name='ding-dong.jpg')
61+
await conversation.say(file_box)
62+
63+
64+
bot: Optional[Wechaty] = None
65+
66+
67+
async def main():
68+
"""doc"""
69+
# pylint: disable=W0603
70+
global bot
71+
bot = Wechaty().on('message', message)
72+
await bot.start()
73+
74+
75+
asyncio.run(main())

0 commit comments

Comments
 (0)