【问题标题】:Create a valid ConversationReference from scratch to send proactive messages从头开始创建有效的 ConversationReference 以发送主动消息
【发布时间】:2019-11-21 16:20:17
【问题描述】:

我能够使用机器人构建器示例中描述的方式向对话发送主动消息。到目前为止,我发现的所有样本都依赖于内存中的 ConversationReference

我还能够从转录 blob 存储中获取一条消息并回复该消息。

但我真正想要实现的是通过手动实例化来创建一个有效的ConversationReference。但我无法弄清楚我必须设置的必需属性才能使其工作。 我知道 channelId、serviceUrl 和 conversationId。

有人有一个工作示例如何生成有效的ConversationReference

【问题讨论】:

  • 正在使用什么频道?如果您希望在 Teams 中执行此操作,则非常困难,因为您需要大量附加信息。我会开始寻找答案。
  • 我正在使用直线和网络聊天。只是尝试您的建议,目前正在为 403 苦苦挣扎......
  • 403?你有TrustServiceUrl 部分吗?您使用的是正确的 appId 和密码吗?如果你像我一样编辑样本 16,你还会收到错误吗?如果您没有使用该示例,如果您错误地实例化 ConnectorClient,我会看到一些权限错误弹出;如果您包含一些代码,我可能会提供帮助。
  • 原来,403是由“bot”和“user”的内容交换引起的。我在使用 ApplyConversationReference 时没有将“isIncoming”设置为 true。

标签: c# botframework


【解决方案1】:

我相信某些频道可能有不同的要求。如果您正在寻找如何在特定频道上执行此操作,我可以更新此答案。对于 DirecLine/Webchat,我编辑了 Sample 16.proactive-messages。在NotifyController.cs,我把Get()的方法改成:

public async Task<IActionResult> Get()
{
    foreach (var conversationReference in _conversationReferences.Values)
    {
        // Here, I create my own ConversationReference from a known one for the purpose of testing requirements
        // I found that this is the bare minimum for WebChat/DirectLine
        var newReference = new ConversationReference()
        {
            Bot = new ChannelAccount()
            {
                Id = conversationReference.Bot.Id
            },
            Conversation = new ConversationAccount()
            {
                Id = conversationReference.Conversation.Id
            },
            ServiceUrl = conversationReference.ServiceUrl,
        };

        // You may need this to ensure the message isn't rejected
        MicrosoftAppCredentials.TrustServiceUrl(conversationReference.ServiceUrl);

        // Here, I replaced conversationReference with newReference, to ensure it's using the one I created
        await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, newReference, BotCallback, default(CancellationToken));
    }

    // Let the caller know proactive messages have been sent
    return new ContentResult()
    {
        Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
        ContentType = "text/html",
        StatusCode = (int)HttpStatusCode.OK,
    };
}

因此,如您所见,最小值似乎是:

var newReference = new ConversationReference()
{
    Bot = new ChannelAccount()
    {
        Id = conversationReference.Bot.Id
    },
    Conversation = new ConversationAccount()
    {
        Id = conversationReference.Conversation.Id
    },
    ServiceUrl = conversationReference.ServiceUrl,
};

参考文献

【讨论】:

  • 在不为用户指定 ChannelAccount 的情况下是否适合您?
  • @martinoss 确实如此。只需更改代码,我就能够将主动消息发送到 3 个不同的 WebChat 浏览器选项卡和模拟器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 2020-04-21
  • 2021-04-12
  • 2021-11-18
  • 1970-01-01
相关资源
最近更新 更多