对话总是在Directline BOT频道websocket中重新启动,如何保持对话顺畅?

时间:2019-10-24 19:03:44

标签: c# botframework twilio-api luis direct-line-botframework

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

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


相反,我得到了:
机器人:你好,你叫什么名字?
用户: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;

                }

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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