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
|
# -*- coding:utf-8 -*-
import time
import httpx
import string
import nonebot
import hashlib
from AyaBot.plugins import *
from random import randint
from urllib.parse import urlencode
from nonebot.helpers import render_expression
from nonebot import on_command, CommandSession, on_natural_language, NLPSession, IntentCommand
bot = nonebot.get_bot()
ERROR_REPLY = (
'吾辈不是很能理解主人的意思...',
'(。´・ω・)ん?',
'ん?',
'あつ...脑子坏了..理解不能',
'吾辈想听主人再说一次...'
)
class Chat(object):
URL = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat'
app_id = bot.config.TX_APP_ID
app_key = bot.config.TX_APPKEY
print(app_id, app_key)
nonce_str_example = 'fa577ce340859f9fe'
ct = lambda: time.time()
@classmethod
def get_nonce_str(self):
nonce_str = ''
len_str = string.digits + string.ascii_letters
for i in range(len(self.nonce_str_example)):
nonce_str += len_str[randint(0, len(len_str) - 1)]
return nonce_str
@classmethod
def sign(self, req_data):
new_list = sorted(req_data.items())
encode_list = urlencode(new_list)
req_data = encode_list + "&" + "app_key" + "=" + self.app_key
md5 = hashlib.md5()
md5.update(req_data.encode('utf-8'))
data = md5.hexdigest()
return data.upper()
@classmethod
async def request(self, text):
req_data = {
'app_id': self.app_id,
'time_stamp': int(self.ct()),
'nonce_str': self.get_nonce_str(),
'session': 10000,
'question': text,
}
print(req_data)
req_data['sign'] = self.sign(req_data)
req_data = sorted(req_data.items())
requests = httpx.AsyncClient()
result = await requests.get(self.URL, params=req_data)
await requests.aclose()
result = result.json()
print(result)
if result['ret'] == 0:
return result['data']['answer']
return render_expression(ERROR_REPLY)
@on_command('chat')
async def chat(session: CommandSession):
msg = session.state.get('msg')
reply = await Chat.request(msg)
await session.finish(reply)
return
@on_natural_language
async def _(session: NLPSession):
return IntentCommand(60.0, ('chat'), {'msg': session.msg_text})
|