Amazon web services 如何获取聊天机器人的聊天对话(在aws lex中)
如何获得lex聊天机器人对话?我想在聊天结束时发送聊天对话的电子邮件Amazon web services 如何获取聊天机器人的聊天对话(在aws lex中),amazon-web-services,aws-lambda,aws-lex,Amazon Web Services,Aws Lambda,Aws Lex,如何获得lex聊天机器人对话?我想在聊天结束时发送聊天对话的电子邮件 我正在使用c#.net core 2.1构建labda函数在lambda函数中是否有获取聊天机器人对话的空间?我通过在会话属性中保存InputTranscript和lambda响应来管理。在关闭事件时,我发送带有会话属性内容的电子邮件 下面是helper类 public abstract class AbstractIntentProcessor : IIntentProcessor { internal const
我正在使用c#.net core 2.1构建labda函数在lambda函数中是否有获取聊天机器人对话的空间?我通过在会话属性中保存InputTranscript和lambda响应来管理。在关闭事件时,我发送带有会话属性内容的电子邮件 下面是helper类
public abstract class AbstractIntentProcessor : IIntentProcessor
{
internal const string MESSAGE_CONTENT_TYPE = "PlainText";
public abstract Task<LexResponse> Process(LexEvent lexEvent, ILambdaContext context);
protected string SerializeReservation(UtilityBillRequest request)
{
return JsonConvert.SerializeObject(request, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
protected UtilityBillRequest DeserializeReservation(string json)
{
return JsonConvert.DeserializeObject<UtilityBillRequest>(json);
}
protected List<ConversationScript> DeserializeConversation(string json)
{
return JsonConvert.DeserializeObject<List<ConversationScript>>(json);
}
protected string SerializeConversation(List<ConversationScript> result)
{
return JsonConvert.SerializeObject(result, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
}
protected LexResponse Close(IDictionary<string, string> sessionAttributes, string fulfillmentState, LexResponse.LexMessage message, bool isSessionClose=false, LexEvent lexEvent=null, ILambdaContext context=null)
{
AppendTranscript(sessionAttributes, "Bot", message.Content);
var transcript = new List<ConversationScript>();
if (isSessionClose && sessionAttributes.ContainsKey("transcript"))
{
transcript = DeserializeConversation(sessionAttributes["transcript"]);
EmailHelper emailHelper = new EmailHelper();
emailHelper.SendTranscriptEmail(transcript, lexEvent, context);
}
return new LexResponse
{
SessionAttributes = sessionAttributes,
DialogAction = new LexResponse.LexDialogAction
{
Type = "Close",
FulfillmentState = fulfillmentState,
Message = message
}
};
}
protected LexResponse Delegate(IDictionary<string, string> sessionAttributes, IDictionary<string, string> slots)
{
return new LexResponse
{
SessionAttributes = sessionAttributes,
DialogAction = new LexResponse.LexDialogAction
{
Type = "Delegate",
Slots = slots
}
};
}
protected LexResponse ElicitSlot(IDictionary<string, string> sessionAttributes, string intentName, IDictionary<string, string> slots, string slotToElicit, LexResponse.LexMessage message)
{
AppendTranscript(sessionAttributes, "Bot", message.Content);
return new LexResponse
{
SessionAttributes = sessionAttributes,
DialogAction = new LexResponse.LexDialogAction
{
Type = "ElicitSlot",
IntentName = intentName,
Slots = slots,
SlotToElicit = slotToElicit,
Message = message
}
};
}
protected LexResponse ConfirmIntent(IDictionary<string, string> sessionAttributes, string intentName, IDictionary<string, string> slots, LexResponse.LexMessage message)
{
AppendTranscript(sessionAttributes, "Bot", message.Content);
return new LexResponse
{
SessionAttributes = sessionAttributes,
DialogAction = new LexResponse.LexDialogAction
{
Type = "ConfirmIntent",
IntentName = intentName,
Slots = slots,
Message = message
}
};
}
//
public void AppendTranscript(IDictionary<string, string> sessionAttributes, string source, string message)
{
if (source != "Bot" && source != "User")
{
throw new Exception("Invalid Source: " + source);
}
var transcript = new List<ConversationScript>();
if (sessionAttributes.ContainsKey("transcript"))
{
transcript = DeserializeConversation(sessionAttributes["transcript"]);
}
transcript.Add(new ConversationScript
{
Participant = source,
Text = message,
Timestamp = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")
});
sessionAttributes["transcript"] = SerializeConversation(transcript);
}
}
公共抽象类AbstractIntentProcessor:IIntentProcessor
{
内部常量字符串消息内容类型=“明文”;
公共抽象任务流程(LexEvent LexEvent、ILambdaContext上下文);
受保护的字符串序列化保留(UtilityBillRequest请求)
{
返回JsonConvert.SerializeObject(请求,新JsonSerializerSettings
{
NullValueHandling=NullValueHandling.Ignore
});
}
受保护的实用程序BillRequest反序列化保留(字符串json)
{
返回JsonConvert.DeserializeObject(json);
}
受保护列表反序列化会话(字符串json)
{
返回JsonConvert.DeserializeObject(json);
}
受保护的字符串序列化会话(列表结果)
{
返回JsonConvert.SerializeObject(结果,新的JsonSerializerSettings
{
NullValueHandling=NullValueHandling.Ignore
});
}
受保护的LexResponse关闭(IDictionary SessionAttribute、string fulfillmentState、LexResponse.LexMessage消息、bool isSessionClose=false、LexEvent LexEvent=null、ILambdaContext context=null)
{
附加发言稿(会期讲稿,“Bot”,message.Content);
var transcript=新列表();
if(isSessionClose&sessionAttributes.ContainsKey(“转录本”))
{
转录本=反序列化对话(sessionAttributes[“转录本]);
EmailHelper EmailHelper=新的EmailHelper();
SendTranscriptEmail(文本、事件、上下文);
}
返回新的响应
{
会期贡献=会期贡献,
DialogAction=新建LexResponse.LexDialogAction
{
Type=“Close”,
FulfillmentState=FulfillmentState,
消息=消息
}
};
}
受保护的LexResponse委托(IDictionary SessionAttribute、IDictionary插槽)
{
返回新的响应
{
会期贡献=会期贡献,
DialogAction=新建LexResponse.LexDialogAction
{
Type=“委托”,
插槽=插槽
}
};
}
受保护的LexResponse Elicit插槽(IDictionary sessionAttributes、string intentName、IDictionary插槽、string slotToElicit、LexResponse.LexMessage消息)
{
附加发言稿(会期讲稿,“Bot”,message.Content);
返回新的响应
{
会期贡献=会期贡献,
DialogAction=新建LexResponse.LexDialogAction
{
Type=“插槽”,
IntentName=IntentName,
插槽=插槽,
SlotToElicit=SlotToElicit,
消息=消息
}
};
}
受保护的LexResponse确认(IDictionary SessionAttribute、字符串名称、IDictionary插槽、LexResponse.LexMessage)
{
附加发言稿(会期讲稿,“Bot”,message.Content);
返回新的响应
{
会期贡献=会期贡献,
DialogAction=新建LexResponse.LexDialogAction
{
Type=“confirminant”,
IntentName=IntentName,
插槽=插槽,
消息=消息
}
};
}
//
public void AppendTranscript(IDictionary SessionAttribute、字符串源、字符串消息)
{
if(源!=“机器人”&&source!=“用户”)
{
抛出新异常(“无效源:+源”);
}
var transcript=新列表();
if(会议致辞全文(“抄本”))
{
转录本=反序列化对话(sessionAttributes[“转录本]);
}
添加(新对话脚本)
{
参与者=来源,
文本=消息,
Timestamp=DateTime.Now.ToString(“MM/dd/yyyy HH:MM:ss”)
});
sessionAttributes[“转录本”]=连续对话(转录本);
}
}
Lex没有保存完整的对话记录,至少在我们可以访问的地方没有。您必须自己保存对话的输入和响应,可以保存在数据库中,也可以保存在sessionAttributes的短期内。是的,谢谢您的评论,我也采用了同样的方法