generated from GT-ZhangAcer/AI-Studio-Template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_bot.py
201 lines (168 loc) · 6.8 KB
/
run_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 29 10:27:33 2021
@author: D&E Tech
"""
"""
set WECHATY_PUPPET_SERVICE_TOKEN="puppet_padlocal_2d7902aeb6ec4160a22bf146c8a532f7"
set WECHATY_PUPPET_SERVICE_TOKEN=kevinsun17
"""
import time
import asyncio
import logging
from typing import Optional, Union
import os
from wechaty_puppet import FileBox, ScanStatus # type: ignore
# from wechaty import Wechaty, Contact
from wechaty.user import Message, Room
import botProcess
from wechaty import (
Contact,
FileBox,
Message,
Wechaty,
ScanStatus,
)
from talkProcess import talkManger
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
def create_gif():
"""
refer to : https://note.nkmk.me/en/python-pillow-gif/
you can create gif file with your own code.
"""
from PIL import Image, ImageDraw
images = []
width = 200
center = width // 2
color_1 = (0, 0, 0)
color_2 = (255, 255, 255)
max_radius = int(center * 1.5)
step = 8
for i in range(0, max_radius, step):
im = Image.new('RGB', (width, width), color_1)
draw = ImageDraw.Draw(im)
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_2)
images.append(im)
for i in range(0, max_radius, step):
im = Image.new('RGB', (width, width), color_2)
draw = ImageDraw.Draw(im)
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_1)
images.append(im)
images[0].save('./bot.gif',
save_all=True, append_images=images[1:], optimize=False, duration=40, loop=0)
class MyBot(Wechaty):
"""
listen wechaty event with inherited functions, which is more friendly for
oop developer
"""
def __init__(self):
super().__init__()
self.splitNum=5
self.bm = botProcess.botManger(3)
path='/root/paddlejob/workspace/output/'
print('path',path,os.path.exists(path))
self.tm = talkManger(path, path)
# if os.path.exists(path):
#
# self.tm = talkManger(path, path)
# else:
# self.tm = talkManger()
async def on_message(self, msg: Message):
"""
listen for message event
"""
from_contact = msg.talker()
text = msg.text()
type = msg.type()
room = msg.room()
#
username = from_contact.name
if username=='KFu':
print('message from myself')
return
# 不处理群消息
# if room is None:
if msg.type() == Message.Type.MESSAGE_TYPE_IMAGE:
print('__image')
image_file_box = await msg.to_file_box()
filename='p'+str(time.time())+'.jpg'
await image_file_box.to_file(file_path=filename,overwrite=True)
inputdata="#pic#"+filename
bot = self.bm.run(username, inputdata)
if bot is not None:
# print('bot',bot)
# print('bot replys',bot.replys[-1])
# print('bot.replys_index',bot.replys_index)
for i in range(bot.replys_index):
bot, rdict = self.tm.run(bot)
print('rdict',rdict)
if len(list(rdict.keys()))==0:continue
if list(rdict.keys())[0] == "str":
print('reply str')
conversation: Union[
Room, Contact] = from_contact if room is None else room
print('ready')
await conversation.ready()
print(list(rdict.values())[0])
await conversation.say(list(rdict.values())[0])
elif list(rdict.keys())[0] == "pic" or 'mov':
print('reply pic/mov')
conversation: Union[
Room, Contact] = from_contact if room is None else room
await conversation.ready()
try:
file_box = FileBox.from_file(list(rdict.values())[0])
except Exception as e:
print('file box error',e)
file_box='嗯嗯'
await conversation.say(file_box)
elif msg.type() == Message.Type.MESSAGE_TYPE_TEXT:
inputdata = "#str#" + msg.text()
print('————text')
bot = self.bm.run(username, inputdata)
if bot is not None:
# print('bot', bot)
# print('bot replys',bot.replys[-1])
# print('bot.replys_index',bot.replys_index)
for i in range(bot.replys_index):
bot, rdict = self.tm.run(bot)
print('rdict',rdict)
if len(list(rdict.keys()))==0:continue
if list(rdict.keys())[0] == "str":
print('reply str')
conversation: Union[
Room, Contact] = from_contact if room is None else room
await conversation.ready()
print('rdict[splitNum:]',list(rdict.values())[0])
await conversation.say(list(rdict.values())[0])
elif list(rdict.keys())[0] == "pic" or 'mov':
print('reply pic/mov')
conversation: Union[
Room, Contact] = from_contact if room is None else room
await conversation.ready()
try:
file_box = FileBox.from_file(list(rdict.values())[0])
except Exception as e:
print('file box error',e)
file_box='嗯嗯'
await conversation.say(file_box)
else:
print('__new for dict')
conversation: Union[
Room, Contact] = from_contact if room is None else room
await conversation.ready()
await conversation.say('暂时不支持这种类型的消息哦')
async def on_login(self, contact: Contact):
print(f'user: {contact} has login')
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
data: Optional[str] = None):
contact = self.Contact.load(self.contact_id)
print(f'user <{contact}> scan status: {status.name} , '
f'qr_code: {qr_code}')
async def main():
"""doc"""
# create_gif()
bot = MyBot()
await bot.start()
asyncio.run(main())