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

[JS sample] Add CoreBot with Application-Insights #1773

Merged
merged 36 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f5643db
[AppInsights Sample] Init project [WIP]
gasper-az Sep 11, 2019
b6196bc
add ErrorViewModel class
Aliandi Sep 11, 2019
271e30e
add cancel dialog
Aliandi Sep 11, 2019
24b9952
add dateResolver dialog
Aliandi Sep 11, 2019
3ca77fd
Change ! to cast as boolean
Aliandi Sep 11, 2019
93db436
[AppInsights sample] Add dialogBot
gasper-az Sep 11, 2019
878adf6
[AppInsights sample] Export DialogBot
gasper-az Sep 11, 2019
ceae8a3
[AppInsights Sample] create DialogState
gasper-az Sep 11, 2019
0c1e07a
[AppInsight sample] dialogAndWelcomeBot + resource
gasper-az Sep 11, 2019
935ac9e
[AppInsight Bot] Fix styling.
gasper-az Sep 11, 2019
1fec03a
add header
Aliandi Sep 11, 2019
fbb6d0f
[AppInsights-Bot] Add ErrorViewModel class
gasper-az Sep 11, 2019
f8186d2
[AppInsights Bot] Add Bot folder
gasper-az Sep 11, 2019
29c9817
add bookingDialog
Aliandi Sep 11, 2019
7e1f808
[AppInsights Bot] Add new Dialogs
gasper-az Sep 12, 2019
7d9b209
[AppInsights Bot] Add mainDialog [WIP]
gasper-az Sep 11, 2019
0e74c2d
[AppInsights Bot] Add mainDialog
gasper-az Sep 12, 2019
531c5f9
[AppInsights Bot] Add MainDialog
gasper-az Sep 12, 2019
c323c54
[AppInsights Bot] Add index.js and flightBookingRecognizer
gasper-az Sep 12, 2019
4ca9ba8
[AppInsight Bot] fix minor issues in dialogs
gasper-az Sep 12, 2019
b4d72c4
[AppInsight Bot] Fix minor issues in dialogBot
gasper-az Sep 12, 2019
d457060
[AppInsights] add index.js
gasper-az Sep 12, 2019
d54100c
[AppInsights Bot] Add cognitiveModels
gasper-az Sep 12, 2019
e41954c
Add deployment templates
gasper-az Sep 12, 2019
bbce2c9
[ApiInsights Bot] Add index.js and folders
gasper-az Sep 12, 2019
84e5a60
[AppInsights Bot] Add README.md file
gasper-az Sep 13, 2019
793941d
Sample#21 - fix typo in mainDialog.js
gasper-az Sep 13, 2019
409d5ef
Update README.md file
gasper-az Sep 13, 2019
4ce2210
update package.json:
gasper-az Sep 13, 2019
8c91840
Add source files and update package.json
gasper-az Sep 13, 2019
5ba2e86
Fix ESLint Errors
gasper-az Sep 13, 2019
3e6ce6a
bookingDialog as parameter
gasper-az Sep 16, 2019
b0d8ffe
Apply feedback on Sample 21
gasper-az Sep 17, 2019
055c3d7
Sample21: Use TelemetryClient through ComponentDialog
gasper-az Sep 25, 2019
2e215cd
Sample 21: Restored previous Dialog constructors.
gasper-az Oct 1, 2019
9364032
Merge branch 'master' into feature/corebot-app-insights
garypretty Jan 23, 2020
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
6 changes: 6 additions & 0 deletions samples/javascript_nodejs/21.corebot-app-insights/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
MicrosoftAppId=
MicrosoftAppPassword=
LuisAppId=
LuisAPIKey=
LuisAPIHostName=
InstrumentationKey=
19 changes: 19 additions & 0 deletions samples/javascript_nodejs/21.corebot-app-insights/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
"extends": "standard",
"rules": {
"semi": [2, "always"],
"indent": [2, 4],
"no-return-await": 0,
"space-before-function-paren": [2, {
"named": "never",
"anonymous": "never",
"asyncArrow": "always"
}],
"template-curly-spacing": [2, "always"]
},
"env": {
"commonjs": true,
"node": true,
"mocha": true
}
};
4 changes: 4 additions & 0 deletions samples/javascript_nodejs/21.corebot-app-insights/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
.nyc_output/
.vscode/
.env
106 changes: 106 additions & 0 deletions samples/javascript_nodejs/21.corebot-app-insights/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# CoreBot with Application Insights

Bot Framework v4 core bot sample.

This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to:

