forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
5,630 additions
and
11 deletions.
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
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 @@ | ||
BotId={TODO: BOT_ID} | ||
BotPassword={TODO: BOT_PASSWORD} |
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,23 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* |
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,41 @@ | ||
# Bots/Messaging Extension | ||
|
||
*Bots* allow users to interact with your web service through text, interactive cards, and task modules. *Messaging extensions* allow users to interact with your web service through buttons and forms in the Microsoft Teams client. They can search, or initiate actions, in an external system from the compose message area, the command box, or directly from a message. | ||
|
||
## Prerequisites | ||
|
||
**Dependencies** | ||
- [NodeJS](https://nodejs.org/en/) | ||
- [ngrok](https://ngrok.com/) or equivalent tunneling solution | ||
- [M365 developer account](https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/build-and-test/prepare-your-o365-tenant) or access to a Teams account with the appropriate permissions to install an app. | ||
|
||
**Configure Ngrok** | ||
|
||
Your app will be run from a localhost server. You will need to setup Ngrok in order to tunnel from the Teams client to localhost. | ||
|
||
**Run Ngrok** | ||
|
||
Run ngrok - point to port 3978 | ||
|
||
`ngrok http -host-header=rewrite 3978` | ||
|
||
**Update Bot Framework Messaging Endpoint** | ||
|
||
Note: You can also do this with the Manifest Editor in App Studio if you are familiar with the process. | ||
|
||
- For the Messaging endpoint URL, use the current `https` URL you were given by running ngrok and append it with the path `/api/messages`. It should like something work `https://{subdomain}.ngrok.io/api/messages`. | ||
|
||
- Click on the `Bots` menu item from the toolkit and select the bot you are using for this project. Update the messaging endpoint and press enter to save the value in the Bot Framework. | ||
|
||
- Ensure that you've [enabled the Teams Channel](https://docs.microsoft.com/en-us/azure/bot-service/channel-connect-teams?view=azure-bot-service-4.0) | ||
|
||
## Build and run | ||
|
||
### `npm install` | ||
|
||
### `npm start` | ||
|
||
## Deploy to Teams (Visual Studio Toolkit Only) | ||
Start debugging the project by hitting the `F5` key or click the debug icon in Visual Studio Code and click the `Start Debugging` green arrow button. | ||
|
||
|
71 changes: 71 additions & 0 deletions
71
samples/bot-conversation-quickstart/js/botActivityHandler.js
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,71 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
const { | ||
TurnContext, | ||
MessageFactory, | ||
TeamsActivityHandler, | ||
CardFactory, | ||
ActionTypes | ||
} = require('botbuilder'); | ||
|
||
class BotActivityHandler extends TeamsActivityHandler { | ||
constructor() { | ||
super(); | ||
/* Teams bots are Microsoft Bot Framework bots. | ||
If a bot receives a message activity, the turn handler sees that incoming activity | ||
and sends it to the onMessage activity handler. | ||
Learn more: https://aka.ms/teams-bot-basics. | ||
NOTE: Ensure the bot endpoint that services incoming conversational bot queries is | ||
registered with Bot Framework. | ||
Learn more: https://aka.ms/teams-register-bot. | ||
*/ | ||
// Registers an activity event handler for the message event, emitted for every incoming message activity. | ||
this.onMessage(async (context, next) => { | ||
TurnContext.removeRecipientMention(context.activity); | ||
switch (context.activity.text.trim()) { | ||
case 'Hello': | ||
await this.mentionActivityAsync(context); | ||
break; | ||
default: | ||
// By default for unknown activity sent by user show | ||
// a card with the available actions. | ||
const value = { count: 0 }; | ||
const card = CardFactory.heroCard( | ||
'Lets talk...', | ||
null, | ||
[{ | ||
type: ActionTypes.MessageBack, | ||
title: 'Say Hello', | ||
value: value, | ||
text: 'Hello' | ||
}]); | ||
await context.sendActivity({ attachments: [card] }); | ||
break; | ||
} | ||
await next(); | ||
}); | ||
} | ||
|
||
/** | ||
* Say hello and @ mention the current user. | ||
*/ | ||
async mentionActivityAsync(context) { | ||
const TextEncoder = require('html-entities').XmlEntities; | ||
|
||
const mention = { | ||
mentioned: context.activity.from, | ||
text: `<at>${ new TextEncoder().encode(context.activity.from.name) }</at>`, | ||
type: 'mention' | ||
}; | ||
|
||
const replyActivity = MessageFactory.text(`Hi ${ mention.text }`); | ||
replyActivity.entities = [mention]; | ||
|
||
await context.sendActivity(replyActivity); | ||
} | ||
} | ||
|
||
module.exports.BotActivityHandler = BotActivityHandler; | ||
|
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,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
// index.js is used to setup and configure your bot | ||
|
||
// Import required packages | ||
const path = require('path'); | ||
const express = require('express'); | ||
|
||
// Import required bot services. | ||
// See https://aka.ms/bot-services to learn more about the different parts of a bot. | ||
const { BotFrameworkAdapter } = require('botbuilder'); | ||
|
||
// Import bot definitions | ||
const { BotActivityHandler } = require('./botActivityHandler'); | ||
|
||
// Read botFilePath and botFileSecret from .env file. | ||
const ENV_FILE = path.join(__dirname, '.env'); | ||
require('dotenv').config({ path: ENV_FILE }); | ||
|
||
// Create adapter. | ||
// See https://aka.ms/about-bot-adapter to learn more about adapters. | ||
const adapter = new BotFrameworkAdapter({ | ||
appId: process.env.BotId, | ||
appPassword: process.env.BotPassword | ||
}); | ||
|
||
adapter.onTurnError = async (context, error) => { | ||
// This check writes out errors to console log .vs. app insights. | ||
// NOTE: In production environment, you should consider logging this to Azure | ||
// application insights. | ||
console.error(`\n [onTurnError] unhandled error: ${ error }`); | ||
|
||
// Send a trace activity, which will be displayed in Bot Framework Emulator | ||
await context.sendTraceActivity( | ||
'OnTurnError Trace', | ||
`${ error }`, | ||
'https://www.botframework.com/schemas/error', | ||
'TurnError' | ||
); | ||
|
||
// Send a message to the user | ||
await context.sendActivity('The bot encountered an error or bug.'); | ||
await context.sendActivity('To continue to run this bot, please fix the bot source code.'); | ||
}; | ||
|
||
// Create bot handlers | ||
const botActivityHandler = new BotActivityHandler(); | ||
|
||
// Create HTTP server. | ||
const server = express(); | ||
const port = process.env.port || process.env.PORT || 3978; | ||
server.listen(port, () => | ||
console.log(`\Bot/ME service listening at http://localhost:${port}`) | ||
); | ||
|
||
// Listen for incoming requests. | ||
server.post('/api/messages', (req, res) => { | ||
adapter.processActivity(req, res, async (context) => { | ||
// Process bot activity | ||
await botActivityHandler.run(context); | ||
}); | ||
}); |
Oops, something went wrong.