影刀BOSS机器人_发送飞书卡片

# 使用此指令前,请确保安装必要的Python库,例如使用以下命令安装:
# pip install requests

import requests

from typing import *
try:
    from xbot.app.logging import trace as print
except:
    from xbot import print


def send_boss_card(receiver_type, receiver_value, template_id, boss_token, modules):
    """
    title: BOSS机器人-发送飞书卡片
    description: 通过【影刀BOSS系统】机器人发送互动卡片。% receiver_type % 只支持 chatId/organizationName 等四种类型。
    inputs:
        - receiver_type (str): 接收者类型:chatId(群ID,默认)/organizationName(组织名称)/customNo(客户编号)/organizationUuid(组织uuid),eg: "chatId"
        - receiver_value (str): 接收者具体值,eg: "oc_xxxxxx"
        - template_id (str): 卡片模板ID,eg: "ctp_xxxxxx"
        - boss_token (str): 登录BOSS系统获取的token,eg: "xxxxxxx"
        - modules (list): 卡片模块列表,eg: [{"title": "标题", "content": "内容"}]
    outputs:
        - response (str): 接口返回内容,eg: '{"code":200}'
    """

    # 1. 检查输入有效性
    RECEIVER_TYPES = ("chatId", "customNo", "organizationUuid", "organizationName")
    
    receiver_type = str(receiver_type or "").strip() or "chatId"
    if receiver_type not in RECEIVER_TYPES:
        raise ValueError(f"receiver_type 只能是 {RECEIVER_TYPES} 之一,当前收到:{receiver_type}")

    if not modules:
        return ""

    def _execute_send():
        BOSS_URL = "https://boss-api.shadow-rpa.net/boss/api/v3/manager/message/sendCardToChat"
        CARD_VAR_NAME = "object_list_2"  # 循环容器变量名,需与卡片模板里的一致

        payload = {
            receiver_type: receiver_value,
            "templateId": template_id,
            "variableMap": {CARD_VAR_NAME: modules}
        }

        try:
            resp = requests.post(
                BOSS_URL,
                headers={"Authorization": f"Bearer {boss_token}"},
                json=payload,
                timeout=15,
            )
            resp.raise_for_status()
            
            result_json = resp.json()
            if result_json.get("code") != 200:
                raise Exception(f"BOSS返回失败:{resp.text}")
            
            return resp.text
        except requests.exceptions.RequestException as e:
            raise Exception(f"网络请求异常: {str(e)}")

    # 2. 执行发送逻辑
    return _execute_send()

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