首页 > 解决方案 > 对话总是在 Directline BOT 通道 websocket 中重新启动,如何保持流畅?

问题描述

我已经构建了一个需要连接到 Bot DirectLine - websockets 通道的应用程序,以通过 LUIS 和与 Twilio 的短信进行对话。为了让机器人与应用程序对话,我编写了一个中继消息的 mvc 控制器。我不确定这种方法是否正确,我是从一些样本中得出的。它可以工作,但主要问题是我的代码似乎总是在从客户端收到消息时开始新的对话,因此没有维护上下文。我怎样才能保持对话流畅,而不是在每条消息都重新开始?我的意思是,步骤应该是,例如:

Bot:你好,你叫什么名字?
用户:Carl
Bot:很高兴认识你 Carl!


相反,我得到:
Bot:你好,你叫什么名字?
用户:Carl
Bot:对不起,我帮不了你。

就像对话从头开始一样。

这是我的控制器代码(Twilio webhook 设置为https://mySmsMVCapp.azurewebsites.net/smsapp/):

public class smsappController : TwilioController
{

    private static string directLineSecret = ConfigurationManager.AppSettings["DirectLineSecret"];
    private static string botId = ConfigurationManager.AppSettings["BotId"];


    const string accountSid = "obfuscated";
    const string authToken = "obfuscated";

    private static string fromUser = "DirectLineSampleClientUser";
    private string SMSreply = "";


    public async Task<TwiMLResult> Index(SmsRequest incomingMessage)
    {

            // Obtain a token using the Direct Line secret
            var tokenResponse = await new DirectLineClient(directLineSecret).Tokens.GenerateTokenForNewConversationAsync();

            // Use token to create conversation
            var directLineClient = new DirectLineClient(tokenResponse.Token);
            var conversation = await directLineClient.Conversations.StartConversationAsync();


        using (var webSocketClient = new WebSocket(conversation.StreamUrl))
        {
            webSocketClient.OnMessage += WebSocketClient_OnMessage;
            // You have to specify TLS version to 1.2 or connection will be failed in handshake.
            webSocketClient.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            webSocketClient.Connect();

            while (true)
            {
                string input = incomingMessage.Body;
                if (!string.IsNullOrEmpty(input))
                {
                    if (input.ToLower() == "exit")
                    {
                        break;
                    }
                    else
                    {
                        if (input.Length > 0)
                        {
                            Activity userMessage = new Activity
                            {
                                From = new ChannelAccount(fromUser),
                                Text = input,
                                Type = ActivityTypes.Message
                            };

                            await directLineClient.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
                            //break;

                            if (!string.IsNullOrEmpty(SMSreply))
                            {
                                var messagingResponse = new MessagingResponse();
                                var message = messagingResponse.AddChild("Message");

                                message.AddText(SMSreply); //send text


                                SMSreply = string.Empty;
                                return TwiML(messagingResponse);
                            }

                        }
                    }
                }
            }
        }

        return null;

    }


    private void WebSocketClient_OnMessage(object sender, MessageEventArgs e)
    {
        // Occasionally, the Direct Line service sends an empty message as a liveness ping. Ignore these messages.
        if (!string.IsNullOrWhiteSpace(e.Data))
        {

            var activitySet = JsonConvert.DeserializeObject<ActivitySet>(e.Data);
            var activities = from x in activitySet.Activities
                             where x.From.Id == botId
                             select x;

            foreach (Activity activity in activities)
            {
                if (!string.IsNullOrEmpty(activity.Text))
                {

                    SMSreply = activity.Text;

                }

            }
        }
    }
}

标签: c#botframeworktwilio-apiazure-language-understandingdirect-line-botframework

解决方案


问题实际上是我没有保存和检索对话ID。目前我正在测试使用静态变量来存储值。然后我重新连接到与它的对话,并且与机器人的对话保持在上下文中。


推荐阅读