TurnContext.updateActivity()引发错误:未知的活动ID

时间:2019-07-05 19:03:44

标签: node.js botframework

我正在尝试更新机器人发送给用户的消息。用例是在后台进程中通过状态更改不断更新卡。我正在使用Bot框架和Node.js的v4来实现Bot。

我正在尝试使用adapter.continueConversation()传达状态变化,如代码片段所示。

server.post('/api/notify', (req, res) => {
    const conversationReference: ConversationReference = conversationReferences[req.query.refId]; //stored conversation references   
    adapter.continueConversation(conversationReference, async (context) => {
        //stored activities
        const activity: Partial<Activity> = activityMap[req.query.i];
        let updatedActivity: Partial<Activity> = {};
        updatedActivity.id = req.query.id || activity.id;
        updatedActivity.text = 'Finally updated';
        updatedActivity.serviceUrl = activity.serviceUrl;
        updatedActivity.conversation = activity.conversation;
        console.log(updatedActivity);
        await context.updateActivity(updatedActivity); //Error
    });
    res.send(200);
    res.end();
});

要更新的活动的JSON

{
  "attachmentLayout": "list",
  "attachments": [
    {
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "body": [
          {
            "size": "Small",
            "text": "Dummy Text",
            "type": "TextBlock"
          }
        ],
        "type": "AdaptiveCard",
        "version": "1.0"
      },
      "contentType": "application/vnd.microsoft.card.adaptive"
    }
  ],
  "channelId": "emulator",
  "conversation": {
    "id": "87eaf011-9f4d-11e9-9f39-5522e8750ae9|livechat"
  },
  "from": {
    "id": "15fbd7c0-9ee0-11e9-9f39-5522e8750ae9",
    "name": "Bot",
    "role": "bot"
  },
  "id": "8de84b20-9f4d-11e9-be24-034531d2d092",
  "inputHint": "acceptingInput",
  "localTimestamp": "2019-07-05T23:21:45+05:30",
  "locale": "en-US",
  "recipient": {
    "id": "fb9fddeb-e180-472b-91b7-b963ee7cf89b",
    "role": "user"
  },
  "replyToId": "8ddba0f0-9f4d-11e9-be24-034531d2d092",
  "serviceUrl": "http://localhost:62975",
  "timestamp": "2019-07-05T17:51:45.362Z",
  "type": "message"
}

我认为该活动将使用文本“最终更新”进行更新,但我收到“错误:未知活动ID”。 ID为'8de84b20-9f4d-11e9-be24-034531d2d092',与已发送的活动ID相匹配。

2 个答案:

答案 0 :(得分:0)

尝试使用replyToId属性获取最后一个上下文活动ID 例子:

updatedActivity.id = context.activity.replyToId;
updatedActivity.serviceUrl = context.activity.serviceUrl;
await context.updateActivity(updatedActivity);

答案 1 :(得分:0)

您可能需要创建 turnConext 来发送消息。

const { TurnContext } = require('botbuilder');

const turnContext = await new TurnContext(adapter, conversationReference);
    await adapter.continueConversation(conversationReference, async () => {
        response = await turnContext.sendActivity({{Message or Card here}});
    });