Skip to content

Commit

Permalink
Merge pull request #1326 from Microsoft/kaiqb/suggestedActions
Browse files Browse the repository at this point in the history
Updated SuggestedActions, AdaptiveCards, and Welcome User sample
  • Loading branch information
Kaiqb authored Mar 13, 2019
2 parents 2bbe832 + 533e8f7 commit bf99a66
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 464 deletions.

This file was deleted.

8 changes: 8 additions & 0 deletions samples/csharp_dotnetcore/03.welcome-user/WelcomeUser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CodeAnalysisRuleSet />
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CodeAnalysisRuleSet />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
Expand Down
22 changes: 0 additions & 22 deletions samples/csharp_dotnetcore/03.welcome-user/samples.ruleset

This file was deleted.

15 changes: 0 additions & 15 deletions samples/csharp_dotnetcore/03.welcome-user/welcome-user.bot

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ public class AdaptiveCardsBot : ActivityHandler
Path.Combine(".", "Resources", "SolitaireCard.json"),
};

protected override async Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
if (turnContext.Activity.MembersAdded != null)
{
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Logging;

namespace Microsoft.BotBuilderSamples
{
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
public AdapterWithErrorHandler(ICredentialProvider credentialProvider, ILogger<BotFrameworkHttpAdapter> logger)
: base(credentialProvider)
{
// Enable logging at the adapter level using OnTurnError.
OnTurnError = async (turnContext, exception) =>
{
logger.LogError($"Exception caught : {exception}");
await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");
await turnContext.SendActivityAsync("To run this sample make sure you have the QnA model deployed.");
};
}
}
}
23 changes: 0 additions & 23 deletions samples/csharp_dotnetcore/08.suggested-actions/BotBuilder.ruleset

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;

namespace Microsoft.BotBuilderSamples
{
// This bot will respond to the user's input with suggested actions.
// Suggested actions enable your bot to present buttons that the user
// can tap to provide input.
public class SuggestedActionsBot : ActivityHandler
{
public const string WelcomeText = @"This bot will introduce you to suggestedActions.
Please answer the question:";


protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
// Send a welcome message to the user and tell them what actions they may perform to use this bot
await SendWelcomeMessageAsync(turnContext, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{

// Extract the text from the message activity the user sent.
var text = turnContext.Activity.Text.ToLowerInvariant();

// Take the input from the user and create the appropriate response.
var responseText = ProcessInput(text);

// Respond to the user.
await turnContext.SendActivityAsync(responseText, cancellationToken: cancellationToken);

await SendSuggestedActionsAsync(turnContext, cancellationToken);
}
private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
foreach (var member in turnContext.Activity.MembersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(
$"Welcome to SuggestedActionsBot {member.Name}. {WelcomeText}",
cancellationToken: cancellationToken);
await SendSuggestedActionsAsync(turnContext, cancellationToken);
}
}
}

private static string ProcessInput(string text)
{
const string colorText = "is the best color, I agree.";
switch (text)
{
case "red":
{
return $"Red {colorText}";
}

case "yellow":
{
return $"Yellow {colorText}";
}

case "blue":
{
return $"Blue {colorText}";
}

default:
{
return "Please select a color from the suggested action choices";
}
}
}

// Creates and sends an activity with suggested actions to the user. When the user
/// clicks one of the buttons the text value from the "CardAction" will be
/// displayed in the channel just as if the user entered the text. There are multiple
/// "ActionTypes" that may be used for different situations.
private static async Task SendSuggestedActionsAsync(ITurnContext turnContext, CancellationToken cancellationToken)
{
var reply = turnContext.Activity.CreateReply("What is your favorite color?");
reply.SuggestedActions = new SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction() { Title = "Red", Type = ActionTypes.ImBack, Value = "Red" },
new CardAction() { Title = "Yellow", Type = ActionTypes.ImBack, Value = "Yellow" },
new CardAction() { Title = "Blue", Type = ActionTypes.ImBack, Value = "Blue" },
},
};
await turnContext.SendActivityAsync(reply, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.Bot.Connector.Authentication;
using Microsoft.Extensions.Configuration;

namespace Microsoft.BotBuilderSamples
{
public class ConfigurationCredentialProvider : SimpleCredentialProvider
{
public ConfigurationCredentialProvider(IConfiguration configuration)
: base(configuration["MicrosoftAppId"], configuration["MicrosoftAppPassword"])
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;

namespace Microsoft.BotBuilderSamples
{
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
// achieved by specifying a more specific type for the bot constructor argument.
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;

public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
_adapter = adapter;
_bot = bot;
}

[HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await _adapter.ProcessAsync(Request, Response, _bot);
}
}
}

This file was deleted.

33 changes: 6 additions & 27 deletions samples/csharp_dotnetcore/08.suggested-actions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,20 @@

namespace Microsoft.BotBuilderSamples
{
/// <summary>
/// This is an ASP.NET Core app that creates a webserver.
/// </summary>
public class Program
{
/// <summary>
/// This is the entry point for your application.
/// It is run first once your application is started.
/// This method creates a web server.
/// </summary>
/// <param name="args">Arguments for this method.</param>
public static void Main(string[] args)
{
BuildWebHost(args).Run();
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHost BuildWebHost(string[] args) =>
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
.ConfigureLogging((logging) =>
{
// Add Azure Logging
logging.AddAzureWebAppDiagnostics();

// Logging Options.
// There are other logging options available:
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-2.1
// logging.AddDebug();
// logging.AddConsole();
logging.AddDebug();
logging.AddConsole();
})

// Logging Options.
// Consider using Application Insights for your logging and metrics needs.
// https://azure.microsoft.com/en-us/services/application-insights/
// .UseApplicationInsights()
.UseStartup<Startup>()
.Build();
.UseStartup<Startup>();
}
}
Loading

0 comments on commit bf99a66

Please sign in to comment.