lintonxue00 commited on
Commit
7528b7b
1 Parent(s): b3b551a

Upload ext_acg_image.py

Browse files
不知道/回收站/4/ext_acg_image.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .Extension import Extension
2
+ import requests, random
3
+
4
+ # 拓展的配置信息,用于ai理解拓展的功能 *必填*
5
+ class CustomExtension(Extension):
6
+ ext_config = {
7
+ "name": "acg_image",
8
+ "arguments": {
9
+ "keyword": "str", # 关键字
10
+ },
11
+ "description": "Send an ACG image related to the keyword(in Chinese). Usage in response: /#acg_image&动漫#/",
12
+ # 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
13
+ "refer_word": [],
14
+ # 每次消息回复中最大调用次数,不填则默认为99
15
+ "max_call_times_per_msg": 3,
16
+ # 作者信息
17
+ "author": "KroMiose",
18
+ # 版本
19
+ "version": "0.0.1",
20
+ # 拓展简介
21
+ "intro": "发送ACG图片",
22
+ }
23
+
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
+ 'format': 'json',
42
+ 'keyword': keyword
43
+ }
44
+
45
+ url = "https://v2.alapi.cn/api/acg"
46
+ payload = {'token': custom_config['token'], 'format': 'json'}
47
+ headers = {'Content-Type': 'application/json'}
48
+
49
+ try:
50
+ res = requests.post(url, json=req_args, headers=headers, timeout=10).json()
51
+ except Exception as e:
52
+ return {
53
+ 'text': f"[ext_acg_image] 访问api时发生错误: {e}"
54
+
55
+ }
56
+
57
+ if 'data' in res and 'url' in res['data']:
58
+ return {
59
+ 'image': res['data']['url']
60
+ }
61
+ else:
62
+ error_msg = f"[ext_acg_image] 返回的数据格式不正确: {res}"
63
+ return {
64
+ 'text': error_msg if self.get_custom_config().get('debug', False) else None
65
+ }
66
+
67
+ def __init__(self, custom_config: dict):
68
+ super().__init__(self.ext_config.copy(), custom_config)
69
+