lintonxue00
commited on
Commit
•
72eaefa
1
Parent(s):
2d989e4
Upload ext_emoticon.py
Browse files- 不知道/回收站/ext_emoticon.py +66 -0
不知道/回收站/ext_emoticon.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .Extension import Extension
|
2 |
+
import requests, random
|
3 |
+
|
4 |
+
# 拓展的配置信息,用于ai理解拓展的功能 *必填*
|
5 |
+
ext_config:dict = {
|
6 |
+
"name": "emoticon",
|
7 |
+
"arguments": {
|
8 |
+
"keyword": "str", # 关键字
|
9 |
+
},
|
10 |
+
"description": "Send a emoticon related to the keyword(in chinese). usage in response: /#emoticon&开心#/",
|
11 |
+
# 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
|
12 |
+
"refer_word": [],
|
13 |
+
# 每次消息回复中最大调用次数,不填则默认为99
|
14 |
+
"max_call_times_per_msg": 3,
|
15 |
+
# 作者信息
|
16 |
+
"author": "KroMiose",
|
17 |
+
# 版本
|
18 |
+
"version": "0.0.1",
|
19 |
+
# 拓展简介
|
20 |
+
"intro": "发送表情包",
|
21 |
+
}
|
22 |
+
|
23 |
+
class CustomExtension(Extension):
|
24 |
+
async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
|
25 |
+
""" 当拓展被调用时执行的函数 *由拓展自行实现*
|
26 |
+
|
27 |
+
参数:
|
28 |
+
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值}
|
29 |
+
"""
|
30 |
+
custom_config:dict = self.get_custom_config() # 获取yaml中的配置信息
|
31 |
+
|
32 |
+
token = custom_config.get('token', None)
|
33 |
+
if token is None:
|
34 |
+
return {
|
35 |
+
'text': "[ext_emoticon] 请在配置文件中填写alapi访问token"
|
36 |
+
}
|
37 |
+
|
38 |
+
keyword = arg_dict.get('keyword', '')
|
39 |
+
req_args = {
|
40 |
+
'token': custom_config['token'],
|
41 |
+
'keyword': str(keyword),
|
42 |
+
'page': 1,
|
43 |
+
'type': 7,
|
44 |
+
}
|
45 |
+
|
46 |
+
url = f"https://v2.alapi.cn/api/doutu"
|
47 |
+
|
48 |
+
try:
|
49 |
+
res = requests.get(url, params=req_args, timeout=10).json()
|
50 |
+
except Exception as e:
|
51 |
+
return {
|
52 |
+
'text': f"[ext_emoticon] 访问api时发生错误: {e}"
|
53 |
+
}
|
54 |
+
|
55 |
+
if res.get('data') and len(res['data']) > 0:
|
56 |
+
# 从返回的data中随机选择一个返回
|
57 |
+
return {
|
58 |
+
'image': random.choice(res['data'])
|
59 |
+
}
|
60 |
+
else:
|
61 |
+
return {
|
62 |
+
'text': f"[ext_emoticon] 未找到与'{keyword}'相关的表情" if self.get_custom_config().get('debug', False) else None
|
63 |
+
}
|
64 |
+
|
65 |
+
def __init__(self, custom_config: dict):
|
66 |
+
super().__init__(ext_config.copy(), custom_config)
|