// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Linq; using Microsoft.Bot.Schema; using Newtonsoft.Json.Linq; namespace Microsoft.Bot.Builder { /// /// Contains utility methods for creating various event types. /// public static class EventFactory { /// /// Create handoff initiation event. /// /// turn context. /// agent hub-specific context. /// transcript of the conversation. /// handoff event. public static IEventActivity CreateHandoffInitiation(ITurnContext turnContext, object handoffContext, Transcript transcript = null) { if (turnContext == null) { throw new ArgumentNullException(nameof(turnContext)); } var handoffEvent = CreateHandoffEvent(HandoffEventNames.InitiateHandoff, handoffContext, turnContext.Activity.Conversation); handoffEvent.From = turnContext.Activity.From; handoffEvent.RelatesTo = turnContext.Activity.GetConversationReference(); handoffEvent.ReplyToId = turnContext.Activity.Id; handoffEvent.ServiceUrl = turnContext.Activity.ServiceUrl; handoffEvent.ChannelId = turnContext.Activity.ChannelId; if (transcript != null) { var attchment = new Attachment { Content = transcript, ContentType = "application/json", Name = "Transcript", }; handoffEvent.Attachments.Add(attchment); } return handoffEvent; } /// /// Create handoff status event. /// /// Conversation being handed over. /// State, possible values are: "accepted", "failed", "completed". /// Additional message for failed handoff. /// handoff event. public static IEventActivity CreateHandoffStatus(ConversationAccount conversation, string state, string message = null) { if (conversation == null) { throw new ArgumentNullException(nameof(conversation)); } if (state == null) { throw new ArgumentNullException(nameof(state)); } object value = new { state, message }; var handoffEvent = CreateHandoffEvent(HandoffEventNames.HandoffStatus, value, conversation); return handoffEvent; } private static Activity CreateHandoffEvent(string name, object value, ConversationAccount conversation) { var handoffEvent = Activity.CreateEventActivity() as Activity; handoffEvent.Name = name; handoffEvent.Value = value == null ? null : JObject.FromObject(value); handoffEvent.Id = Guid.NewGuid().ToString(); handoffEvent.Timestamp = DateTime.UtcNow; handoffEvent.Conversation = conversation; handoffEvent.Attachments = new List(); handoffEvent.Entities = new List(); return handoffEvent; } } }