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 Teams Handler for task/fetch and submit | TaskModule scenario #1242

Merged
merged 1 commit into from
Oct 2, 2019
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
12 changes: 8 additions & 4 deletions libraries/botbuilder/src/teamsActivityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
TaskModuleTaskInfo,
TaskModuleRequest,
TaskModuleResponse,
TaskModuleResponseBase,
TeamsChannelData,
TeamInfo,
TurnContext
Expand Down Expand Up @@ -98,11 +99,14 @@ export class TeamsActivityHandler extends ActivityHandler {

case 'task/fetch':
const fetchResponse = await this.onTeamsTaskModuleFetch(context, context.activity.value);
return TeamsActivityHandler.createInvokeResponse(fetchResponse);
const taskModuleContineResponse = { type: 'continue', value: fetchResponse };
const taskModuleResponse = { task: taskModuleContineResponse };
return TeamsActivityHandler.createInvokeResponse(taskModuleResponse);

case 'task/submit':
const submitResponse = await this.onTeamsTaskModuleSubmit(context, context.activity.value);
return TeamsActivityHandler.createInvokeResponse(submitResponse);
const submitResponseBase = await this.onTeamsTaskModuleSubmit(context, context.activity.value);
const taskModuleResponse_submit = { task: submitResponseBase };
return TeamsActivityHandler.createInvokeResponse(taskModuleResponse_submit);

default:
throw new Error('NotImplemented');
Expand Down Expand Up @@ -210,7 +214,7 @@ export class TeamsActivityHandler extends ActivityHandler {
* @param context
* @param taskModuleRequest
*/
protected async onTeamsTaskModuleSubmit(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponse | void> {
protected async onTeamsTaskModuleSubmit(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponseBase | void> {
throw new Error('NotImplemented');
}

Expand Down
2 changes: 2 additions & 0 deletions libraries/botbuilder/tests/teams/taskModule/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MicrosoftAppId=
MicrosoftAppPassword=
30 changes: 30 additions & 0 deletions libraries/botbuilder/tests/teams/taskModule/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "task-module",
"version": "1.0.0",
"description": "",
"main": "./lib/index.js",
"scripts": {
"start": "tsc --build && node ./lib/index.js",
"build": "tsc --build",
"watch": "nodemon --watch ./src -e ts --exec \"npm run start\""
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"botbuilder": "file:../../../",
"dotenv": "^8.1.0",
"node-fetch": "^2.6.0",
"restify": "^8.4.0",
"uuid": "^3.3.3"
},
"devDependencies": {
"@types/node": "^12.7.1",
"@types/node-fetch": "^2.5.0",
"@types/request": "^2.48.1",
"@types/restify": "^7.2.7",
"nodemon": "^1.19.1",
"ts-node": "^7.0.1",
"typescript": "^3.2.4"
}
}
53 changes: 53 additions & 0 deletions libraries/botbuilder/tests/teams/taskModule/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { config } from 'dotenv';
import * as path from 'path';
import * as restify from 'restify';

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
import { BotFrameworkAdapter } from 'botbuilder';

// This bot's main dialog.
import { TaskModuleBot } from './taskModuleBot';

const ENV_FILE = path.join(__dirname, '..', '.env');
config({ path: ENV_FILE });

// Create HTTP server.
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log(`\n${server.name} listening to ${server.url}`);
console.log(`\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator`);
console.log(`\nTo test your bot, see: https://aka.ms/debug-with-emulator`);
});

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new BotFrameworkAdapter({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword
});

// adapter.use(new TranscriptLoggerMiddleware(new FileTranscriptStore('./transcripts')));

// Catch-all for errors.
adapter.onTurnError = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
console.error('[onTurnError]:');
console.error(error);
// Send a message to the user
await context.sendActivity(`Oops. Something went wrong in the bot!\n ${error.message}`);
};

// Create the main dialog.
const myBot = new TaskModuleBot();

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route to main dialog.
await myBot.run(context);
});
});
82 changes: 82 additions & 0 deletions libraries/botbuilder/tests/teams/taskModule/src/taskModuleBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {
TeamsActivityHandler,
} from 'botbuilder';
import {
Attachment,
CardFactory,
MessageFactory,
TaskModuleMessageResponse,
TaskModuleRequest,
TaskModuleResponseBase,
TaskModuleTaskInfo,
TurnContext,
} from 'botbuilder-core';

export class TaskModuleBot extends TeamsActivityHandler {
constructor() {
super();

// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
this.onMessage(async (context, next) => {
const card = this.GetTaskModuleHeroCard();
const message = MessageFactory.attachment(card);
await context.sendActivity(message);
await next();
});
}

protected async onTeamsTaskModuleFetch(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleTaskInfo> {
var reply = MessageFactory.text("OnTeamsTaskModuleFetchAsync TaskModuleRequest" + JSON.stringify(taskModuleRequest));
await context.sendActivity(reply);
return {
card: this.GetTaskModuleAdaptiveCard(),
height: 200,
width: 400,
title: "Adaptive Card: Inputs",
};
}
// TaskModuleResponseBase: type:
protected async onTeamsTaskModuleSubmit(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponseBase | void> {
var reply = MessageFactory.text("OnTeamsTaskModuleFetchAsync Value: " + JSON.stringify(taskModuleRequest));
await context.sendActivity(reply);
var response : TaskModuleMessageResponse = { type: "message", value: "Hello", };
return <TaskModuleResponseBase> response ;
}

private GetTaskModuleHeroCard() : Attachment {
return CardFactory.heroCard("Task Module Invocation from Hero Card",
"This is a hero card with a Task Module Action button. Click the button to show an Adaptive Card within a Task Module.",
null, // No images
[{type: "invoke", title:"Adaptive Card", value: {type:"task/fetch", data:"adaptivecard"} }]
);
}

private GetTaskModuleAdaptiveCard(): Attachment {
return CardFactory.adaptiveCard({
version: '1.0.0',
type: 'AdaptiveCard',
body: [
{
type: 'TextBlock',
text: `Enter Text Here`,
},
{
type: 'Input.Text',
id: 'usertext',
placeholder: 'add some text and submit',
IsMultiline: true,
}
],
actions: [
{
type: 'Action.Submit',
title: 'Submit',
}
]
});
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json",
"manifestVersion": "1.5",
"version": "1.0.0",
"id": "<<YOUR_GENERATED_APP_GUID>>",
"packageName": "com.teams.microsoft.bot.test",
"developer": {
"name": "Bot Framework Team",
"websiteUrl": "https://dev.botframework.com",
"privacyUrl": "https://dev.botframework.com",
"termsOfUseUrl": "https://dev.botframework.com"
},
"icons": {
"color": "color.png",
"outline": "outline.png"
},
"name": {
"short": "Task Module",
"full": "Simple Task Module"
},
"description": {
"short": "Test Task Module Scenario",
"full": "Simple Task Module Scenario Test"
},
"accentColor": "#FFFFFF",
"bots": [
{
"botId": "<<YOUR_BOTS_MSA_APP_ID>>",
"scopes": [
"personal",
"team",
"groupchat"
],
"supportsFiles": false,
"isNotificationOnly": false
}
],
"permissions": [
"identity",
"messageTeamMembers"
],
"validDomains": [
"YourBotWebApp.azurewebsites.net"
]
}
11 changes: 11 additions & 0 deletions libraries/botbuilder/tests/teams/taskModule/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"composite": true,
"declaration": true,
"sourceMap": true,
"outDir": "./lib",
"rootDir": "./src",
}
}