"""
@author: luojunhui
"""
import json
import requests

from applications.decorator import retryOnTimeout


@retryOnTimeout()
def bot(title, detail, mention=True):
    """
    机器人
    """
    title_obj = {
        "content": "{}<at id=all></at>\n".format(title) if mention else "{}\n".format(title),
        "tag": "lark_md",
    }
    head_title = "【重点关注】" if mention else "【普通通知】"
    url = "https://open.feishu.cn/open-apis/bot/v2/hook/b44333f2-16c0-4cb1-af01-d135f8704410"
    headers = {"Content-Type": "application/json"}
    payload = {
        "msg_type": "interactive",
        "card": {
            "elements": [
                {
                    "tag": "div",
                    "text": title_obj,
                },
                {
                    "tag": "div",
                    "text": {
                        "content": json.dumps(
                            detail, ensure_ascii=False, indent=4
                        ),
                        "tag": "lark_md",
                    },
                },
            ],
            "header": {"title": {"content": head_title, "tag": "plain_text"}},
        },
    }
    requests.request("POST", url=url, headers=headers, data=json.dumps(payload), timeout=10)