1

我有一个 SMS / 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);
            }
        }
4

1 回答 1

1

我不相信你可以。通常,你会通过传递值来做这样的事情Activity.ChannelData,但ChannelDataConversationReference.


根据下面的评论,@Ryan 指出您可以在ConversationAccount.Properties. 但是请注意,这目前仅在 C# SDK 中可用。我已经打开了一个问题,将其引入 Node SDK,但目前尚不清楚 ETA。


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

  1. 创建一个ConcurrentDictionary
private ConcurrentDictionary<string, string> _idMap;
  1. 映射MySpecialIdConversation.Id(在您的 Get 函数中)
_idMap.AddOrUpdate(conversationReference.Conversation.Id, MySpecialId, (key, newValue) => MySpecialId);
  1. MySpecialIdActivity.Conversation.Id(在 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);

还有其他方法可以做到这一点,并且您需要添加一些验证检查,但这应该可以帮助您入门。

于 2019-06-06T18:08:58.970 回答