BotFramework消息控制器通过Backchannel设置变量

问题描述 投票:1回答:1

我试图通过Backchannel接收一个int数(1-4),然后将其交给第一个对话框。

我的消息控制器看起来像这样:

       private int option = 1;
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message)
        {
            try
            {

                var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                Activity isTypingReply = activity.CreateReply();
                isTypingReply.Type = ActivityTypes.Typing;
                await connector.Conversations.ReplyToActivityAsync(isTypingReply);

                await Conversation.SendAsync(activity, () => new Dialogs.MenuDialog(option));


            }
            catch (Exception e)
            {
                //SendEmail(e);
            }
        }
        else
        {
            await HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private async Task HandleSystemMessage(Activity message)
    {
        if (message.Type == ActivityTypes.DeleteUserData)
        {
            // Implement user deletion here
            // If we handle user deletion, return a real message
        }
        else if (message.Type == ActivityTypes.ConversationUpdate)
        {

        }
        else if (message.Type == ActivityTypes.ContactRelationUpdate)
        {
            // Handle add/remove from contact lists
            // Activity.From + Activity.Action represent what happened
        }
        else if (message.Type == ActivityTypes.Typing)
        {
            // Handle knowing tha the user is typing
        }
        else if (message.Type == ActivityTypes.Ping)
        {
        }
        else if (message.Type == ActivityTypes.Event && message.Name == "option")
        {

           // var reply = message.CreateReply();
            //reply.Text = message.Value.ToString();
           // ConnectorClient connector = new ConnectorClient(new Uri(message.ServiceUrl));
           // await connector.Conversations.ReplyToActivityAsync(reply);

            if (message.Value.ToString() == "1")
            {
                option = 1;

            }
            else if (message.Value.ToString() == "2")
            {
                option = 2;
            }
            else if (message.Value.ToString() == "3")
            {
                option = 3;
            }
            else if (message.Value.ToString() == "4")
            {
                option = 4;
            }
            else
            {
                option = 1;
            }

        }


        return;
    }

Backchannel方法被调用为右,当我在函数末尾打印时,选项值被设置。但是当第一条消息出现时,Bot始终使用默认的“1”值。它以前工作但现在它停止工作,我不明白为什么。

c# azure botframework direct-line-botframework
1个回答
0
投票
private int option = 1;

限定为MessageController,并在每次调用时刷新。您可以使用PrivateConversationData来保留Event和Message调用之间的“选项”:

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        //retrieve the option value before processing the message
        string optionValue = string.Empty;
        using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
        {
            var botData = scope.Resolve<IBotData>();
            await botData.LoadAsync(CancellationToken.None);
            optionValue = botData.PrivateConversationData.GetValue<string>("option");
        }

        await Conversation.SendAsync(activity, () => new ParameterizedRootDialog(optionValue));

    }
    else if (activity.Type == ActivityTypes.Event)
    {
        var eventActivity = activity.AsEventActivity();
        if (string.Equals(eventActivity.Name, "option", StringComparison.InvariantCultureIgnoreCase))
        {
            //save the option into PrivateConversationData
            string optionValue = eventActivity.Value.ToString();
            using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
            {
                var botData = scope.Resolve<IBotData>();
                await botData.LoadAsync(CancellationToken.None);
                botData.PrivateConversationData.SetValue("option", optionValue);
                await botData.FlushAsync(CancellationToken.None);
            }                    
        }
    }

    return Request.CreateResponse(HttpStatusCode.OK);
}

另外值得注意的是:使用此方法,不必将选项作为参数发送到对话框。您可以使用IDialogContext.PrivateConversationData从对话框本身中检索值。像这样:

var optionFromContext = context.PrivateConversationData.GetValue<string>("option");
© www.soinside.com 2019 - 2024. All rights reserved.