lintonxue00
commited on
Commit
•
48fba35
1
Parent(s):
96eeb81
Upload ext_one_photo.py
Browse files- 不知道/回收站/1/ext_one_photo.py +70 -0
不知道/回收站/1/ext_one_photo.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .Extension import Extension
|
2 |
+
import requests, random
|
3 |
+
|
4 |
+
# 拓展的配置信息,用于ai理解拓展的功能 *必填*
|
5 |
+
ext_config = {
|
6 |
+
"name": "one_photo",
|
7 |
+
"arguments": {
|
8 |
+
"keyword": "str", # 关键字
|
9 |
+
},
|
10 |
+
"description": "Send an ACG image related to the keyword(in Chinese). Usage in response: /#one_photo&2022-12-21#/",
|
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': error_msg if self.get_custom_config().get('debug', False) else None
|
36 |
+
}
|
37 |
+
|
38 |
+
keyword = arg_dict.get('keyword', '')
|
39 |
+
req_args = {
|
40 |
+
'token': custom_config['token'],
|
41 |
+
'date': '2021-04-21',
|
42 |
+
}
|
43 |
+
url = "https://v2.alapi.cn/api/one/photo"
|
44 |
+
headers = {'Content-Type': 'application/json'}
|
45 |
+
|
46 |
+
try:
|
47 |
+
res = requests.get(url, params=req_args, headers=headers, timeout=10).json()
|
48 |
+
except Exception as e:
|
49 |
+
return {
|
50 |
+
'text': f"[ext_acg_image] 访问api时发生错误: {e}"
|
51 |
+
}
|
52 |
+
|
53 |
+
if 'data' in res:
|
54 |
+
data = res['data']
|
55 |
+
return {
|
56 |
+
'title': data.get('title', ''),
|
57 |
+
'subtitle': data.get('subtitle', ''),
|
58 |
+
'content': data.get('content', ''),
|
59 |
+
'cover': data.get('cover', ''),
|
60 |
+
'make_time': data.get('make_time', '')
|
61 |
+
}
|
62 |
+
else:
|
63 |
+
error_msg = f"[ext_acg_image] 返回的数据格式不正确: {res}"
|
64 |
+
return {
|
65 |
+
'text': error_msg if self.get_custom_config().get('debug', False) else None
|
66 |
+
}
|
67 |
+
|
68 |
+
def __init__(self, custom_config: dict):
|
69 |
+
super().__init__(ext_config.copy(), custom_config)
|
70 |
+
|