c# - 对话总是在 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;
}
}
}
}
}
解决方案
问题实际上是我没有保存和检索对话ID。目前我正在测试使用静态变量来存储值。然后我重新连接到与它的对话,并且与机器人的对话保持在上下文中。
推荐阅读
- python-3.x - 问题是来自hackerrank的匹配销售
- c++ - 如何隐藏从静态 LibXml2.a 导出的符号
- python - 在 Django Rest Framework 中缓存序列化程序响应
- java - 在 BuildConfig 上生成 javadoc 失败
- reactjs - Wordpress registerBlockType,保存时未获取数据
- laravel - 雄辩的查询虽然有多个 whereHas
- google-apps-script - 使用 GAS 通过 ServiceM8 API 上传附件问题
- python - 有没有办法调试嵌入在 Visual Studio 中的 C++ 解决方案中运行的 python 脚本?
- flutter - 在颤动中使用facebook登录后电子邮件为空
- c# - 何时以及如何将任何状态设置为“已过期”?