|
from .Extension import Extension |
|
import requests, random |
|
|
|
|
|
ext_config = { |
|
"name": "acg_image", |
|
"arguments": { |
|
"keyword": "str", |
|
}, |
|
"description": "Send an ACG image related to the keyword(in Chinese). Usage in response: /#acg_image&动漫#/", |
|
|
|
"refer_word": [], |
|
|
|
"max_call_times_per_msg": 3, |
|
|
|
"author": "KroMiose", |
|
|
|
"version": "0.0.1", |
|
|
|
"intro": "发送ACG图片", |
|
} |
|
|
|
|
|
class CustomExtension(Extension): |
|
async def call(self, arg_dict: dict, ctx_data: dict) -> dict: |
|
""" 当拓展被调用时执行的函数 *由拓展自行实现* |
|
|
|
参数: |
|
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值} |
|
""" |
|
custom_config: dict = self.get_custom_config() |
|
|
|
token = custom_config.get('token', None) |
|
if token is None: |
|
return { |
|
'text': error_msg if self.get_custom_config().get('debug', False) else None |
|
} |
|
|
|
keyword = arg_dict.get('keyword', '') |
|
req_args = { |
|
'token': custom_config['token'], |
|
'format': 'json', |
|
'keyword': keyword |
|
} |
|
|
|
url = "https://v2.alapi.cn/api/acg" |
|
payload = {'token': custom_config['token'], 'format': 'json'} |
|
headers = {'Content-Type': 'application/json'} |
|
|
|
try: |
|
res = requests.post(url, json=req_args, headers=headers, timeout=10).json() |
|
except Exception as e: |
|
return { |
|
'text': f"[ext_acg_image] 访问api时发生错误: {e}" |
|
|
|
} |
|
|
|
if 'data' in res and 'url' in res['data']: |
|
return { |
|
'image': res['data']['url'] |
|
} |
|
else: |
|
error_msg = f"[ext_acg_image] 返回的数据格式不正确: {res}" |
|
return { |
|
'text': error_msg if self.get_custom_config().get('debug', False) else None |
|
} |
|
|
|
def __init__(self, custom_config: dict): |
|
super().__init__(ext_config.copy(), custom_config) |
|
|
|
|