2

如何获得 lex 聊天机器人对话?我想在聊天结束时发送聊天对话的电子邮件?

我正在使用 c# .net core 2.1 来构建 labda 函数是否有任何范围可以在 lambda 函数中进行聊天机器人对话?

4

1 回答 1

2

我通过在会话属性中保存 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);
    }
}
于 2019-04-23T10:15:08.070 回答