- Use [LUIS](https://www.luis.ai) to implement core AI capabilities
- Implement a multi-turn conversation using Dialogs
- Handle user interruptions for such things as `Help` or `Cancel`
- Prompt for and validate requests for information from the user
- Use [Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/cloudservices) to monitor your bot

## Prerequisites

This sample **requires** prerequisites in order to run.

### Overview

This bot uses [LUIS](https://www.luis.ai), an AI based cognitive service, to implement language understanding
and [Application Insights](https://docs.microsoft.com/azure/azure-monitor/app/cloudservices), an extensible Application Performance Management (APM) service for web developers on multiple platforms.

- [Node.js](https://nodejs.org) version 10.14 or higher

```bash
# determine node version
node --version
```

### Create a LUIS Application to enable language understanding

The LUIS model for this example can be found under `CognitiveModels/FlightBooking.json` and the LUIS language model setup, training, and application configuration steps can be found [here](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-luis?view=azure-bot-service-4.0&tabs=cs).

Once you created the LUIS model, update `.env` with your `LuisAppId`, `LuisAPIKey`, `LuisAPIHostName` and AppInsights's `InstrumentationKey` (more info on instrumentation key [here](https://docs.microsoft.com/en-us/azure/azure-monitor/app/create-new-resource#copy-the-instrumentation-key)).

```json
"LuisAppId": "Your LUIS App Id",
"LuisAPIKey": "Your LUIS Subscription key here",
"LuisAPIHostName": "Your LUIS App region here (i.e: westus.api.cognitive.microsoft.com)"
"InstrumentationKey": "Your AppInsights instrumentation key here"
```

### Add Application Insights service to enable the bot monitoring
Application Insights resource creation steps can be found [here](https://docs.microsoft.com/azure/azure-monitor/app/create-new-resource).

## To try this sample

- Clone the repository

```bash
git clone https://github.com/microsoft/botbuilder-samples.git
```

- In a terminal, navigate to `samples/javascript_nodejs/21.corebot-app-insights`

```bash
cd samples/javascript_nodejs/21.corebot-app-insights
```

- Install modules

```bash
npm install
```

- Setup LUIS

The prerequisites outlined above contain the steps necessary to provision a language understanding model on www.luis.ai. Refer to _Create a LUIS Application to enable language understanding_ above for directions to setup and configure LUIS.

- Run the sample

```bash
npm start
```

## Testing the bot using Bot Framework Emulator

[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.

- Install the Bot Framework Emulator version 4.3.0 or greater from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)

### Connect to the bot using Bot Framework Emulator

- Launch Bot Framework Emulator
- File -> Open Bot
- Enter a Bot URL of `http://localhost:3978/api/messages`

## Deploy the bot to Azure

To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions.

## Further reading

- [Bot Framework Documentation](https://docs.botframework.com)
gasper-az marked this conversation as resolved.
Show resolved Hide resolved
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
- [Dialogs](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0)
- [Gathering Input Using Prompts](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp)
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
- [Azure Portal](https://portal.azure.com)
- [Language Understanding using LUIS](https://docs.microsoft.com/en-us/azure/cognitive-services/luis/)
- [Application Insights](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview)
- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
- [Restify](https://www.npmjs.com/package/restify)
- [dotenv](https://www.npmjs.com/package/dotenv)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

const { CardFactory } = require('botbuilder');
const { DialogBot } = require('./dialogBot');
const WelcomeCard = require('./resources/welcomeCard.json');

class DialogAndWelcomeBot extends DialogBot {
/**
* @param {ConversationState} conversationState
* @param {UserState} userState
* @param {Dialog} dialog
*/
constructor(conversationState, userState, dialog) {
super(conversationState, userState, dialog);

this.onMembersAdded(async (turnContext, next) => {
const membersAdded = turnContext.activity.membersAdded;

for (let pos = 0; pos < membersAdded.length; pos++) {
if (membersAdded[pos].id !== turnContext.activity.recipient.id) {
const welcomeCard = CardFactory.adaptiveCard(WelcomeCard);

await turnContext.sendActivity({ attachments: [welcomeCard] });
await dialog.run(turnContext, conversationState.createProperty('DialogState'));
}
}
// Ensure next BotHandler is executed.
await next();
});
}
}

module.exports.DialogAndWelcomeBot = DialogAndWelcomeBot;
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.

const { ActivityHandler } = require('botbuilder');

class DialogBot extends ActivityHandler {
/**
* @param {ConversationState} conversationState
* @param {UserState} userState
* @param {Dialog} dialog
*/
constructor(conversationState, userState, dialog) {
super();

if (!conversationState) { throw new Error('[DialogBot]: Missing conversationState parameter.'); }
if (!userState) { throw new Error('[DialogBot]: Missing userState parameter.'); }
if (!dialog) { throw new Error('[DialogBot]: Missing dialog parameter.'); }

this.conversationState = conversationState;
this.userState = userState;
this.dialog = dialog;

this.onTurn(async (turnContext, next) => {
await this.conversationState.saveChanges(turnContext, false);
await this.userState.saveChanges(turnContext, false);

// Ensure next BotHandler is executed.
await next();
});

this.onMessage(async (turnContext, next) => {
console.log('Running dialog with Message Activity.');
const dialogState = this.conversationState.createProperty('DialogState');

await this.dialog.run(turnContext, dialogState);

// Ensure next BotHandler is executed.
await next();
});

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);

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

module.exports.DialogBot = DialogBot;
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Image",
"url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU",
"size": "stretch"
},
{
"type": "TextBlock",
"spacing": "medium",
"size": "default",
"weight": "bolder",
"text": "Welcome to Bot Framework!",
"wrap": true,
"maxLines": 0
},
{
"type": "TextBlock",
"size": "default",
"isSubtle": "true",
"text": "Now that you have successfully run your bot, follow the links in this Adaptive Card to expand your knowledge of Bot Framework.",
"wrap": true,
"maxLines": 0
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "Get an overview",
"url": "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0"
},
{
"type": "Action.OpenUrl",
"title": "Ask a question",
"url": "https://stackoverflow.com/questions/tagged/botframework"
},
{
"type": "Action.OpenUrl",
"title": "Learn how to deploy",
"url": "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0"
}
]
}
Loading