-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Teams] Add update and delete bot scenario (#1260)
* add update and delete bot scenario * remove unused line from turnContext.test.js * address feedback
- Loading branch information
Showing
9 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
libraries/botbuilder/tests/teams/activityUpdateAndDelete/.env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
MicrosoftAppId= | ||
MicrosoftAppPassword= |
30 changes: 30 additions & 0 deletions
30
libraries/botbuilder/tests/teams/activityUpdateAndDelete/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "activity-update-delete-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" | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
libraries/botbuilder/tests/teams/activityUpdateAndDelete/src/activityUpdateAndDeleteBot.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { | ||
ActivityHandler, | ||
TurnContext, | ||
ActivityTypes, | ||
} from 'botbuilder'; | ||
|
||
|
||
export class ActivityUpdateAndDeleteBot extends ActivityHandler { | ||
activityIds: string[]; | ||
|
||
/* | ||
* From the UI you can just @mention the bot from any channelwith any string EXCEPT for "delete". If you send the bot "delete" it will delete | ||
* all of the previous bot responses and empty it's internal storage. | ||
*/ | ||
constructor(activityIds: string[]) { | ||
super(); | ||
|
||
this.activityIds = activityIds; | ||
|
||
// 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); | ||
if (context.activity.text == "delete") { | ||
for (const activityId of this.activityIds) { | ||
await context.deleteActivity(activityId); | ||
} | ||
|
||
this.activityIds = []; | ||
} | ||
else { | ||
await this.sendMessageAndLogActivityId(context, `${context.activity.text}`); | ||
|
||
const text = context.activity.text; | ||
for (const id of this.activityIds) { | ||
await context.updateActivity({ id, text, type: ActivityTypes.Message }); | ||
} | ||
} | ||
|
||
// By calling next() you ensure that the next BotHandler is run. | ||
await next(); | ||
}); | ||
} | ||
|
||
async sendMessageAndLogActivityId(context: TurnContext, text: string): Promise<void> { | ||
var resourceResponse = await context.sendActivity({ text }); | ||
await this.activityIds.push(resourceResponse.id); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
libraries/botbuilder/tests/teams/activityUpdateAndDelete/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, MemoryStorage } from 'botbuilder'; | ||
|
||
// This bot's main dialog. | ||
import { ActivityUpdateAndDeleteBot } from './activityUpdateAndDeleteBot'; | ||
|
||
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 | ||
}); | ||
|
||
// 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}`); | ||
}; | ||
|
||
let activityIds: string[] = []; | ||
|
||
// Create the bot. | ||
const myBot = new ActivityUpdateAndDeleteBot(activityIds); | ||
|
||
// Listen for incoming requests. | ||
server.post('/api/messages', (req, res) => { | ||
adapter.processActivity(req, res, async (context) => { | ||
// Route to bot | ||
await myBot.run(context); | ||
}); | ||
}); |
Binary file added
BIN
+1.2 KB
...ies/botbuilder/tests/teams/activityUpdateAndDelete/teams-app-manifest/color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions
43
libraries/botbuilder/tests/teams/activityUpdateAndDelete/teams-app-manifest/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.3/MicrosoftTeams.schema.json", | ||
"manifestVersion": "1.3", | ||
"version": "1.0.0", | ||
"id": "", | ||
"packageName": "com.teams.sample.activityupdateanddelete", | ||
"developer": { | ||
"name": "ActivityUpdateAndDeleteBot", | ||
"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": "ActivityUpdateAndDeleteBot", | ||
"full": "ActivityUpdateAndDeleteBot" | ||
}, | ||
"description": { | ||
"short": "TeamsActivityUpdateAndDeleteBot", | ||
"full": "TeamsActivityUpdateAndDeleteBot" | ||
}, | ||
"accentColor": "#FFFFFF", | ||
"bots": [ | ||
{ | ||
"botId": "", | ||
"scopes": [ | ||
"groupchat", | ||
"team", | ||
"personal" | ||
], | ||
"supportsFiles": false, | ||
"isNotificationOnly": false | ||
} | ||
], | ||
"permissions": [ | ||
"identity", | ||
"messageTeamMembers" | ||
], | ||
"validDomains": [] | ||
} |
Binary file added
BIN
+383 Bytes
...s/botbuilder/tests/teams/activityUpdateAndDelete/teams-app-manifest/outline.png
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
11
libraries/botbuilder/tests/teams/activityUpdateAndDelete/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |