Skip to content

Instantly share code, notes, and snippets.

@wj-Mcat
Last active January 17, 2021 00:46
Show Gist options
  • Save wj-Mcat/8e6e168dece2cbfcb374416d890ef6a2 to your computer and use it in GitHub Desktop.
Save wj-Mcat/8e6e168dece2cbfcb374416d890ef6a2 to your computer and use it in GitHub Desktop.
start padlocal-service with python-wechaty

概述

介绍如何使用使用docker将Padlocal服务通过Hostie的方式暴露给python-wehcaty,以下为核心的启动代码。

Docker Serivce

docker run -ti -e WECHATY_LOG="verbose" \
  -e WECHATY_PUPPET="wechaty-puppet-padlocal" \
  -e WECHATY_PUPPET_PADLOCAL_TOKEN="your-padlocal-token" \
  -e WECHATY_HOSTIE_PORT="9011" \
  -p "$WECHATY_HOSTIE_PORT:9011" \
  -e WECHATY_TOKEN="mytoken-wujingjing-1" \
  wechaty/wechaty:next

Bot with Python-Wechaty

"""doc"""
# pylint: disable=R0801
import asyncio
import logging
import os
from typing import Optional, Union

from chatie_grpc.wechaty import MessageType

from wechaty_puppet import FileBox, ScanStatus  # type: ignore

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')


bot: Optional[MyBot] = None


async def main():
    """doc"""
    # pylint: disable=W0603
    global bot
    import os
    print("================================================")
    os.environ['WECHATY_LOG'] = 'DEBUG'
    os.environ['WECHATY_PUPPET_HOSTIE_TOKEN'] = 'mytoken-wujingjing-1'
    os.environ['WECHATY_PUPPET_HOSTIE_ENDPOINT'] = '127.0.0.1:9011'
    print("================================================")
    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