机器人框架主动消息将 ID 传递到对话状态



我有一个短信/Twilio频道,我用它来向用户发送主动消息。 为了发送主动消息,我调用了一个传入MySpecialId的API方法,该方法稍后将在对话中使用。

我想将此 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传递值来执行此类操作,但ChannelData ConversationReference上不存在。


根据下面的评论,@Ryan指出您可以在ConversationAccount.Properties上传递数据。但请注意,这目前仅在 C# SDK 中可用。我已经打开了一个问题以将其引入节点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访问MySpecialId(在 BotCallback 中(
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);

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

最新更新