对话总是在 Directline BOT 通道 websocket 中重新开始,如何保持流畅?
Conversation always restarts in Directline BOT channel websocket, how to keep it flowing?
我构建了一个应用程序,它需要连接到 Bot DirectLine - websockets 通道以通过 LUIS 与 Twilio 进行对话和短信交互。
为了让机器人与应用程序对话,我编写了一个中继消息的 mvc 控制器。
我不确定这种方法是否正确,我是从一些样本中得出的。
它有效,但主要问题是我的代码似乎总是在从客户端收到消息时开始新的对话,因此不会维护上下文。
我怎样才能保持对话流畅而不是在每条消息都重新开始?
我的意思是,步骤应该是,例如:
机器人:你好,你叫什么名字?
用户:卡尔
机器人:很高兴认识你 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;
}
}
}
}
}
问题实际上是我没有保存和检索 conversationID。
目前我正在测试使用静态变量来存储值。
然后我重新连接到与它的对话,并且与机器人的对话保持在上下文中。
我构建了一个应用程序,它需要连接到 Bot DirectLine - websockets 通道以通过 LUIS 与 Twilio 进行对话和短信交互。
为了让机器人与应用程序对话,我编写了一个中继消息的 mvc 控制器。
我不确定这种方法是否正确,我是从一些样本中得出的。
它有效,但主要问题是我的代码似乎总是在从客户端收到消息时开始新的对话,因此不会维护上下文。
我怎样才能保持对话流畅而不是在每条消息都重新开始?
我的意思是,步骤应该是,例如:
机器人:你好,你叫什么名字?
用户:卡尔
机器人:很高兴认识你 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;
}
}
}
}
}
问题实际上是我没有保存和检索 conversationID。 目前我正在测试使用静态变量来存储值。 然后我重新连接到与它的对话,并且与机器人的对话保持在上下文中。