Skip to content
This repository has been archived by the owner on Jun 3, 2023. It is now read-only.

Feat Python版本启动wechaty机器人 #50

Open
koreQAQ opened this issue Mar 6, 2022 · 1 comment
Open

Feat Python版本启动wechaty机器人 #50

koreQAQ opened this issue Mar 6, 2022 · 1 comment

Comments

@koreQAQ
Copy link

koreQAQ commented Mar 6, 2022

Is your feature request related to a problem? Please describe.
Python 启动企业微信机器人

Describe the solution you'd like

# bot.py
from datetime import datetime
from typing import List, Optional

from wechaty import Wechaty, WechatyOptions, Contact, Message, Friendship, Room, RoomInvitation

import asyncio

from wechaty_puppet import PuppetOptions, EventReadyPayload, EventHeartbeatPayload, EventErrorPayload, ScanStatus

class MyBot(Wechaty):

    # System Event
    async def on_ready(self, payload: EventReadyPayload):
        """Any initialization work can be put in here

        Args:
            payload (EventReadyPayload): ready data
        """
        print(f'receive ready event<{payload}>')
        """creating room"""
        # 1. filter friend
        friends: List[Contact] = await self.Contact.find_all()
        print(f"联系人总数 :{len(friends)}")

    async def on_heartbeat(self, payload: EventHeartbeatPayload):
        print(f'receive heartbeat event from server <{payload}>')

    async def on_error(self, payload: EventErrorPayload):
        print(f'receive error event<{payload}> from sever')

    async def on_scan(self, qr_code: str, status: ScanStatus, data: Optional[str]):
        """listen scan event"""
        print('Scan QR Code to login: {}\n'.format(status))
        print('https://wechaty.js.org/qrcode/{}'.format(qr_code))

    async def on_login(self, contact: Contact):
        print(f'User {contact} logged in\n')

    async def on_logout(self, contact: Contact):
        print(f'User <{contact}> logout')

    async def on_message(self, msg: Message):
        #
        print(f'receive message <{msg}>')
       
    async def on_friendship(self, friendship: Friendship):
        # 好友请求
        print(f'receive friendship<{friendship}> event')

    async def on_room_topic(self, room: Room, new_topic: str, old_topic: str, changer: Contact, date: datetime):
        print(f'receive room topic changed event <from<{new_topic}> to <{old_topic}>> from room<{room}> ')

    async def on_room_invite(self, room_invitation: RoomInvitation):
        # 群聊邀请
        print(f'receive room invitation<{room_invitation}> event')

    async def on_room_join(self, room: Room, invitees: List[Contact], inviter: Contact, date: datetime):
        print(f'receive room join event from Room<{room}>')

    async def on_room_leave(self, room: Room, leavers: List[Contact], remover: Contact, date: datetime):
        print(f'receive room leave event from Room<{room}>')


puppet_options = PuppetOptions(token='your token')
asyncio.run(MyBot(WechatyOptions(puppet='wechaty-puppet-service', puppet_options=puppet_options)).start())

启动

python bot.py

Additional context
Java 版本的无法启动 遇到了 Issue#85

@su-chang
Copy link
Collaborator

建议使用 node 版本的wechaty测试下看看哈。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants