File size: 2,456 Bytes
48fba35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from .Extension import Extension
import requests, random

# 拓展的配置信息,用于ai理解拓展的功能 *必填*
ext_config = {
    "name": "one_photo",
    "arguments": {
        "keyword": "str",   # 关键字
    },
    "description": "Send an ACG image related to the keyword(in Chinese). Usage in response: /#one_photo&2022-12-21#/",
    # 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
    "refer_word": [],
    # 每次消息回复中最大调用次数,不填则默认为99
    "max_call_times_per_msg": 3,
    # 作者信息
    "author": "KroMiose",
    # 版本
    "version": "0.0.1",
    # 拓展简介
    "intro": "发送摄影图片",
}

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()  # 获取yaml中的配置信息

        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'],
            'date': '2021-04-21',
        }
        url = "https://v2.alapi.cn/api/one/photo"
        headers = {'Content-Type': 'application/json'}

        try:
            res = requests.get(url, params=req_args, headers=headers, timeout=10).json()
        except Exception as e:
            return {
                'text': f"[ext_acg_image] 访问api时发生错误: {e}"
            }

        if 'data' in res:
            data = res['data']
            return {
                'title': data.get('title', ''),
                'subtitle': data.get('subtitle', ''),
                'content': data.get('content', ''),
                'cover': data.get('cover', ''),
                'make_time': data.get('make_time', '')
            }
        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)