Created
June 14, 2020 01:40
-
-
Save wj-Mcat/fd0815754ffb925d902a8ab2edca6aa0 to your computer and use it in GitHub Desktop.
python-wechaty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""doc""" | |
# pylint: disable=R0801 | |
import asyncio | |
import logging | |
from typing import Optional, Union | |
from wechaty_puppet import FileBox, ScanStatus # type: ignore | |
import base64 | |
from wechaty import Wechaty, Contact | |
from wechaty.user import Message, Room | |
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_message(self, msg: Message): | |
""" | |
listen for message event | |
""" | |
from_contact = msg.talker() | |
text = msg.text() | |
room = msg.room() | |
if text == '#ding': | |
conversation: Union[ | |
Room, Contact] = from_contact if room is None else room | |
await conversation.ready() | |
await conversation.say('dong') | |
with open('./avatar.jpg', 'rb') as f: | |
content = base64.b64encode(f.read()) | |
file_box = FileBox.from_base64(name='avatar.jpg', base64=content) | |
await conversation.say(file_box) | |
bot: Optional[MyBot] = None | |
async def main(): | |
global bot | |
bot = MyBot() | |
await bot.start() | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment