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

fix: upgrade workflow bot template based on Teams store policy #12119

Merged
merged 2 commits into from
Aug 1, 2024
Merged
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
Expand Up @@ -13,7 +13,7 @@ namespace {{SafeProjectName}}.Commands

public IEnumerable<ITriggerPattern> TriggerPatterns => new List<ITriggerPattern>
{
// Used to trigger the command handler if the command text contains 'helloWorld'
// Used to trigger the command handler when the user enters a generic or unknown command
new RegExpTrigger("^.+$")
};

Expand Down
58 changes: 58 additions & 0 deletions templates/csharp/workflow/Commands/GenericCommandHandler.cs.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.Bot.Builder;
using Microsoft.TeamsFx.Conversation;

namespace {{SafeProjectName}}.Commands
{
/// <summary>
/// The <see cref="GenericCommandHandler"/> registers patterns with the <see cref="ITeamsCommandHandler"/> and
/// responds with appropriate messages if the user types general command inputs, such as "hi", "hello", and "help".
/// </summary>
public class GenericCommandHandler : ITeamsCommandHandler
{
private readonly ILogger<GenericCommandHandler> _logger;

public IEnumerable<ITriggerPattern> TriggerPatterns => new List<ITriggerPattern>
{
// Used to trigger the command handler when the user enters a generic or unknown command
new RegExpTrigger("^.+$")
};

public GenericCommandHandler(ILogger<GenericCommandHandler> logger)
{
_logger = logger;
}

public async Task<ICommandResponse> HandleCommandAsync(ITurnContext turnContext, CommandMessage message, CancellationToken cancellationToken = default)
{
_logger?.LogInformation($"App received message: {message.Text}");

// Determine the appropriate response based on the command
string responseText;
switch (message.Text)
{
case "hi":
responseText = "Hi there! I'm your Workflow Bot, here to assist you with your tasks. Type 'help' for a list of available commands.";
break;
case "hello":
responseText = "Hello! I'm your Workflow Bot, always ready to help you out. If you need assistance, just type 'help' to see the available commands.";
break;
case "help":
responseText = "Here's a list of commands I can help you with:\n" +
"- 'hi' or 'hello': Say hi or hello to me, and I'll greet you back.\n" +
"- 'help': Get a list of available commands.\n" +
"- 'helloworld': See a sample workflow from me.\n" +
"\nFeel free to ask for help anytime you need it!";
break;
default:
responseText = "Sorry, command unknown. Please type 'help' to see the list of available commands.";
break;
}

// Build the response activity
var activity = MessageFactory.Text(responseText);

// Send response
return new ActivityCommandResponse(activity);
}
}
}
6 changes: 5 additions & 1 deletion templates/csharp/workflow/Program.cs.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ builder.Services.AddSingleton<BotAdapter>(sp => sp.GetService<CloudAdapter>());

// Create command handlers and the Conversation with command-response feature enabled.
builder.Services.AddSingleton<HelloWorldCommandHandler>();
builder.Services.AddSingleton<GenericCommandHandler>();
builder.Services.AddSingleton<DoStuffActionHandler>();
builder.Services.AddSingleton(sp =>
{
Expand All @@ -39,7 +40,10 @@ builder.Services.AddSingleton(sp =>
Adapter = sp.GetService<CloudAdapter>(),
Command = new CommandOptions()
{
Commands = new List<ITeamsCommandHandler> { sp.GetService<HelloWorldCommandHandler>() }
Commands = new List<ITeamsCommandHandler> {
sp.GetService<HelloWorldCommandHandler>(),
sp.GetService<GenericCommandHandler>()
}
},
CardAction = new CardActionOptions()
{
Expand Down
21 changes: 18 additions & 3 deletions templates/csharp/workflow/TeamsBot.cs.tpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Schema;

namespace {{SafeProjectName}}
{
/// <summary>
/// An empty bot handler.
/// Bot handler.
/// You can add your customization code here to extend your bot logic if needed.
/// </summary>
public class TeamsBot : TeamsActivityHandler
{
public override Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default) =>
Task.CompletedTask;
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
await base.OnTurnAsync(turnContext, cancellationToken);
}

protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
var welcomeText = "Welcome to the Workflow Bot! I can help you work through the Adaptive Card and perform various tasks. Type \"helloworld\" or \"help\" to get started.";
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText), cancellationToken);
}
}
}
}
}
35 changes: 35 additions & 0 deletions templates/js/workflow/src/commands/genericCommandHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class GenericCommandHandler {
triggerPatterns = new RegExp(/^.+$/);

async handleCommandReceived(context, message) {
// verify the command arguments which are received from the client if needed.
console.log(`App received message: ${message.text}`);

let response = "";
switch (message.text) {
case "hi":
response =
"Hi there! I'm your Workflow Bot, here to assist you with your tasks. Type 'help' for a list of available commands.";
break;
case "hello":
response =
"Hello! I'm your Workflow Bot, always ready to help you out. If you need assistance, just type 'help' to see the available commands.";
break;
case "help":
response =
"Here's a list of commands I can help you with:\n" +
"- 'hi' or 'hello': Say hi or hello to me, and I'll greet you back.\n" +
"- 'help': Get a list of available commands.\n" +
"- 'helloworld': See a sample workflow from me.\n" +
"\nFeel free to ask for help anytime you need it!";
break;
default:
response = `Sorry, command unknown. Please type 'help' to see the list of available commands.`;
}
return response;
}
}

