public static async Task <EntityReference> PromptInitialQuestion(Activity context, ConnectorClient connector, IOrganizationService service, Microsoft.Xrm.Sdk.Entity cxConversation, Microsoft.Xrm.Sdk.Entity cxCurrentStep) { Microsoft.Xrm.Sdk.Entity cxStep = cxCurrentStep; //update conversation to reflect the current step and initiate any workflow triggers Microsoft.Xrm.Sdk.Entity upsConversation1 = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversation", "aspect_conversationid", context.Conversation.Id); upsConversation1["aspect_currentcxstepid"] = new EntityReference(cxCurrentStep.LogicalName, cxCurrentStep.Id); upsConversation1["aspect_nextcxstepid"] = null; service.Execute(new UpsertRequest() { Target = upsConversation1 }); string responseText = ReplaceOutputText(service, new EntityReference(cxConversation.LogicalName, cxConversation.Id), (string)cxStep["aspect_message"]); //Create a conversation message with the text sent to the user Microsoft.Xrm.Sdk.Entity conversationClient = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversationmessage"); conversationClient["aspect_cxconversationid"] = new EntityReference("aspect_cxconversation", cxConversation.Id); conversationClient["aspect_cxstepid"] = new EntityReference(cxCurrentStep.LogicalName, cxCurrentStep.Id); conversationClient["aspect_direction"] = true; conversationClient["aspect_name"] = cxConversation.Id.ToString(); conversationClient["aspect_message"] = responseText; service.Create(conversationClient); //send reply var responseMessage = context.CreateReply(); responseMessage.Text = responseText; responseMessage.Speak = responseText; responseMessage.InputHint = Dynamics.CXGetAnswers(service, cxStep); responseMessage.Value = JsonConvert.SerializeObject(new Dynamics.CXInformation() { //CXBotId = ((Guid)(((AliasedValue)["aspect_cxbot.aspect_cxbotid"]).Value)).ToString(), CXBotName = (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_name"]).Value), AudioDirectory = cxCurrentStep.Contains("aspect_cxbot.aspect_audiodirectory") ? (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_audiodirectory"]).Value) : string.Empty, RecordingDirectory = cxCurrentStep.Contains("aspect_cxbot.aspect_recordingdirectory") ? (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_recordingdirectory"]).Value) : string.Empty, CXStepId = cxStep.Id.ToString(), CXStepAudio = cxStep.Contains("aspect_audio") ? (string)cxStep["aspect_audio"] : string.Empty, CXText = responseText, CXAnswers = CXGetAnswers(service, cxStep), CXType = Dynamics.CXGetType((OptionSetValue)cxStep["aspect_type"]) }); ResourceResponse msgResponse = connector.Conversations.ReplyToActivity(responseMessage); //update conversation to reflect the current step and initiate any workflow triggers Microsoft.Xrm.Sdk.Entity upsConversation2 = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversation", "aspect_conversationid", context.Conversation.Id); upsConversation2["aspect_currentcxstepid"] = new EntityReference(cxCurrentStep.LogicalName, cxCurrentStep.Id); upsConversation2["aspect_nextcxstepid"] = cxStep.Contains("aspect_nextcxstepid") ? (EntityReference)cxStep["aspect_nextcxstepid"] : null; service.Execute(new UpsertRequest() { Target = upsConversation2 }); return(cxStep.Contains("aspect_nextcxstepid") ? (EntityReference)cxStep["aspect_nextcxstepid"] : null); }
public static async Task <Microsoft.Xrm.Sdk.Entity> PromptNextQuestion(IDialogContext context, IOrganizationService service, Microsoft.Xrm.Sdk.Entity cxCurrentStep, Microsoft.Xrm.Sdk.Entity cxNextStep, IMessageActivity message) { Microsoft.Xrm.Sdk.Entity cxStep = service.Retrieve(cxNextStep.LogicalName, cxNextStep.Id, new ColumnSet(true)); //update conversation to initiate any workflow triggers Microsoft.Xrm.Sdk.Entity upsConversation = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversation", "aspect_conversationid", message.Conversation.Id); upsConversation["aspect_nextcxstepid"] = new EntityReference(cxNextStep.LogicalName, cxNextStep.Id); UpsertResponse upsResponse = (UpsertResponse)service.Execute(new UpsertRequest() { Target = upsConversation }); string responseText = ReplaceOutputText(service, (EntityReference)upsResponse["Target"], (string)cxStep["aspect_message"]); //Create a conversation message with outbound message Microsoft.Xrm.Sdk.Entity conversationMessageOutbound = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversationmessage"); conversationMessageOutbound["aspect_cxconversationid"] = new EntityReference("aspect_cxconversation", "aspect_conversationid", message.Conversation.Id); conversationMessageOutbound["aspect_cxstepid"] = new EntityReference(cxNextStep.LogicalName, cxNextStep.Id); conversationMessageOutbound["aspect_direction"] = true; conversationMessageOutbound["aspect_name"] = message.Id; conversationMessageOutbound["aspect_message"] = responseText; service.Create(conversationMessageOutbound); //Update conversation to reflect the current step Microsoft.Xrm.Sdk.Entity upsConversation2 = new Microsoft.Xrm.Sdk.Entity("aspect_cxconversation", "aspect_conversationid", message.Conversation.Id); upsConversation2["aspect_currentcxstepid"] = new EntityReference(cxNextStep.LogicalName, cxNextStep.Id); upsConversation2["aspect_lastanswer"] = message.Text; upsConversation2["aspect_nextcxstepid"] = null; service.Execute(new UpsertRequest() { Target = upsConversation2 }); //send outbound message IMessageActivity responseMessage = context.MakeMessage(); responseMessage.Text = responseText; responseMessage.Speak = responseText; responseMessage.InputHint = Dynamics.CXGetAnswers(service, cxStep); responseMessage.Value = JsonConvert.SerializeObject(new Dynamics.CXInformation() { //CXBotId = ((Guid)(((AliasedValue)["aspect_cxbot.aspect_cxbotid"]).Value)).ToString(), CXBotName = (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_name"]).Value), AudioDirectory = cxCurrentStep.Contains("aspect_cxbot.aspect_audiodirectory") ? (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_audiodirectory"]).Value) : string.Empty, RecordingDirectory = cxCurrentStep.Contains("aspect_cxbot.aspect_recordingdirectory") ? (string)(((AliasedValue)cxCurrentStep["aspect_cxbot.aspect_recordingdirectory"]).Value) : string.Empty, CXStepId = cxStep.Id.ToString(), CXStepAudio = cxStep.Contains("aspect_audio") ? (string)cxStep["aspect_audio"] : string.Empty, CXText = responseText, CXAnswers = CXGetAnswers(service, cxStep), CXType = Dynamics.CXGetType((OptionSetValue)cxStep["aspect_type"]) }); await context.PostAsync(responseMessage); return(cxNextStep); }