lintonxue00 commited on
Commit
cb7e648
1 Parent(s): 75eb7e2

Upload ext_emoticon.py

Browse files
不知道/回收站/1/ext_emoticon.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # 可用会话类型 (server即MC服务器 | chat即QQ聊天)
22
+ "available": ['chat'],
23
+ }
24
+
25
+ class CustomExtension(Extension):
26
+ async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
27
+ """ 当扩展被调用时执行的函数 *由扩展自行实现*
28
+
29
+ 参数:
30
+ arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值}
31
+ """
32
+ custom_config:dict = self.get_custom_config() # 获取yaml中的配置信息
33
+
34
+ token = custom_config.get('token', None)
35
+ if token is None:
36
+ return {
37
+ 'text': "[ext_emoticon] 请在配置文件中填写alapi访问token"
38
+ }
39
+
40
+ keyword = arg_dict.get('keyword', '')
41
+ req_args = {
42
+ 'token': custom_config['token'],
43
+ 'keyword': str(keyword),
44
+ 'page': 1,
45
+ 'type': 7,
46
+ }
47
+
48
+ url = f"https://v2.alapi.cn/api/doutu"
49
+
50
+ try:
51
+ res = requests.get(url, params=req_args, timeout=10).json()
52
+ except Exception as e:
53
+ return {
54
+ 'text': f"[ext_emoticon] 访问api时发生错误: {e}"
55
+ }
56
+
57
+ if res.get('data') and len(res['data']) > 0:
58
+ # 从返回的data中随机选择一个返回
59
+ return {
60
+ 'image': random.choice(res['data'])
61
+ }
62
+ else:
63
+ return {
64
+ 'text': f"[ext_emoticon] 未找到与'{keyword}'相关的表情" if self.get_custom_config().get('debug', False) else None
65
+ }
66
+
67
+ def __init__(self, custom_config: dict):
68
+ super().__init__(ext_config.copy(), custom_config)