gpt4 book ai didi

node.js - 微软团队机器人自适应卡片轮播删除卡片

转载 作者:太空宇宙 更新时间:2023-11-03 23:51:15 25 4
gpt4 key购买 nike

我正在将 Microsoft 团队机器人与 Node.js 一起使用。我正在渲染自适应卡的轮播,并在每张卡上执行操作。我的要求是删除单击该操作的单个卡片。是否可以?

当前代码如下所示。我尝试过删除事件,但这会删除整个轮播

const {
TurnContext,
TeamsActivityHandler,
CardFactory,
AttachmentLayoutTypes,
ActionTypes
} = require('botbuilder');

class TeamsConversationBot extends TeamsActivityHandler {
constructor() {
super();
this.onMessage(async (context:any, next:any) => {
TurnContext.removeRecipientMention(context.activity);
console.log("context activigty at the begin is:" + JSON.stringify(context.activity))
let msg = context.activity.text
let action = context.activity.value

if(msg.startsWith('lead')){
msg = 'lead'
}

if(action !== undefined){
console.log("user did some action on a card")
msg = action.action
}

switch (msg) {
case 'lead':
await this.lead(context)
break;
case 'qualify_lead':
await this.qualifyLead(context)
break;
}
await next();
});
}


/**
*
* @param context this method does a lead qualification
*/
async qualifyLead(context:any){
console.log("in qualifyLead:" + JSON.stringify(context.activity))
//await context.deleteActivity(context.activity.replyToId)

const leadId = context.activity.value.objectId
console.log("Lead to qualify is:" + leadId)


await context.sendActivity('Lead is qualified')
}


/**
* Search contact by name
* @param context
* @param keyword
*/
async lead(context:any){
console.log("Start of lead with context:" + JSON.stringify(context))
const cardArr = []
let items = [
{"Name": 'x', "LeadId": "1"},
{"Name": 'a', "LeadId": "2"},
{"Name": 'b', "LeadId": "3"},
{"Name": 'c', "LeadId": "4"},
{"Name": 'd', "LeadId": "5"}
]

for(const item of items){
const header = {
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": item.Name
}



const actions = [
{
"type": "Action.Submit",
"title": "Qualify",
"data": { "action" : "qualify_lead", "objectId" : item.LeadId }
}
]


const acard = CardFactory.adaptiveCard(
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
header,
''
],
"actions": actions
}
)

cardArr.push(acard)
console.log("payload is::::" + JSON.stringify(acard))
}

const reply = {
"attachments" : cardArr,
"attachmentLayout" : AttachmentLayoutTypes.Carousel
}

await context.sendActivity(reply);
}

}

module.exports.TeamsConversationBot = TeamsConversationBot;

最佳答案

this other answer一样,答案将类似于 this one 。我可以看到您正在尝试使用 TypeScript,但您的代码与 JavaScript 的偏差很小,因此我将用 JavaScript 编写我的答案。

First, you'll need a way of saving state for your [carousel] so you can update the [carousel]'s activity.

this.carouselState = this.conversationState.createProperty('carouselState');

You'll want a consistent way to generate your [carousel] that you can use when you send the [carousel] initially and when you update the [carousel].

createCarousel(batchId, leads)
{
const cardArr = [];

let items = [
{ "Name": 'x', "LeadId": 1 },
{ "Name": 'a', "LeadId": 2 },
{ "Name": 'b', "LeadId": 3 },
{ "Name": 'c', "LeadId": 4 },
{ "Name": 'd', "LeadId": 5 }
];

items = items.filter(item => leads.includes(item.LeadId));

for (const item of items) {
const header = {
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": item.Name
};

const actions = [
{
"type": "Action.Submit",
"title": "Qualify",
"data": { [KEYACTION]: ACTIONQUALIFYLEAD, [KEYOBJECTID]: item.LeadId, [KEYBATCHID]: batchId }
}
];

const acard = CardFactory.adaptiveCard(
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
header
],
"actions": actions
}
);

cardArr.push(acard);
}

return {
"type": "message",
"attachments": cardArr,
"attachmentLayout": AttachmentLayoutTypes.Carousel
};
}

这与您的代码类似,但有一些重要的区别。首先,我过滤项目数组以允许更少的项目,这就是您最终从轮播中删除卡片的方式。其次,我在每个操作的数据中包含一个“批处理 ID”,这样您的机器人在收到操作的有效负载时就会知道要更新哪个事件。另外,这与您的问题无关,但我在大多数地方都使用字符串常量而不是字符串文字,我希望多次使用该字符串,这是我遵循的做法,以避免与拼写错误相关的错误等。

Using this function, you can send the [carousel] initially like this

async testCarousel(turnContext) {
const batchId = Date.now();
const leads = [1, 2, 3, 4, 5];
const reply = this.createCarousel(batchId, leads);
const response = await turnContext.sendActivity(reply);
const dict = await this.carouselState.get(turnContext, {});

dict[batchId] = {
[KEYACTIVITYID]: response.id,
[KEYLEADS]: leads
};
}

And you can update the [carousel] in response to the card's [qualify] submit action like this

async handleSubmitAction(turnContext) {
const value = turnContext.activity.value;

switch (value[KEYACTION]) {
case ACTIONQUALIFYLEAD:
const dict = await this.carouselState.get(turnContext, {});
const batchId = value[KEYBATCHID];
const info = dict[batchId];
if (info) {
const leads = info[KEYLEADS];
const objectId = value[KEYOBJECTID];
var index = leads.indexOf(objectId);
if (index !== -1) leads.splice(index, 1);
const update = this.createCarousel(batchId, leads);
update.id = info[KEYACTIVITYID];
if (update.attachments.length) {
await turnContext.updateActivity(update);
} else {
await turnContext.deleteActivity(update.id);
}
}
break;
}
}

关于node.js - 微软团队机器人自适应卡片轮播删除卡片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59405348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com