Skip to content

Commit ccd2a75

Browse files
authored
Merge pull request #11 from wj-Mcat/master
add friendship & scheduler bot
2 parents dd95e47 + 7edc143 commit ccd2a75

File tree

3 files changed

+136
-24
lines changed

3 files changed

+136
-24
lines changed

examples/advanced/busy-bot.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1-
"""doc"""
2-
# pylint: disable=R0801
1+
"""
2+
Python Wechaty - https://github.com/wechaty/python-wechaty
3+
Authors: Huan LI (李卓桓) <https://github.com/huan>
4+
Jingjing WU (吴京京) <https://github.com/wj-Mcat>
5+
2020 @ Copyright Wechaty Contributors <https://github.com/wechaty>
6+
Licensed under the Apache License, Version 2.0 (the 'License');
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an 'AS IS' BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
316
import asyncio
417
import logging
518
from typing import Optional
619

20+
# waiting for wechaty-puppet new version
721
from wechaty_puppet import ScanStatus # type: ignore
822

9-
from wechaty import Wechaty, Contact
10-
from wechaty.user import Message
23+
from wechaty import (
24+
Wechaty, Contact, Message
25+
)
1126

1227
logging.basicConfig(level=logging.INFO)
1328
log = logging.getLogger(__name__)
@@ -62,7 +77,6 @@ async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
6277

6378
async def main():
6479
"""doc"""
65-
# pylint: disable=W0603
6680
global bot
6781
bot = MyBot()
6882
await bot.start()

examples/advanced/friendship-bot.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
Python Wechaty - https://github.com/wechaty/python-wechaty
3+
Authors: Huan LI (李卓桓) <https://github.com/huan>
4+
Jingjing WU (吴京京) <https://github.com/wj-Mcat>
5+
2020 @ Copyright Wechaty Contributors <https://github.com/wechaty>
6+
Licensed under the Apache License, Version 2.0 (the 'License');
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an 'AS IS' BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
import os
17+
import asyncio
18+
from typing import Optional
19+
20+
from wechaty import (
21+
Wechaty, Contact, Friendship, FriendshipType
22+
)
23+
24+
25+
class MyBot(Wechaty):
26+
"""
27+
listen wechaty event with inherited functions, which is more friendly for
28+
oop developer
29+
"""
30+
def __init__(self):
31+
super().__init__()
32+
self.busy = False
33+
self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back."
34+
35+
async def on_friendship(self, friendship: Friendship):
36+
administrator = bot.Contact.load('admin-id')
37+
await administrator.ready()
38+
39+
contact = friendship.contact()
40+
await contact.ready()
41+
42+
log_msg = f'receive "friendship" message from {contact.name}'
43+
await administrator.say(log_msg)
44+
45+
if friendship.type() == FriendshipType.FRIENDSHIP_TYPE_RECEIVE:
46+
if friendship.hello() == 'ding':
47+
log_msg = 'accepted automatically because verify messsage is "ding"'
48+
print('before accept ...')
49+
await friendship.accept()
50+
# if want to send msg, you need to delay sometimes
51+
52+
print('waiting to send message ...')
53+
await asyncio.sleep(3)
54+
await contact.say('hello from wechaty ...')
55+
print('after accept ...')
56+
else:
57+
log_msg = 'not auto accepted, because verify message is: ' + friendship.hello()
58+
59+
elif friendship.type() == FriendshipType.FRIENDSHIP_TYPE_CONFIRM:
60+
log_msg = 'friend ship confirmed with ' + contact.name
61+
62+
print(log_msg)
63+
await administrator.say(log_msg)
64+
65+
async def on_login(self, contact: Contact):
66+
print(f'user: {contact} has login')
67+
68+
69+
bot: Optional[MyBot] = None
70+
71+
72+
async def main():
73+
"""Async Main Entry"""
74+
if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
75+
print('''
76+
Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
77+
You need a TOKEN to run the Java Wechaty. Please goto our README for details
78+
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
79+
''')
80+
81+
global bot
82+
bot = MyBot()
83+
await bot.start()
84+
85+
86+
asyncio.run(main())
87+

examples/advanced/scheduler-bot.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
"""doc"""
2-
# pylint: disable=R0801
1+
"""
2+
Python Wechaty - https://github.com/wechaty/python-wechaty
3+
Authors: Huan LI (李卓桓) <https://github.com/huan>
4+
Jingjing WU (吴京京) <https://github.com/wj-Mcat>
5+
2020 @ Copyright Wechaty Contributors <https://github.com/wechaty>
6+
Licensed under the Apache License, Version 2.0 (the 'License');
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an 'AS IS' BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
import os
317
import asyncio
4-
import logging
518
from typing import Optional, Union
619

7-
from wechaty_puppet import ScanStatus # type: ignore
8-
920
from wechaty import Wechaty, Contact, Room
1021
from wechaty.user import Message
1122

1223
from apscheduler.schedulers.asyncio import AsyncIOScheduler
1324

14-
logging.basicConfig(level=logging.INFO)
15-
log = logging.getLogger(__name__)
16-
1725

1826
class MyBot(Wechaty):
1927
"""
@@ -25,7 +33,7 @@ def __init__(self):
2533
self.busy = False
2634
self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back."
2735

28-
async def message(msg: Message):
36+
async def message(self, msg: Message):
2937
"""back on message"""
3038
from_contact = msg.talker()
3139
text = msg.text()
@@ -39,27 +47,30 @@ async def message(msg: Message):
3947
async def on_login(self, contact: Contact):
4048
print(f'user: {contact} has login')
4149

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-
4850

4951
bot: Optional[MyBot] = None
5052

5153

5254
async def tick(bot: Wechaty):
55+
"""
56+
find a specific room, and say something to it.
57+
"""
5358
room = bot.Room.load('room-id')
5459
await room.ready()
5560
from datetime import datetime
56-
await room.say(f'say ding automatically, {datetime.now()}')
57-
await room.say('ding')
61+
await room.say(f'it"s a new day, let"s welcome, now it"s {datetime.now()}')
62+
await room.say('hello world !')
5863

5964

6065
async def main():
61-
"""doc"""
62-
# pylint: disable=W0603
66+
"""Async Main Entry"""
67+
if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ:
68+
print('''
69+
Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables
70+
You need a TOKEN to run the Java Wechaty. Please goto our README for details
71+
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token
72+
''')
73+
6374
global bot
6475
bot = MyBot()
6576

0 commit comments

Comments
 (0)