Bot Framework主动将ID传递到会话状态的消息

时间:2019-06-05 19:35:50

标签: botframework proactive

我有一个SMS / Twilio频道,用于向用户发送主动消息。要发送主动消息,我正在调用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);
            }
        }

1 个答案:

答案 0 :(得分:1)

我不相信你可以。通常,您可以通过通过auth.php传递值来进行类似的操作,但是Activity.ChannelDataChannelData上并不存在。


根据下面的评论,@ Ryan指出您可以在ConversationReference上传递数据。但是请注意,当前仅在C#SDK中可用。我已经打开an issue并将其带入Node SDK,但目前尚不知道ETA。


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

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

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

相关问题