钉钉 ai卡片 stream模式联调

发布于:2024-07-27 ⋅ 阅读:(169) ⋅ 点赞:(0)

sdk连接

  1. 新建卡片模板
  2. 下载node.js sdk
  3. config.json 配置应用信息 启动项目npm i npm run build npm run start
  4. 连接成功
    在这里插入图片描述

获取卡片回调

  1. 注册卡片回调事件
  2. 调用https://api.dingtalk.com/v1.0/card/instances 创建卡片实例,返回实例Id
//参数结构
{
    "cardTemplateId": "b7078d27-e321-4ee6-9b2d-ef3cb89cea05.schema",
    "outTrackId": "asdasdas",
    "cardData" : {
    "cardParamMap" : {
      "flowStatus" : "3", //ai卡片状态
      "feedbacks":"",
      "feedbackinput": "",
      "markdowm": "钉钉平台<font color=common_blue1_color>markdown内容</font>**",
  	  "likeStatus": "",
  	  "likes": "0"
    }
  },
    "robotCode": "dingimeeh8go8yo8usyc",
    "callbackType":"STREAM"
}
  1. 调用https://api.dingtalk.com/v1.0/card/instances/spaces 新增场域
{
    "outTrackId" : "asdasdas",
    "imGroupOpenSpaceModel" : { // 群聊场域属性
    "supportForward" : false, // 是否支持转发
    "lastMessageI18n" : { // 消息lastMessage
        "ZH_CN": "卡片",
        "EN_US": "card"
      }
    }
  }
  1. 调用https://api.dingtalk.com/v1.0/card/instances/deliver 投放卡片
{
  "outTrackId" : "asdasdas",
  "openSpaceId" : "dtv1.card//IM_GROUP.cid5o+bbZdK/80y1V5ESE5Trw==",
  "imGroupOpenDeliverModel" : { // 群聊投放属性
   "robotCode" : "dingimeeh8go8yo8usyc" // 机器人code
  }
}

在这里插入图片描述

更新示例

/** 卡片回调 */
client.registerCallbackListener(TOPIC_CARD, async (res) => {
  // 注册卡片回调事件
  console.log("收到卡片消息");
  const accessToken = await client.getAccessToken();
  let datas = JSON.parse(res.data)
  let contens = JSON.parse(datas.content)
  console.log(contens)
  //更新数据
  const obj = {} as any //卡片定义回调的字段参数
  const params = contens.cardPrivateData.params
  if(contens.cardPrivateData.actionIds[0] === 'multiSelect'){
    obj['feedbacks'] = JSON.stringify(params.multiSelect.index)
  }else if(contens.cardPrivateData.actionIds[0] === 'input'){
    obj['feedbackinput'] =  params.input
  }else {
    obj['likeStatus'] =  params.action || params.axtion
    if(params.axtion === 'dislike'){
	 obj['likes'] =  '0'
    }else {
 	 obj['likes'] =  '1'
    }
   
  }
  const dataJson = {
    "cardTemplateId": "b7078d27-e321-4ee6-9b2d-ef3cb89cea05.schema",
    "outTrackId": datas.outTrackId,
    "cardData": {
      "cardParamMap": obj,
      "robotCode": "dingimeeh8go8yo8usyc",
      "callbackType": "STREAM"
    },
    "cardUpdateOptions": {
      "updateCardDataByKey": true
    },
  }
  const result = await axios({
    url: 'https://api.dingtalk.com/v1.0/card/instances',
    method: "PUT",
    responseType: "json",
    data: dataJson,
    headers: {
      "x-acs-dingtalk-access-token": accessToken,
    },
  });
  if (result) {
    console.log("更新回调卡片成功");
    console.log((result.data));
  }
});