如何获得lex chat bot对话?我想在聊天结束时发送聊天对话的电子邮件吗?
我正在使用c#.net core 2.1来构建labda函数,在lambda函数中是否可以进行聊天机器人对话?
答案 0 :(得分:1)
下面是助手类。
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);
}
}