// Starts SkillDialog based on the user's selections private async Task <DialogTurnResult> CallSkillActionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { var selectedSkill = (BotFrameworkSkill)stepContext.Values[_selectedSkillKey]; Activity skillActivity = null; switch (selectedSkill.Id) { case "EchoSkillBot": // Echo skill only handles message activities, send a dummy utterance to get it started. skillActivity = (Activity)Activity.CreateMessageActivity(); skillActivity.Text = "Start echo skill"; break; case "DialogSkillBot": skillActivity = GetDialogSkillBotActivity(((FoundChoice)stepContext.Result).Value); break; default: throw new Exception($"Unknown target skill id: {selectedSkill.Id}."); } // Create the SkillDialogArgs var skillDialogArgs = new SkillDialogArgs { Skill = selectedSkill, Activity = skillActivity }; // We are manually creating the activity to send to the skill, ensure we add the ChannelData and Properties // from the original activity so the skill gets them. // Note: this is not necessary if we are just forwarding the current activity from context. skillDialogArgs.Activity.ChannelData = stepContext.Context.Activity.ChannelData; skillDialogArgs.Activity.Properties = stepContext.Context.Activity.Properties; // Start the skillDialog with the arguments. return(await stepContext.BeginDialogAsync(nameof(SkillDialog), skillDialogArgs, cancellationToken)); }
// Starts SkillDialog based on the user's selection private async Task <DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { // Send a message activity to the skill. if (stepContext.Context.Activity.Text.StartsWith("m:", StringComparison.CurrentCultureIgnoreCase)) { var dialogArgs = new SkillDialogArgs { SkillId = _targetSkillId, ActivityType = ActivityTypes.Message, Text = stepContext.Context.Activity.Text.Substring(2).Trim() }; return(await stepContext.BeginDialogAsync(nameof(SkillDialog), dialogArgs, cancellationToken)); } // Send a message activity to the skill with some artificial parameters in value if (stepContext.Context.Activity.Text.StartsWith("mv:", StringComparison.CurrentCultureIgnoreCase)) { var dialogArgs = new SkillDialogArgs { SkillId = _targetSkillId, ActivityType = ActivityTypes.Message, Text = stepContext.Context.Activity.Text.Substring(3).Trim(), Value = new BookingDetails { Destination = "New York" } }; return(await stepContext.BeginDialogAsync(nameof(SkillDialog), dialogArgs, cancellationToken)); } // Send an event activity to the skill with "OAuthTest" in the name. if (stepContext.Context.Activity.Text.Equals("OAuthTest", StringComparison.CurrentCultureIgnoreCase)) { var dialogArgs = new SkillDialogArgs { SkillId = _targetSkillId, ActivityType = ActivityTypes.Event, Name = "OAuthTest" }; return(await stepContext.BeginDialogAsync(nameof(SkillDialog), dialogArgs, cancellationToken)); } // Send an event activity to the skill with "BookFlight" in the name. if (stepContext.Context.Activity.Text.Equals("BookFlight", StringComparison.CurrentCultureIgnoreCase)) { var dialogArgs = new SkillDialogArgs { SkillId = _targetSkillId, ActivityType = ActivityTypes.Event, Name = "BookFlight" }; return(await stepContext.BeginDialogAsync(nameof(SkillDialog), dialogArgs, cancellationToken)); } // Send an event activity to the skill "BookFlight" in the name and some testing values. if (stepContext.Context.Activity.Text.Equals("BookFlightWithValues", StringComparison.CurrentCultureIgnoreCase)) { var dialogArgs = new SkillDialogArgs { SkillId = _targetSkillId, ActivityType = ActivityTypes.Event, Name = "BookFlight", Value = new BookingDetails { Destination = "New York", Origin = "Seattle" } }; return(await stepContext.BeginDialogAsync(nameof(SkillDialog), dialogArgs, cancellationToken)); } // Send an invoke activity to the skill with "GetWeather" in the name and some testing values. // Note that this operation doesn't use SkillDialog, InvokeActivities are single turn Request/Response. if (stepContext.Context.Activity.Text.Equals("GetWeather", StringComparison.CurrentCultureIgnoreCase)) { var invokeActivity = Activity.CreateInvokeActivity(); invokeActivity.Name = "GetWeather"; invokeActivity.Value = new Location { PostalCode = "11218" }; invokeActivity.ApplyConversationReference(stepContext.Context.Activity.GetConversationReference(), true); // Always save state before forwarding await _conversationState.SaveChangesAsync(stepContext.Context, true, cancellationToken); var skillInfo = _skillsConfig.Skills[_targetSkillId]; var response = await _skillClient.PostActivityAsync(_botId, skillInfo, _skillsConfig.SkillHostEndpoint, (Activity)invokeActivity, cancellationToken); if (!(response.Status >= 200 && response.Status <= 299)) { throw new HttpRequestException($"Error invoking the skill id: \"{skillInfo.Id}\" at \"{skillInfo.SkillEndpoint}\" (status is {response.Status}). \r\n {response.Body}"); } var invokeResult = $"Invoke result: {JsonConvert.SerializeObject(response.Body)}"; await stepContext.Context.SendActivityAsync(MessageFactory.Text(invokeResult, inputHint: InputHints.IgnoringInput), cancellationToken : cancellationToken); return(await stepContext.NextAsync(null, cancellationToken)); } // Catch all for unhandled intents var didntUnderstandMessageText = "Sorry, I didn't get that. Please try asking in a different way."; var didntUnderstandMessage = MessageFactory.Text(didntUnderstandMessageText, didntUnderstandMessageText, InputHints.IgnoringInput); await stepContext.Context.SendActivityAsync(didntUnderstandMessage, cancellationToken); return(await stepContext.NextAsync(null, cancellationToken)); }
private static void ApplyParentActivityProperties(DialogContext dc, Activity skillActivity, SkillDialogArgs dialogArgs) { // Apply conversation reference and common properties from incoming activity before sending. skillActivity.ApplyConversationReference(dc.Context.Activity.GetConversationReference(), true); skillActivity.Value = dialogArgs.Value; skillActivity.ChannelData = dc.Context.Activity.ChannelData; skillActivity.Properties = dc.Context.Activity.Properties; }