-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathding-dong-bot.py
executable file
·99 lines (83 loc) · 2.94 KB
/
ding-dong-bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
Python Wechaty - https://github.com/wechaty/python-wechaty
Authors: Huan LI (李卓桓) <https://github.com/huan>
Jingjing WU (吴京京) <https://github.com/wj-Mcat>
2020 @ Copyright Wechaty Contributors <https://github.com/wechaty>
Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import asyncio
import os
from typing import Optional
from wechaty import (
Contact,
FileBox,
Message,
Wechaty,
ScanStatus,
)
async def on_message(msg: Message):
"""
Message Handler for the Bot
"""
if msg.text() == 'ding':
await msg.say('dong')
file_box = FileBox.from_url(
'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/'
'u=1116676390,2305043183&fm=26&gp=0.jpg',
name='ding-dong.jpg'
)
await msg.say(file_box)
async def on_scan(
qr_code: str,
status: ScanStatus,
data: Optional[str] = None
):
"""
Scan Handler for the Bot
"""
if status == ScanStatus.Waiting:
print("scan status: ", status.name, "qr_code: ", "https://wechaty.js.org/qrcode/" + qr_code)
async def on_login(user: Contact):
"""
Login Handler for the Bot
"""
print(user)
# TODO: To be written
async def main():
"""
Async Main Entry
"""
#
# Make sure we have set WECHATY_PUPPET_SERVICE_TOKEN in the environment variables.
# Learn more about services (and TOKEN) from https://wechaty.js.org/docs/puppet-services/
#
# It is highly recommanded to use token like [paimon] and [wxwork].
# Those types of puppet_service are supported natively.
# https://wechaty.js.org/docs/puppet-services/paimon
# https://wechaty.js.org/docs/puppet-services/wxwork
#
# Replace your token here and umcommt that line, you can just run this python file successfully!
# os.environ['token'] = 'puppet_paimon_your_token'
# os.environ['token'] = 'puppet_wxwork_your_token'
#
if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ:
print('''
Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables
You need a TOKEN to run the Python Wechaty. Please goto our README for details
https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token
''')
bot = Wechaty()
bot.on('scan', on_scan)
bot.on('login', on_login)
bot.on('message', on_message)
await bot.start()
print('[Python Wechaty] Ding Dong Bot started.')
asyncio.run(main())