generated from GT-ZhangAcer/AI-Studio-Template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_wechaty.py
149 lines (124 loc) · 5.14 KB
/
run_wechaty.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
import time
import asyncio
import logging
from typing import Optional, Union
from wechaty_puppet import PuppetOptions, FileBox # type: ignore
from wechaty import Wechaty, Contact
from wechaty.user import Message, Room
# from .tencentaiplat import TencentAI
import botProcess
from talkProcess import talkManger
import os
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s %(filename)s <%(funcName)s> %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
log = logging.getLogger('DingDongBot')
chat_friend: list = []
splitNum = 5
bm = botProcess.botManger(3)
path = '/root/paddlejob/workspace/output'
print('path', path, os.path.exists(path))
tm = talkManger(path, path)
async def message(msg: Message):
"""back on message"""
from_contact = msg.talker()
username = from_contact.name
text = msg.text()
room = msg.room()
conversation: Union[
Room, Contact] = from_contact if room is None else room
global chat_friend
global splitNum
global bm
global tm
if "吐槽" in text or "图槽" in text or "树洞" in text :
chat_friend.append(conversation)
inputdata = "#str#" + msg.text()
print('————text')
bot = 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 = 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)
# await conversation.ready()
# await conversation.say('闲聊功能开启成功!现在你可以和我聊天啦!')
return
if conversation in chat_friend:
if username=='KFu':
print('KFu')
return
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
elif msg.type() == Message.Type.MESSAGE_TYPE_TEXT:
print('--text')
inputdata = "#str#" + msg.text()
bot = 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 = 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)
# data = TencentAI(text)
# await conversation.ready()
# await conversation.say(data)
return
else:
print('not in friend')
return
bot: Optional[Wechaty] = None
async def main():
"""doc"""
# pylint: disable=W0603
global bot
bot = Wechaty().on('message', message)
await bot.start()
asyncio.run(main())