|
| 1 | +""" |
| 2 | +Python Wechaty - https://github.com/wechaty/python-wechaty |
| 3 | +Authors: Jingjing WU (吴京京) <https://github.com/wj-Mcat> |
| 4 | +
|
| 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 | +from __future__ import annotations |
| 17 | +import os |
| 18 | +import asyncio |
| 19 | +from typing import List, Optional |
| 20 | + |
| 21 | +from wechaty import Wechaty, Room, RoomQueryFilter |
| 22 | +from wechaty.user.contact import Contact |
| 23 | +from wechaty_puppet import get_logger |
| 24 | + |
| 25 | +log = get_logger('RoomMemberBot') |
| 26 | + |
| 27 | + |
| 28 | +class MyBot(Wechaty): |
| 29 | + """oop wechaty bot, all of your entrypoint should be done here. |
| 30 | + """ |
| 31 | + async def on_ready(self, payload): |
| 32 | + """all of initialization jobs shoule be done here. |
| 33 | + """ |
| 34 | + log.info('ready event<%s>', payload) |
| 35 | + # search contact and add them to the specific room |
| 36 | + room: Optional[Room] = await self.Room.find(query=RoomQueryFilter(topic='room-topic-name')) |
| 37 | + if not room: |
| 38 | + return |
| 39 | + contacts: List[Contact] = await self.Contact.find_all() |
| 40 | + |
| 41 | + for contact in contacts: |
| 42 | + await contact.ready() |
| 43 | + if contact.name == 'your-friend-name': |
| 44 | + await room.add(contact) |
| 45 | + |
| 46 | + |
| 47 | +async def main(): |
| 48 | + """Async Main Entry""" |
| 49 | + if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ: |
| 50 | + print(''' |
| 51 | + Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables |
| 52 | + You need a TOKEN to run the Java Wechaty. Please goto our README for details |
| 53 | + https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token |
| 54 | + ''') |
| 55 | + |
| 56 | + bot = MyBot() |
| 57 | + await bot.start() |
| 58 | + |
| 59 | + |
| 60 | +asyncio.run(main()) |
0 commit comments