amazon-web-servicesaws-lambdaaws-lex

How to get chat-conversation of chatbot(in aws lex)


How to get the lex chat bot conversation ? i would like to send email of the chat conversation at end of the chat ?

i am using c# .net core 2.1 to build labda functions is there any scope to get chat bot conversation in lambda function ?


Solution

  • i managed by saving InputTranscript and lambda response in session attributes. on close event i am sending email with the session attributes content.

    below is the helper class.

    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);
        }
    }