amazon-web-services - 如何获得聊天机器人的聊天对话(在 aws lex 中)
问题描述
如何获得 lex 聊天机器人对话?我想在聊天结束时发送聊天对话的电子邮件?
我正在使用 c# .net core 2.1 来构建 labda 函数是否有任何范围可以在 lambda 函数中进行聊天机器人对话?
解决方案
我通过在会话属性中保存 InputTranscript 和 lambda 响应来管理。在关闭事件中,我正在发送包含会话属性内容的电子邮件。
下面是助手类。
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);
}
}
推荐阅读
- spring-boot - 检查 Spring Boot (Webflux) 过滤器性能
- python - 如何使用日期时间中第一列的 Excel 数据?
- javascript - 切换到另一个页面时如何保存用户主题选择
- excel - 为什么我的电子表格在运行我的脚本后显示来自另一个工作表的重影图像
- cypress - 调试为什么 cy.visit() 会导致测试运行程序崩溃
- javascript - 有没有办法关闭 iframe 中的脚本打开的选项卡?
- javascript - 如何使用 javascript 使导航栏整齐地打开和关闭?
- visual-studio - 将两个 Github 脚本放在一起
- typescript - 如何从 TS 接口/类型列出类型/值?
- c++ - unique_ptr 的自定义克隆不返回正确的类型