Пример #1
0
        // 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));
        }
Пример #2
0
 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;
 }