From fa41d1bf035a2ffaf51e7255d7148ba779ae2cb4 Mon Sep 17 00:00:00 2001
From: Kyomotoi <1172294279@qq.com>
Date: Sun, 21 Jun 2020 14:50:04 +0800
Subject: First official version

---
 AyaBot/plugins/__pycache__/chat.cpython-37.pyc     | Bin 0 -> 2726 bytes
 AyaBot/plugins/__pycache__/covid19.cpython-37.pyc  | Bin 2252 -> 2291 bytes
 AyaBot/plugins/__pycache__/other.cpython-37.pyc    | Bin 1953 -> 2498 bytes
 AyaBot/plugins/__pycache__/setu.cpython-37.pyc     | Bin 3586 -> 3596 bytes
 .../plugins/__pycache__/translate.cpython-37.pyc   | Bin 1756 -> 1795 bytes
 AyaBot/plugins/__pycache__/weather.cpython-37.pyc  | Bin 6213 -> 6252 bytes
 AyaBot/plugins/chat.py                             |  80 +++++++++++++++++++++
 AyaBot/plugins/setu.py                             |   8 ++-
 8 files changed, 85 insertions(+), 3 deletions(-)
 create mode 100644 AyaBot/plugins/__pycache__/chat.cpython-37.pyc
 create mode 100644 AyaBot/plugins/chat.py

(limited to 'AyaBot/plugins')

diff --git a/AyaBot/plugins/__pycache__/chat.cpython-37.pyc b/AyaBot/plugins/__pycache__/chat.cpython-37.pyc
new file mode 100644
index 0000000..ad1818d
Binary files /dev/null and b/AyaBot/plugins/__pycache__/chat.cpython-37.pyc differ
diff --git a/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc b/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc
index 7524c7d..f1a5940 100644
Binary files a/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc and b/AyaBot/plugins/__pycache__/covid19.cpython-37.pyc differ
diff --git a/AyaBot/plugins/__pycache__/other.cpython-37.pyc b/AyaBot/plugins/__pycache__/other.cpython-37.pyc
index 1b4d258..9934055 100644
Binary files a/AyaBot/plugins/__pycache__/other.cpython-37.pyc and b/AyaBot/plugins/__pycache__/other.cpython-37.pyc differ
diff --git a/AyaBot/plugins/__pycache__/setu.cpython-37.pyc b/AyaBot/plugins/__pycache__/setu.cpython-37.pyc
index 66e8215..1bca92b 100644
Binary files a/AyaBot/plugins/__pycache__/setu.cpython-37.pyc and b/AyaBot/plugins/__pycache__/setu.cpython-37.pyc differ
diff --git a/AyaBot/plugins/__pycache__/translate.cpython-37.pyc b/AyaBot/plugins/__pycache__/translate.cpython-37.pyc
index 42462a8..17bf117 100644
Binary files a/AyaBot/plugins/__pycache__/translate.cpython-37.pyc and b/AyaBot/plugins/__pycache__/translate.cpython-37.pyc differ
diff --git a/AyaBot/plugins/__pycache__/weather.cpython-37.pyc b/AyaBot/plugins/__pycache__/weather.cpython-37.pyc
index 02ebb2f..b931e84 100644
Binary files a/AyaBot/plugins/__pycache__/weather.cpython-37.pyc and b/AyaBot/plugins/__pycache__/weather.cpython-37.pyc differ
diff --git a/AyaBot/plugins/chat.py b/AyaBot/plugins/chat.py
new file mode 100644
index 0000000..38a4759
--- /dev/null
+++ b/AyaBot/plugins/chat.py
@@ -0,0 +1,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 context_id, 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})
\ No newline at end of file
diff --git a/AyaBot/plugins/setu.py b/AyaBot/plugins/setu.py
index f0d5365..3f24129 100644
--- a/AyaBot/plugins/setu.py
+++ b/AyaBot/plugins/setu.py
@@ -7,6 +7,7 @@ import requests
 from aiohttp import ClientSession
 from nonebot import on_command, CommandSession
 
+bot = nonebot.get_bot()
 
 async def post_bytes(url, headers=None,data=None):
     async with ClientSession() as asyncsession:
@@ -62,8 +63,9 @@ async def _(session: CommandSession):
             await session.send(f'[CQ:image,file={pt}]')
         if r == '2':
             url = 'https://api.lolicon.app/setu/'
+            apikey = bot.config.LOLICONAPI
             values = {
-                "apikey": "574300555ee56eb2be5e03",
+                "apikey": apikey,
                 "r18": "0",
                 "num": "1"
              }
@@ -78,7 +80,7 @@ async def _(session: CommandSession):
         # if r == '2':
         #     URL = 'https://danbooru.donmai.us/post.json'
         #     values = {
-        #         "api_key": "UoTNRNeta73tqqdGVvsU9mmH"
+        #         "api_key": ""
         #     }
         #     response = requests.get(URL, params=values)
         #     html = response.text
@@ -89,7 +91,7 @@ async def _(session: CommandSession):
         # if r == '3':
         #     URL = 'https://yande.re/post.json'
         #     values = {
-        #         "api_key": "zgBbal8sZVuRYp3UNX5Frg"
+        #         "api_key": ""
         #     }
         #     response = requests.get(URL, params=values)
         #     html = response.text
-- 
cgit v1.2.3