通过 BotFramework 的 REST API 回复消息的最小示例?

Minimum example for replying to a message via BotFramework's REST API?

在我的 Python 网络应用程序中 Microsoft Botframework, I want to reply to a message with a REST API call/bot/v1.0/messages

在我的本地机器上试验模拟器时,我意识到 REST 调用的最小负载类似于:

{
  "text": "Hello, Hello!",
  "from": {
    "address": "MyBot"
  },
  "channelConversationId": "ConvId"
}

其中 "ConvId" 是我的本地模拟器在原始消息中给出的 ID(注意,我必须发送 channelConversationId 而不是 conversationId)。

显然,这对于实时机器人连接器站点来说还不够。但是,使用 REST API 调用 /bot/v1.0/messages 回复消息的(最小)示例是什么?

我测试了不同的负载数据,例如属性 fromtochannelConversationIdtextlanguage,如文档。但通常我会收到 500 错误:

{
  "error": {
    "message": "Expression evaluation failed. Object reference not set to an instance of an object.",
    "code": "ServiceError"
  }
}

当我试图发回原始消息时,只是交换了 tofrom,我收到了一个不同的 500 错误:

{
  "error": {
     "code": "ServiceError",
     "message": "*Sorry, Web Chat is having a problem responding right now.*",
     "statusCode": 500
  }
}

内联回复(作为响应返回)的最小负载为:

{ "text": "Hello, Hello!" }

如果您使用 POST 到 /bot/v1.0/messages 发布带外回复,那么您是对的,您需要更多。这是我在 Bot Builder SDK 的节点版本中所做的:

// Post an additional reply
reply.from = ses.message.to;
reply.to = ses.message.replyTo ? ses.message.replyTo : ses.message.from;
reply.replyToMessageId = ses.message.id;
reply.conversationId = ses.message.conversationId;
reply.channelConversationId = ses.message.channelConversationId;
reply.channelMessageId = ses.message.channelMessageId;
reply.participants = ses.message.participants;
reply.totalParticipants = ses.message.totalParticipants;
this.emit('reply', reply);
post(this.options, '/bot/v1.0/messages', reply, (err) => {
    if (err) {
        this.emit('error', err);
    }
});

发送对现有对话的回复有点复杂,因为您必须包含将其返回到源对话所需的所有路由位。开始新对话要容易得多:

// Start a new conversation
reply.from = ses.message.from;
reply.to = ses.message.to;
this.emit('send', reply);
post(this.options, '/bot/v1.0/messages', reply, (err) => {
    if (err) {
        this.emit('error', err);
    }
});

两个例子都假定 reply.textreply.language 已经设置。

同时,答案已发布至 a GitHub issue, quoting wiltodelta

Experimentally found necessary parameters for Slack, Skype, Telegram: ... ChannelConversationId only necessary Slack, otherwise the message will be added @userAddress.

Message message = new Message
{
  ChannelConversationId = channelConversationId,
  From = new ChannelAccount
  {
    ChannelId = channelId,
    Address = botAddress,
    IsBot = true
  },
  To = new ChannelAccount
  {
    ChannelId = channelId,
    Address = userAddress
  },
  Text = text
};

特别地,属性 replyToMessageIdchannelConversationId(前面提到的)不是必需的:它们指的是对话中最后看到的消息,因此会在对话期间发生变化。