diff --git a/examples/advanced/busy-bot.py b/examples/advanced/busy-bot.py index 3c8d249..fb9c431 100644 --- a/examples/advanced/busy-bot.py +++ b/examples/advanced/busy-bot.py @@ -1,13 +1,28 @@ -"""doc""" -# pylint: disable=R0801 +""" +Python Wechaty - https://github.com/wechaty/python-wechaty +Authors: Huan LI (李卓桓) + Jingjing WU (吴京京) +2020 @ Copyright Wechaty Contributors +Licensed under the Apache License, Version 2.0 (the 'License'); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an 'AS IS' BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" import asyncio import logging from typing import Optional +# waiting for wechaty-puppet new version from wechaty_puppet import ScanStatus # type: ignore -from wechaty import Wechaty, Contact -from wechaty.user import Message +from wechaty import ( + Wechaty, Contact, Message +) logging.basicConfig(level=logging.INFO) log = logging.getLogger(__name__) @@ -62,7 +77,6 @@ async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None, async def main(): """doc""" - # pylint: disable=W0603 global bot bot = MyBot() await bot.start() diff --git a/examples/advanced/friendship-bot.py b/examples/advanced/friendship-bot.py new file mode 100644 index 0000000..f53d94b --- /dev/null +++ b/examples/advanced/friendship-bot.py @@ -0,0 +1,87 @@ +""" +Python Wechaty - https://github.com/wechaty/python-wechaty +Authors: Huan LI (李卓桓) + Jingjing WU (吴京京) +2020 @ Copyright Wechaty Contributors +Licensed under the Apache License, Version 2.0 (the 'License'); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an 'AS IS' BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" +import os +import asyncio +from typing import Optional + +from wechaty import ( + Wechaty, Contact, Friendship, FriendshipType +) + + +class MyBot(Wechaty): + """ + listen wechaty event with inherited functions, which is more friendly for + oop developer + """ + def __init__(self): + super().__init__() + self.busy = False + self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back." + + async def on_friendship(self, friendship: Friendship): + administrator = bot.Contact.load('admin-id') + await administrator.ready() + + contact = friendship.contact() + await contact.ready() + + log_msg = f'receive "friendship" message from {contact.name}' + await administrator.say(log_msg) + + if friendship.type() == FriendshipType.FRIENDSHIP_TYPE_RECEIVE: + if friendship.hello() == 'ding': + log_msg = 'accepted automatically because verify messsage is "ding"' + print('before accept ...') + await friendship.accept() + # if want to send msg, you need to delay sometimes + + print('waiting to send message ...') + await asyncio.sleep(3) + await contact.say('hello from wechaty ...') + print('after accept ...') + else: + log_msg = 'not auto accepted, because verify message is: ' + friendship.hello() + + elif friendship.type() == FriendshipType.FRIENDSHIP_TYPE_CONFIRM: + log_msg = 'friend ship confirmed with ' + contact.name + + print(log_msg) + await administrator.say(log_msg) + + async def on_login(self, contact: Contact): + print(f'user: {contact} has login') + + +bot: Optional[MyBot] = None + + +async def main(): + """Async Main Entry""" + if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ: + print(''' + Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables + You need a TOKEN to run the Java Wechaty. Please goto our README for details + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token + ''') + + global bot + bot = MyBot() + await bot.start() + + +asyncio.run(main()) + diff --git a/examples/advanced/scheduler-bot.py b/examples/advanced/scheduler-bot.py index b20f7d8..3b3669f 100644 --- a/examples/advanced/scheduler-bot.py +++ b/examples/advanced/scheduler-bot.py @@ -1,19 +1,27 @@ -"""doc""" -# pylint: disable=R0801 +""" +Python Wechaty - https://github.com/wechaty/python-wechaty +Authors: Huan LI (李卓桓) + Jingjing WU (吴京京) +2020 @ Copyright Wechaty Contributors +Licensed under the Apache License, Version 2.0 (the 'License'); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at +http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an 'AS IS' BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +""" +import os import asyncio -import logging from typing import Optional, Union -from wechaty_puppet import ScanStatus # type: ignore - from wechaty import Wechaty, Contact, Room from wechaty.user import Message from apscheduler.schedulers.asyncio import AsyncIOScheduler -logging.basicConfig(level=logging.INFO) -log = logging.getLogger(__name__) - class MyBot(Wechaty): """ @@ -25,7 +33,7 @@ def __init__(self): self.busy = False self.auto_reply_comment = "Automatic Reply: I cannot read your message because I'm busy now, will talk to you when I get back." - async def message(msg: Message): + async def message(self, msg: Message): """back on message""" from_contact = msg.talker() text = msg.text() @@ -39,27 +47,30 @@ async def message(msg: Message): async def on_login(self, contact: Contact): print(f'user: {contact} has login') - async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None, - data: Optional[str] = None): - 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 tick(bot: Wechaty): + """ + find a specific room, and say something to it. + """ room = bot.Room.load('room-id') await room.ready() from datetime import datetime - await room.say(f'say ding automatically, {datetime.now()}') - await room.say('ding') + await room.say(f'it"s a new day, let"s welcome, now it"s {datetime.now()}') + await room.say('hello world !') async def main(): - """doc""" - # pylint: disable=W0603 + """Async Main Entry""" + if 'WECHATY_PUPPET_HOSTIE_TOKEN' not in os.environ: + print(''' + Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variables + You need a TOKEN to run the Java Wechaty. Please goto our README for details + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_hostie_token + ''') + global bot bot = MyBot()