module.exports = {
GenericCommandHandler,
};
3 changes: 2 additions & 1 deletion templates/js/workflow/src/internal/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { BotBuilderCloudAdapter } = require("@microsoft/teamsfx");
const ConversationBot = BotBuilderCloudAdapter.ConversationBot;
const { DoStuffActionHandler } = require("../cardActions/doStuffActionHandler");
const { HelloWorldCommandHandler } = require("../commands/helloworldCommandHandler");
const { GenericCommandHandler } = require("../commands/genericCommandHandler");
const config = require("./config");

// Create the conversation bot and register the command and card action handlers for your app.
Expand All @@ -11,7 +12,7 @@ const workflowApp = new ConversationBot({
adapterConfig: config,
command: {
enabled: true,
commands: [new HelloWorldCommandHandler()],
commands: [new HelloWorldCommandHandler(), new GenericCommandHandler()],
},
cardAction: {
enabled: true,
Expand Down
16 changes: 15 additions & 1 deletion templates/js/workflow/src/teamsBot.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
const { TeamsActivityHandler } = require("botbuilder");

// An empty teams activity handler.
// Teams activity handler.
// You can add your customization code here to extend your bot logic if needed.
class TeamsBot extends TeamsActivityHandler {
constructor() {
super();

// Listen to MembersAdded event, view https://docs.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bots-notifications for more events
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id) {
await context.sendActivity(
'Welcome to the Workflow Bot! I can help you work through the Adaptive Card and perform various tasks. Type "helloworld" or "help" to get started.'
);
break;
}
}
await next();
});
}
}

Expand Down
41 changes: 41 additions & 0 deletions templates/ts/workflow/src/commands/genericCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Activity, TurnContext } from "botbuilder";
import { CommandMessage, TeamsFxBotCommandHandler, TriggerPatterns } from "@microsoft/teamsfx";

/**
* The `GenericCommandHandler` registers patterns with the `TeamsFxBotCommandHandler` and responds
* with appropriate messages if the user types general command inputs, such as "hi", "hello", and "help".
*/
export class GenericCommandHandler implements TeamsFxBotCommandHandler {
triggerPatterns: TriggerPatterns = new RegExp(/^.+$/);

async handleCommandReceived(
context: TurnContext,
message: CommandMessage
): Promise<string | Partial<Activity> | void> {
console.log(`App received message: ${message.text}`);

let response = "";
switch (message.text) {
case "hi":
response =
"Hi there! I'm your Workflow Bot, here to assist you with your tasks. Type 'help' for a list of available commands.";
break;
case "hello":
response =
"Hello! I'm your Workflow Bot, always ready to help you out. If you need assistance, just type 'help' to see the available commands.";
break;
case "help":
response =
"Here's a list of commands I can help you with:\n" +
"- 'hi' or 'hello': Say hi or hello to me, and I'll greet you back.\n" +
"- 'help': Get a list of available commands.\n" +
"- 'helloworld': See a sample workflow from me.\n" +
"\nFeel free to ask for help anytime you need it!";
break;
default:
response = `Sorry, command unknown. Please type 'help' to see the list of available commands.`;
}

return response;
}
}
3 changes: 2 additions & 1 deletion templates/ts/workflow/src/internal/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DoStuffActionHandler } from "../cardActions/doStuffActionHandler";
import { HelloWorldCommandHandler } from "../commands/helloworldCommandHandler";
import { GenericCommandHandler } from "../commands/genericCommandHandler";
import { BotBuilderCloudAdapter } from "@microsoft/teamsfx";
import ConversationBot = BotBuilderCloudAdapter.ConversationBot;
import config from "./config";
Expand All @@ -11,7 +12,7 @@ export const workflowApp = new ConversationBot({
adapterConfig: config,
command: {
enabled: true,
commands: [new HelloWorldCommandHandler()],
commands: [new HelloWorldCommandHandler(), new GenericCommandHandler()],
},
cardAction: {
enabled: true,
Expand Down
16 changes: 15 additions & 1 deletion templates/ts/workflow/src/teamsBot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import { TeamsActivityHandler } from "botbuilder";

// An empty teams activity handler.
// Teams activity handler.
// You can add your customization code here to extend your bot logic if needed.
export class TeamsBot extends TeamsActivityHandler {
constructor() {
super();

// Listen to MembersAdded event, view https://docs.microsoft.com/en-us/microsoftteams/platform/resources/bot-v3/bots-notifications for more events
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id) {
await context.sendActivity(
'Welcome to the Workflow Bot! I can help you work through the Adaptive Card and perform various tasks. Type "helloworld" or "help" to get started.'
);
break;
}
}
await next();
});
}
}
Loading