mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
first_commit .gitignore
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
from .data_source import get_qqbot_chat_result, hello, no_result
|
||||
from services.log import logger
|
||||
from nonebot import on_message
|
||||
from nonebot.rule import to_me
|
||||
from nonebot.typing import T_State
|
||||
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent, PrivateMessageEvent
|
||||
from util.utils import get_message_text, get_message_imgs
|
||||
from models.friend_user import FriendUser
|
||||
from models.group_member_info import GroupInfoUser
|
||||
|
||||
__plugin_name__ = 'AI'
|
||||
|
||||
|
||||
ai = on_message(rule=to_me(), priority=8)
|
||||
|
||||
|
||||
@ai.handle()
|
||||
async def _(bot: Bot, event: PrivateMessageEvent, state: T_State):
|
||||
msg = get_message_text(event.json())
|
||||
imgs = get_message_imgs(event.json())
|
||||
if str(event.get_message()).find('CQ:xml') != -1:
|
||||
return
|
||||
# 打招呼
|
||||
if not msg and not imgs:
|
||||
await ai.finish(hello())
|
||||
img = imgs[0] if imgs else ''
|
||||
nickname = await FriendUser.get_friend_nickname(event.user_id)
|
||||
if not nickname:
|
||||
nickname = await FriendUser.get_user_name(event.user_id)
|
||||
if not nickname:
|
||||
nickname = "你"
|
||||
result = await get_qqbot_chat_result(msg, img, event.user_id, nickname)
|
||||
logger.info(f"USER {event.user_id} 问题:{msg}\n回答:{result}")
|
||||
if result:
|
||||
await ai.finish(result)
|
||||
else:
|
||||
await ai.finish(no_result())
|
||||
|
||||
|
||||
@ai.handle()
|
||||
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
||||
# if await GroupRemind.get_status(event.group_id, 'ai'):
|
||||
msg = get_message_text(event.json())
|
||||
imgs = get_message_imgs(event.json())
|
||||
# 打招呼
|
||||
if not msg and not imgs:
|
||||
await ai.finish(hello())
|
||||
img = imgs[0] if imgs else ''
|
||||
nickname = await GroupInfoUser.get_group_member_nickname(event.user_id, event.group_id)
|
||||
if not nickname:
|
||||
try:
|
||||
nickname = (await GroupInfoUser.select_member_info(event.user_id, event.group_id)).user_name
|
||||
except AttributeError:
|
||||
nickname = "你"
|
||||
result = await get_qqbot_chat_result(msg, img, event.user_id, nickname)
|
||||
logger.info(f"问题:{msg}\n回答:{result}")
|
||||
if result:
|
||||
await ai.finish(result)
|
||||
else:
|
||||
await ai.finish(no_result())
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,142 @@
|
||||
from configs.config import TL_KEY
|
||||
import aiohttp
|
||||
import random
|
||||
import os
|
||||
from configs.path_config import IMAGE_PATH, DATA_PATH
|
||||
from util.init_result import image
|
||||
from util.utils import get_bot
|
||||
|
||||
try:
|
||||
import ujson as json
|
||||
except ModuleNotFoundError:
|
||||
import json
|
||||
|
||||
url = "http://openapi.tuling123.com/openapi/api/v2"
|
||||
|
||||
index = 0
|
||||
|
||||
anime_data = json.load(open(DATA_PATH + 'anime.json', 'r', encoding='utf8'))
|
||||
|
||||
|
||||
# 图灵AI
|
||||
async def get_qqbot_chat_result(text: str, img_url: str, user_id: int, user_name: str) -> str:
|
||||
global index
|
||||
if index == 5:
|
||||
index = 0
|
||||
if len(text) < 6 and random.random() < 0.6:
|
||||
keys = anime_data.keys()
|
||||
for key in keys:
|
||||
if text.find(key) != -1:
|
||||
return random.choice(anime_data[key]).replace('你', user_name)
|
||||
if text:
|
||||
req = {
|
||||
"perception":
|
||||
{
|
||||
"inputText":
|
||||
{
|
||||
"text": text
|
||||
},
|
||||
"selfInfo":
|
||||
{
|
||||
"location":
|
||||
{
|
||||
"city": "陨石坑",
|
||||
"province": "火星",
|
||||
"street": "第5坑位"
|
||||
}
|
||||
}
|
||||
},
|
||||
"userInfo":
|
||||
{
|
||||
"apiKey": TL_KEY[index],
|
||||
"userId": str(user_id)
|
||||
}
|
||||
}
|
||||
elif img_url:
|
||||
req = {
|
||||
"reqType": 1,
|
||||
"perception":
|
||||
{
|
||||
"inputImage": {
|
||||
"url": img_url
|
||||
},
|
||||
"selfInfo":
|
||||
{
|
||||
"location":
|
||||
{
|
||||
"city": "陨石坑",
|
||||
"province": "火星",
|
||||
"street": "第5坑位"
|
||||
}
|
||||
}
|
||||
},
|
||||
"userInfo":
|
||||
{
|
||||
"apiKey": TL_KEY[index],
|
||||
"userId": str(user_id)
|
||||
}
|
||||
}
|
||||
async with aiohttp.ClientSession() as sess:
|
||||
async with sess.post(url, json=req) as response:
|
||||
if response.status != 200:
|
||||
return ''
|
||||
resp_payload = json.loads(await response.text())
|
||||
if resp_payload['intent']:
|
||||
if resp_payload['intent']['code'] == 4003:
|
||||
index += 1
|
||||
# 该AI很屑!!!!!!!!!!!!
|
||||
async with sess.get(f'http://api.qingyunke.com/api.php?key=free&appid=0&msg={text}') as res:
|
||||
data = json.loads(await res.text())
|
||||
if data['result'] == 0:
|
||||
content = data['content']
|
||||
if content.find('菲菲') != -1:
|
||||
content = content.replace('菲菲', list(get_bot().config.nickname)[0])
|
||||
if content.find('公众号') != -1:
|
||||
content = ''
|
||||
if content.find('{br}') != -1:
|
||||
content = content.replace('{br}', '\n')
|
||||
if content.find('提示') != -1:
|
||||
content = content[:content.find('提示')]
|
||||
return content
|
||||
if resp_payload['results']:
|
||||
for result in resp_payload['results']:
|
||||
if result['resultType'] == 'text':
|
||||
text = result['values']['text']
|
||||
if user_name:
|
||||
text = text.replace('小朋友', user_name)
|
||||
if len(user_name) < 5:
|
||||
if random.random() < 0.5:
|
||||
user_name = "~".join(user_name) + '~'
|
||||
if random.random() < 0.5:
|
||||
if user_name.find('大人') == -1:
|
||||
user_name += '大~人~'
|
||||
text = text.replace('小主人', user_name)
|
||||
return text
|
||||
|
||||
|
||||
def hello() -> str:
|
||||
result = random.choice((
|
||||
"哦豁?!",
|
||||
"你好!Ov<",
|
||||
f"库库库,呼唤{list(get_bot().config.nickname)[0]}做什么呢",
|
||||
"我在呢!",
|
||||
"呼呼,叫俺干嘛"
|
||||
))
|
||||
img = random.choice(os.listdir(IMAGE_PATH + "zai/"))
|
||||
if img[-4:] == ".gif":
|
||||
result += image(img, "zai")
|
||||
else:
|
||||
result += image(img, "zai")
|
||||
return result
|
||||
|
||||
|
||||
def no_result() -> str:
|
||||
return random.choice([
|
||||
'你在说啥子?',
|
||||
f'纯洁的{list(get_bot().config.nickname)[0]}没听懂',
|
||||
'下次再告诉你(下次一定)',
|
||||
'你觉得我听懂了吗?嗯?',
|
||||
'我!不!知!道!'
|
||||
]) + image(
|
||||
random.choice(os.listdir(IMAGE_PATH + "noresult/")
|
||||
), "noresult")
|
||||
Reference in New Issue
Block a user