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

adaptivecards bot #1251

Merged
merged 10 commits into from
Oct 4, 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
2 changes: 2 additions & 0 deletions libraries/botbuilder/tests/teams/adaptiveCards/.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/adaptiveCards/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "adaptive-cards-bot",
"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"
}
}
234 changes: 234 additions & 0 deletions libraries/botbuilder/tests/teams/adaptiveCards/src/adaptiveCardsBot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import {
Activity,
ActionTypes,
Attachment,
CardFactory,
InvokeResponse,
MessageFactory,
TaskModuleRequest,
TaskModuleResponseBase,
TaskModuleMessageResponse,
TaskModuleTaskInfo,
TeamsActivityHandler,
TurnContext
} from 'botbuilder';

//
// You can @mention the bot the text "1", "2", or "3". "1" will send back adaptive cards. "2" will send back a
// task module that contains an adpative card. "3" will return an adpative card that contains BF card actions.
//
export class AdaptiveCardsBot 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) => {
TurnContext.removeRecipientMention(context.activity);
let text = context.activity.text;
if (text && text.length > 0) {
text = text.trim();
if (text == '1')
{
await this.sendAdaptiveCard1(context);
}
else if (text == '2')
{
await this.sendAdaptiveCard2(context);
}
else if (text == '3')
{
await this.sendAdaptiveCard3(context);
}
else
{
await context.sendActivity(`You said: ${text}`);
}
}
else {
await context.sendActivity('App sent a message with empty text');
const activityValue = context.activity.value;
if (activityValue) {
await context.sendActivity(`but with value ${JSON.stringify(activityValue)}`);
}
}
await next();
});

this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (const member of membersAdded) {
if (member.id !== context.activity.recipient.id) {
await context.sendActivity('Hello and welcome!');
}
}

// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

protected async onTeamsTaskModuleFetch(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleTaskInfo> {
await context.sendActivity(MessageFactory.text(`OnTeamsTaskModuleFetchAsync TaskModuleRequest: ${JSON.stringify(taskModuleRequest)}`));

const card = CardFactory.adaptiveCard({
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "This is an Adaptive Card within a Task Module"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Action.Submit",
"data": {
"submitLocation": "taskModule"
}
}
]
});

return <TaskModuleTaskInfo> {
card: card,
height: 200,
width: 400,
title: 'Task Module Example'
};
}

protected async onTeamsTaskModuleSubmit(context: TurnContext, taskModuleRequest: TaskModuleRequest): Promise<TaskModuleResponseBase> {
await context.sendActivity(MessageFactory.text(`OnTeamsTaskModuleSubmit value: ${ JSON.stringify(taskModuleRequest) }`));
return <TaskModuleMessageResponse>{ type: 'message', value: 'Thanks!' };
}

protected async onTeamsCardActionInvoke(context: TurnContext): Promise<InvokeResponse> {
await context.sendActivity(MessageFactory.text(`OnTeamsCardActionInvoke value: ${JSON.stringify(context.activity.value)}`));
return <InvokeResponse>{ status: 200 };
}

private async sendAdaptiveCard1(context: TurnContext): Promise<void> {
const card = CardFactory.adaptiveCard({
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Bot Builder actions"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "imBack",
"data": {
"msteams": {
"type": "imBack",
"value": "text"
}
}
},
{
"type": "Action.Submit",
"title": "message back",
"data": {
"msteams": {
"type": "messageBack",
"value": { "key": "value" }
}
}
},
{
"type": "Action.Submit",
"title": "message back local echo",
"data": {
"msteams": {
"type": "messageBack",
"text": "text received by bots",
"displayText": "display text message back",
"value": { "key": "value" }
}
},
},
{
"type": "Action.Submit",
"title": "invoke",
"data": {
"msteams": {
"type": "invoke",
"value": { "key": "value" }
}
}
}
]
});

await context.sendActivity(MessageFactory.attachment(card));
}

private async sendAdaptiveCard2(context: TurnContext): Promise<void> {
const card = CardFactory.adaptiveCard({
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Task Module Adaptive Card"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Launch Task Module",
"data": {
"msteams": {
"type": "invoke",
"value": {
"hiddenKey": "hidden value from task module launcher",
"type": "task/fetch"
}
}
}
}
]
});

await context.sendActivity(MessageFactory.attachment(card));
}

private async sendAdaptiveCard3(context: TurnContext): Promise<void> {
const card = CardFactory.adaptiveCard({
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Bot Builder actions"
},
{
"type": "Input.Text",
"id": "x"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Action.Submit",
"data": {
"key": "value"
}
}
]
});

await context.sendActivity(MessageFactory.attachment(card));
}
}
53 changes: 53 additions & 0 deletions libraries/botbuilder/tests/teams/adaptiveCards/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 { AdaptiveCardsBot } from './adaptiveCardsBot';

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 AdaptiveCardsBot();

// Listen for incoming requests.
server.post('/api/messages', (req, res) => {
adapter.processActivity(req, res, async (context) => {
// Route to main dialog.
await myBot.run(context);
});
});
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,42 @@
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.3/MicrosoftTeams.schema.json",
"manifestVersion": "1.3",
"version": "1.0.0",
"id": "<<YOUR_BOTS_MSA_APP_ID>>",
"packageName": "com.teams.sample.echobot",
"developer": {
"name": "CardActionsBotJS",
"websiteUrl": "https://www.microsoft.com",
"privacyUrl": "https://www.teams.com/privacy",
"termsOfUseUrl": "https://www.teams.com/termsofuser"
},
"icons": {
"color": "color.png",
"outline": "outline.png"
},
"name": {
"short": "CardActionsBot",
"full": "CardActionsBot"
},
"description": {
"short": "CardActionsBot",
"full": "CardActionsBot"
},
"accentColor": "#FFFFFF",
"bots": [
{
"botId": "<<YOUR_BOTS_MSA_APP_ID>>",
"scopes": [
"groupchat",
"team"
],
"supportsFiles": false,
"isNotificationOnly": false
}
],
"permissions": [
"identity",
"messageTeamMembers"
],
"validDomains": []
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions libraries/botbuilder/tests/teams/adaptiveCards/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",
}
}
2 changes: 1 addition & 1 deletion libraries/botbuilder/tests/teams/fileUpload/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "messaging-extension-action",
"name": "teams-file-bot",
"version": "1.0.0",
"description": "",
"main": "./lib/index.js",
Expand Down
Loading