Bot Framework 主动消息将 Id 传递到对话状态

Bot Framework Proactive Message Passing Id Into Conversation State

我有一个 SMS/Twilio 频道,用于向用户发送主动消息。为了发送 Proactive 消息,我调用了一个 API 方法,传入 MySpecialId 稍后在对话中使用。

我想将此 MySpecialId 保存到对话中,但在我拥有 MySpecialId 时对话尚不存在,而且我没有 turnContext,所以我还不能真正保存它。

有没有办法将这个 Id 从我的 API 方法传递到 BotCallback?我创建了一个简单的例子。 (这是我使用的原始示例 https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/16.proactive-messages

谢谢

        [HttpGet("{number}")]
        public async Task<IActionResult> Get(string MySpecialId)
        {
            //For Twillio Channel
            MicrosoftAppCredentials.TrustServiceUrl("https://sms.botframework.com/");

            var NewConversation = new ConversationReference
            {
                User = new ChannelAccount { Id = $"+1{PHONENUMBERTOTEXTHERE}" },
                Bot = new ChannelAccount { Id = "+1MYPHONENUMBERHERE" },
                Conversation = new ConversationAccount { Id = $"+1{PHONENUMBERTOTEXTHERE}" },
                ChannelId = "sms",
                ServiceUrl = "https://sms.botframework.com/"
            };

        BotAdapter ba = (BotAdapter)_HttpAdapter;
            await ((BotAdapter)_HttpAdapter).ContinueConversationAsync(_AppId, NewConversation, BotCallback, default(CancellationToken));

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

      private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            try
            {
                var MyConversationState = _ConversationState.CreateProperty<MyConversationData>(nameof(MyConversationData));
                var ConversationState = await MyConversationState.GetAsync(turnContext, () => new MyConversationData(), cancellationToken);

                //********************************************************************************************************************
                ConversationState.MySpecialId = //HOW DO I GET MySpecialId FROM THE GET METHOD ABOVE HERE?
                //********************************************************************************************************************

                await _ConversationState.SaveChangesAsync((turnContext, false, cancellationToken);
                await turnContext.SendActivityAsync("Starting proactive message bot call back");                    
            }
            catch (Exception ex)
            {
                this._Logger.LogError(ex.Message);
            }
        }

我不相信你可以。通常,您会通过 Activity.ChannelData 传递值来执行类似的操作,但是 ChannelDataConversationReference 上不存在。


根据下面的评论,@Ryan 指出您可以在 ConversationAccount.Properties 上传递数据。但是请注意,这目前仅在 C# SDK 中可用。我已经打开 an issue 将其引入 Node SDK,但此时 ETA 未知。


相反,我建议使用更适合 C# 的东西,例如:

  1. 创建 ConcurrentDictionary
private ConcurrentDictionary<string, string> _idMap;
  1. MySpecialId 映射到 Conversation.Id(在您的 Get 函数中)
_idMap.AddOrUpdate(conversationReference.Conversation.Id, MySpecialId, (key, newValue) => MySpecialId);
  1. Activity.Conversation.Id(在 BotCallback 中)访问 MySpecialId
var ConversationState = await MyConversationState.GetAsync(turnContext, () => new MyConversationData(), cancellationToken);
ConversationState.MySpecialId = _idMap.GetValueOrDefault(turnContext.Activity.Conversation.Id);
  1. 保存ConversationState
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);

还有其他方法可以执行此操作,并且您需要添加一些验证检查,但这应该可以帮助您入门。