-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
119 lines (96 loc) · 3.89 KB
/
main.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import asyncio
import support
import mix
from wechaty import (
Contact,
FileBox,
Message,
Wechaty,
ScanStatus,
)
os.environ['WECHATY_PUPPET'] = "wechaty-puppet-service"
os.environ['WECHATY_PUPPET_SERVICE_TOKEN'] = "puppet_padlocal_**" # 这里填Wechaty token
os.environ['CUDA_VISIBLE_DEVICES'] = "0"
mix_flag = 0
async def on_message(msg: Message):
global mix_flag
if msg.text() == 'ding':
await msg.say('这是自动回复: dong dong dong')
if msg.text() == 'hi' or msg.text() == '你好':
await msg.say('这是自动回复: 机器人目前的功能是\n'
'- 收到"融合", 请根据提示完成操作进行人像融合\n'
'- 收到"搜图 xx", 自动搜出一张图片(例如:搜图 垃圾)\n'
'- 收到"查天气 XX",自动查询某地天气(例如:查天气 沈阳)')
if msg.text() == '融合':
mix_flag = 1
if os.path.exists('image/first_image.png'):
os.remove('image/first_image.png')
if os.path.exists('image/second_image.png'):
os.remove('image/second_image.png')
await msg.say('please send the first image')
if mix_flag == 1 and msg.type() == Message.Type.MESSAGE_TYPE_IMAGE:
mix_flag = 2
# 将Message转换为FileBox
file_box_user_image = await msg.to_file_box()
# 获取图片名
# img_name = file_box_user_image.name
# 图片保存的路径
img_path = './image/' + 'first_image.png'
# 将图片保存为本地文件
await file_box_user_image.to_file(file_path=img_path)
mix.gan1()
await msg.say('please send the second image')
if mix_flag == 2 and msg.type() == Message.Type.MESSAGE_TYPE_IMAGE:
mix_flag = 3
# 将Message转换为FileBox
file_box_user_image = await msg.to_file_box()
# 获取图片名
# img_name = file_box_user_image.name
# 图片保存的路径
img_path = './image/' + 'second_image.png'
# 将图片保存为本地文件
await file_box_user_image.to_file(file_path=img_path)
mix.gan2()
await msg.say("Nice!Let's wait a minite for a mixed image")
if mix_flag == 3 and os.path.exists('output'):
mix_flag = 4
mix.mix()
await msg.say('it will come soon!')
if mix_flag == 4:
mix_flag = 0
file_box = FileBox.from_file('mixoutput/dst.mixing.png')
await msg.say(file_box)
if msg.text().split(' ', 1)[0] == '查天气':
key = str(msg.text().split(' ')[1])
weather = support.getweather(key)
await msg.say(weather)
if msg.text().split(' ', 1)[0] == '搜图':
key = str(msg.text().split(' ')[1])
url1 = support.download_image(key)
file_box = FileBox.from_url(url=url1, name='xx.jpg')
await msg.say(file_box)
async def on_scan(
qrcode: str,
status: ScanStatus,
_data,
):
print('Status: ' + str(status))
print('View QR Code Online: https://wechaty.js.org/qrcode/' + qrcode)
async def on_login(user: Contact):
print(user)
async def main():
# 确保我们在环境变量中设置了WECHATY_PUPPET_SERVICE_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())