Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello World Sample (CSharp): Coding standards changes #1485

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.6.2
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
// <copyright file="AdapterWithErrorHandler.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>

// Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.6.2
namespace Microsoft.Teams.Samples.HelloWorld.Web
{
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using Microsoft.Bot.Builder.TraceExtensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

/// <summary>
/// A custom BotFrameworkHttpAdapter with error handling capabilities.
/// </summary>
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
/// <summary>
/// Initializes a new instance of the <see cref="AdapterWithErrorHandler"/> class.
/// </summary>
/// <param name="configuration">The configuration for the bot.</param>
/// <param name="logger">The logger to use for logging errors.</param>
public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger)
: base(configuration, logger)
{
OnTurnError = async (turnContext, exception) =>
this.OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
logger.LogError(exception, "[OnTurnError] unhandled error : {Message}", exception.Message);

// Uncomment below commented line for local debugging.
// await turnContext.SendActivityAsync($"Sorry, it looks like something went wrong. Exception Caught: {exception.Message}");
Expand All @@ -27,4 +36,4 @@ public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFramewor
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
using System.Linq;
using System;
using System.Collections.Generic;
using Bogus;
// <copyright file="MessageExtension.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>

namespace Microsoft.Teams.Samples.HelloWorld.Web
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Bogus;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;

/// <summary>
/// A bot that handles messaging extensions for Microsoft Teams.
/// </summary>
public class MessageExtension : TeamsActivityHandler
{
/// <summary>
/// Handles incoming message activities.
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A task that represents the work queued to execute.</returns>
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
turnContext.Activity.RemoveRecipientMention();
Expand All @@ -23,9 +36,17 @@ protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivi
await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
}

/// <summary>
/// Handles messaging extension queries.
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="query">The query object for the messaging extension.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A task that represents the work queued to execute.</returns>
/// <exception cref="NotImplementedException">Thrown when the command ID is invalid.</exception>
protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionQueryAsync(ITurnContext<IInvokeActivity> turnContext, MessagingExtensionQuery query, CancellationToken cancellationToken)
{
var title = "";
var title = string.Empty;
var titleParam = query.Parameters?.FirstOrDefault(p => p.Name == "cardTitle");
if (titleParam != null)
{
Expand All @@ -51,41 +72,54 @@ protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionQue
{
AttachmentLayout = "list",
Type = "result",
Attachments = attachments.ToList()
Attachments = attachments.ToList(),
},
};
return Task.FromResult(result);
}

/// <summary>
/// Handles the selection of an item in a messaging extension.
/// </summary>
/// <param name="turnContext">The context object for this turn.</param>
/// <param name="query">The query object for the messaging extension.</param>
/// <param name="cancellationToken">A cancellation token for the task.</param>
/// <returns>A task that represents the work queued to execute.</returns>
protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSelectItemAsync(ITurnContext<IInvokeActivity> turnContext, JObject query, CancellationToken cancellationToken)
{
return Task.FromResult(new MessagingExtensionResponse
{
ComposeExtension = new MessagingExtensionResult
{
AttachmentLayout = "list",
Type = "result",
Attachments = new MessagingExtensionAttachment[]
{
new ThumbnailCard()
.ToAttachment()
.ToMessagingExtensionAttachment(),
},
},
});
}

/// <summary>
/// Creates a messaging extension attachment with a thumbnail card.
/// </summary>
/// <param name="title">The title for the card.</param>
/// <returns>A messaging extension attachment.</returns>
private static MessagingExtensionAttachment GetAttachment(string title)
{
var card = new ThumbnailCard
{
Title = !string.IsNullOrWhiteSpace(title) ? title : new Faker().Lorem.Sentence(),
Text = new Faker().Lorem.Paragraph(),
Images = new List<CardImage> { new CardImage("http://lorempixel.com/640/480?rand=" + DateTime.Now.Ticks.ToString()) }
Images = new List<CardImage> { new CardImage("http://lorempixel.com/640/480?rand=" + DateTime.Now.Ticks.ToString()) },
};

return card
.ToAttachment()
.ToMessagingExtensionAttachment();
}
protected override Task<MessagingExtensionResponse> OnTeamsMessagingExtensionSelectItemAsync(ITurnContext<IInvokeActivity> turnContext, JObject query, CancellationToken cancellationToken)
{
return Task.FromResult(new MessagingExtensionResponse
{
ComposeExtension = new MessagingExtensionResult
{
AttachmentLayout = "list",
Type = "result",
Attachments = new MessagingExtensionAttachment[]{
new ThumbnailCard()
.ToAttachment()
.ToMessagingExtensionAttachment()
}
},
});
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
// <copyright file="BotController.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
// </copyright>

// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.6.2
namespace Microsoft.Teams.Samples.HelloWorld.Web.Controllers
{
// 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.
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;

/// <summary>
/// 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.
/// </summary>
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter Adapter;
private readonly IBot Bot;
private readonly IBot bot;
private readonly IBotFrameworkHttpAdapter adapter;

/// <summary>
/// Initializes a new instance of the <see cref="BotController"/> class.
/// </summary>
/// <param name="adapter">The bot framework HTTP adapter.</param>
/// <param name="bot">The bot instance.</param>
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
Adapter = adapter;
Bot = bot;
this.adapter = adapter;
this.bot = bot;
}

/// <summary>
/// Handles the HTTP POST request.
/// </summary>
/// <returns>A task that represents the work queued to execute.</returns>
[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);
await this.adapter.ProcessAsync(this.Request, this.Response, this.bot);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
// <copyright file="HomeController.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>

namespace Microsoft.Teams.Samples.HelloWorld.Web.Controllers
{
using Microsoft.AspNetCore.Mvc;

/// <summary>
/// HomeController class to handle web requests for the home page and other routes.
/// </summary>
[Route("")]
public class HomeController : Controller
{
/// <summary>
/// Handles the default route and returns the Index view.
/// </summary>
/// <returns>The Index view.</returns>
[Route("")]
public ActionResult Index()
{
return View();
return this.View();
}

/// <summary>
/// Handles the /hello route and returns the Index view.
/// </summary>
/// <returns>The Index view.</returns>
[Route("hello")]
public ActionResult Hello()
{
return View("Index");
return this.View("Index");
}

/// <summary>
/// Handles the /first route and returns the First view.
/// </summary>
/// <returns>The First view.</returns>
[Route("first")]
public ActionResult First()
{
return View();
return this.View();
}

/// <summary>
/// Handles the /second route and returns the Second view.
/// </summary>
/// <returns>The Second view.</returns>
[Route("second")]
public ActionResult Second()
{
return View();
return this.View();
}

/// <summary>
/// Handles the /configure route and returns the Configure view.
/// </summary>
/// <returns>The Configure view.</returns>
[Route("configure")]
public ActionResult Configure()
{
return View();
return this.View();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// <copyright file="GlobalSuppressions.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Major Code Smell", "S125:Sections of code should not be commented out", Justification = "This code needs to be uncommented for local debugging", Scope = "member", Target = "~M:Microsoft.Teams.Samples.HelloWorld.Web.AdapterWithErrorHandler.#ctor(Microsoft.Extensions.Configuration.IConfiguration,Microsoft.Extensions.Logging.ILogger{Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter})")]
[assembly: SuppressMessage("Minor Code Smell", "S1075:URIs should not be hardcoded", Justification = "This URL will not change later", Scope = "member", Target = "~M:Microsoft.Teams.Samples.HelloWorld.Web.MessageExtension.GetAttachment(System.String)~Microsoft.Bot.Schema.Teams.MessagingExtensionAttachment")]
Loading