Skip to content

Commit cf6f159

Browse files
committed
add room-member-bot
1 parent 403b63b commit cf6f159

File tree

2 files changed

+128
-23
lines changed

2 files changed

+128
-23
lines changed

Makefile

+68-23
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,79 @@
1+
SOURCE_GLOB=$(wildcard examples/*.py examples/**/*.py)
2+
3+
#
4+
# Huan(202003)
5+
# F811: https://github.com/PyCQA/pyflakes/issues/320#issuecomment-469337000
6+
#
7+
IGNORE_PEP=E203,E221,E241,E272,E501,F811
8+
19
.PHONY: all
2-
all: install example
10+
all : clean lint
11+
12+
.PHONY: clean
13+
clean:
14+
rm -fr dist/* .pytype
15+
16+
.PHONY: lint
17+
lint: pylint pycodestyle flake8 mypy
18+
19+
20+
# disable: TODO list temporay
21+
.PHONY: pylint
22+
pylint:
23+
pylint \
24+
--load-plugins pylint_quotes \
25+
--disable=W0511,R0801,cyclic-import \
26+
$(SOURCE_GLOB)
27+
28+
.PHONY: pycodestyle
29+
pycodestyle:
30+
pycodestyle \
31+
--statistics \
32+
--count \
33+
--ignore="${IGNORE_PEP}" \
34+
$(SOURCE_GLOB)
35+
36+
.PHONY: flake8
37+
flake8:
38+
flake8 \
39+
--ignore="${IGNORE_PEP}" \
40+
$(SOURCE_GLOB)
41+
42+
.PHONY: mypy
43+
mypy:
44+
MYPYPATH=stubs/ mypy \
45+
$(SOURCE_GLOB)
46+
47+
.PHONY: pytype
48+
pytype:
49+
pytype \
50+
-V 3.8 \
51+
--disable=import-error \
52+
examples/
353

454
.PHONY: install
555
install:
656
pip3 install -r requirements.txt
757
pip3 install -r requirements-dev.txt
858

9-
.PHONY: bot
10-
bot:
11-
python3 examples/ding-dong-bot.py
59+
.PHONY: pytest
60+
pytest:
61+
pytest src/ tests/
1262

13-
.PHONY: lint
14-
lint:
15-
# stop the build if there are Python syntax errors or undefined names
16-
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
17-
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
18-
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
63+
.PHONY: test-unit
64+
test-unit: pytest
1965

2066
.PHONY: test
21-
test: pytest
67+
test: lint pytest
2268

23-
.PHONY: pytest
24-
pytest:
25-
python3 -m pytest
26-
27-
.PHONY: version
28-
version:
29-
@newVersion=$$(awk -F. '{print $$1"."$$2"."$$3+1}' < VERSION) \
30-
&& echo $${newVersion} > VERSION \
31-
&& git add VERSION \
32-
&& git commit -m "$${newVersion}" > /dev/null \
33-
&& git tag "v$${newVersion}" \
34-
&& echo "Bumped version to $${newVersion}"
69+
.PHONY: check-python-version
70+
check-python-version:
71+
./scripts/check_python_version.py
72+
73+
.PHONY: dist
74+
dist:
75+
python3 setup.py sdist bdist_wheel
76+
77+
.PHONY: bot
78+
bot:
79+
python3 examples/ding-dong-bot.py

examples/advanced/room-member-bot.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)