【问题标题】:Update already posted Adaptive Card in Bot Framework v4 in javascript更新已在 JavaScript 中的 Bot Framework v4 中发布 Adaptive Card
【发布时间】:2020-12-10 08:42:15
【问题描述】:

我是 botframework 和微软团队的新手。我正在尝试构建一个将发布自适应卡的机器人。在提交卡片时,我想执行一些操作并更新现有卡片。

我能够在 microsoft 团队中创建张贴自适应卡片,并且当用户提交操作时,我能够捕获数据。我被困的地方无法用新卡更新现有卡。

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

const TextEncoder = require('util').TextEncoder;


const WELCOME_TEXT = 'This bot will introduce you to Adaptive Cards. Type anything to see an Adaptive Card.';

var replyToID = "";

class AdaptiveCardsBot extends TeamsActivityHandler {
    constructor() {
        super();
        // See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
        this.onMessage(async (context, next) => {
            // const replyText = `Echo: ${ context.activity.text }`;
            var txt = context.activity.text;
            var val = context.activity.value;
            

            if (!txt && val) {
                await this.sendUpdatedCard(context);
            } else
            {
                const sampleCard = require('./resources/adaptiveSample.json');
                console.log (JSON.stringify(context.activity));
                console.log ("---------------------");
                console.log ("---------------------");
                //console.log("Core ID" + context.activity.replyToId);
                var reply = await context.sendActivity(
                    {
                        text: 'Here is an Adaptive Card:',
                        attachments: [CardFactory.adaptiveCard(sampleCard)]
                    });
                
                console.log (JSON.stringify(reply));
                console.log ("---------------------");
                console.log ("---------------------");
                replyToID = reply.id;
                var reference = TurnContext.getReplyConversationReference(context.activity, reply)
                replyToID = reference.activityId;       
                
            }
            // By calling next() you ensure that the next BotHandler is run.
            await next();
        });

        this.onMembersAdded(async (context, next) => {
            const membersAdded = context.activity.membersAdded;
            for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
                if (membersAdded[cnt].id !== context.activity.recipient.id) {
                    await context.sendActivity(`Welcome to Adaptive Cards Bot  ${ membersAdded[cnt].name }. ${ WELCOME_TEXT }`);
                }
            }
            // By calling next() you ensure that the next BotHandler is run.
            await next();
        });
    }
    async sendUpdatedCard(context)
        {
            const data = context.activity.value;
            console.log("Call Submit to Asana");
            console.log (JSON.stringify(context.activity));
            console.log ("---------------------");
            console.log ("---------------------");
            console.log ("replyToID :" + replyToID);
            const returnCard = require('./resources/returnSample.json');

            const card2 = CardFactory.adaptiveCard(returnCard);
            card2.id = replyToID;
            const newActivity = MessageFactory.attachment(card2);
            newActivity.id = replyToID;
            newActivity.type = "message";
            await context.updateActivity(newActivity);
        }
}


// await context.sendActivity({
//     text: 'Here is a return card',
//     attachments: [CardFactory.adaptiveCard(returnCard)]
// });
//const newActivity = MessageFactory.attachment(CardFactory.adaptiveCard(returnCard));
module.exports.AdaptiveCardsBot = AdaptiveCardsBot;


任何帮助将不胜感激。

附上模拟器的图片

【问题讨论】:

    标签: javascript node.js botframework adaptive-cards


    【解决方案1】:

    @Pawan 请转至Updating Bot Messages。这是sample code in Node,用于在单击按钮时使用新卡更新现有卡。

    【讨论】:

    • 我在node.中浏览了上面的示例代码。当调用Action.Submit时,没有replyToID。即 context.activity.replyToID 未定义。
    • 您是否在 Teams 的个人范围内尝试此操作?
    • 是的。我正在尝试在 1-1 设置中使用机器人框架模拟器。附上原始问题中的图片。
    猜你喜欢
    • 2020-01-25
    • 2019-03-13
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多