Skip to content

Commit

Permalink
Merge pull request microsoft#125 from southworkscom/node-update-botbu…
Browse files Browse the repository at this point in the history
…ilder-sdk

[Node] Update to BotBuilder v3.8.2
  • Loading branch information
willportnoy authored Jun 23, 2017
2 parents 45d0f87 + bb42f6b commit 82c2622
Show file tree
Hide file tree
Showing 72 changed files with 194 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Node/blog-LUISActionBinding/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"dependencies": {
"bluebird": "^3.5.0",
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"lodash": "^4.17.4",
"schema-inspector": "^1.6.8"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/blog-LUISActionBinding/samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Microsoft Corp.",
"license": "MIT",
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^1.0.4",
"jsonpath": "^0.2.10",
"lodash": "^4.17.4",
Expand Down
3 changes: 3 additions & 0 deletions Node/blog-customChannelData/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Bot Framework Variables
MSFT_APP_ID=
MSFT_APP_PW=
24 changes: 14 additions & 10 deletions Node/blog-customChannelData/node_custom_channel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//load environment variables from the .env file
require('dotenv-extended').load();

//loading modules
var express = require("express");
var restify = require("restify");
Expand All @@ -10,8 +13,9 @@ const MSFT_APP_PW = process.env.MSFT_APP_PW;

//create an express server
var app = express();
app.listen( process.env.PORT || 3000, function(){
console.log("Express app listening on port: " + process.env.PORT || 3000);
var port = process.env.PORT || 3978;
app.listen(port, function () {
console.log("Express app listening on port: " + port);
});

//create a chat connector for the bot
Expand All @@ -24,31 +28,31 @@ var connector = new botbuilder.ChatConnector({
var bot = new botbuilder.UniversalBot(connector);

//hook up bot endpoint
app.post("/messages/receive", connector.listen());
app.post("/api/messages", connector.listen());

//root dialog
bot.dialog("/", function(session){
bot.dialog("/", function (session) {

console.log("-------------------------------------------------");
console.log("Bot Received Message at '/' dialogue endpoint: ");

//detect Facebook Messenger message here
if(session.message.address.channelId == "facebook"){
if (session.message.address.channelId == "facebook") {
session.send("Facebook message recognized!");
session.beginDialog("/send_share_button");
session.beginDialog("/send_share_button");
} else session.send("Channel other than Facebook recognized.");

});

//where we create a facebook share button using sourceEvent
bot.dialog("/send_share_button", function(session){
bot.dialog("/send_share_button", function (session) {
//construct a new message with the current session context
var msg = new botbuilder.Message(session).sourceEvent({
//specify the channel
facebook: {
//format according to channel's requirements
//(in our case, the above JSON required by Facebook)
attachment: {
//format according to channel's requirements
//(in our case, the above JSON required by Facebook)
attachment: {
type: "template",
payload: {
template_type: "generic",
Expand Down
3 changes: 2 additions & 1 deletion Node/blog-customChannelData/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Microsoft Bot Framework bot written in NodeJS to test issues.",
"main": "node_bot.js",
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^2.0.0",
"express": "^4.14.1",
"request-promise": "^4.1.1",
"restify": "^4.3.0"
Expand Down
2 changes: 1 addition & 1 deletion Node/capability-SimpleTaskAutomation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"restify": "^4.3.0",
"uuid": "^3.0.1"
},
Expand Down
2 changes: 1 addition & 1 deletion Node/capability-middlewareLogging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"restify": "^4.3.0"
}
}
2 changes: 1 addition & 1 deletion Node/cards-CarouselCards/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/cards-CarouselCards/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"restify": "^4.3.0",
"dotenv-extended": "^1.0.4"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/cards-RichCards/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/cards-RichCards/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"restify": "^4.3.0",
"dotenv-extended": "^1.0.4"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/core-AppInsights/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-AppInsights/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"dependencies": {
"applicationinsights": "^0.17.2",
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^1.0.4",
"restify": "^4.3.0"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/core-ChannelData/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-ChannelData/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^1.0.4",
"restify": "^4.3.0"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/core-CreateNewConversation/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-CreateNewConversation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"restify": "^4.3.0",
"dotenv-extended": "^1.0.4"
}
Expand Down
2 changes: 1 addition & 1 deletion Node/core-CustomState/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-CustomState/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"botbuilder-azure": "^3.0.2",
"dotenv-extended": "^1.0.4",
"restify": "^4.3.0"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-DirectLine/DirectLineBot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^1.0.4",
"restify": "^4.3.0"
}
Expand Down
4 changes: 2 additions & 2 deletions Node/core-DirectLine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A sample bot and a custom client communicating to each other using the Direct Li
The minimum prerequisites to run this sample are:
* Latest Node.js with NPM. Download it from [here](https://nodejs.org/en/download/).
* The Bot Framework Emulator. To install the Bot Framework Emulator, download it from [here](https://emulator.botframework.com/). Please refer to [this documentation article](https://github.com/microsoft/botframework-emulator/wiki/Getting-Started) to know more about the Bot Framework Emulator.
* Register your bot with the Microsoft Bot Framework. Please refer to [this](https://docs.microsoft.com/en-us/bot-framework/portal-register-bot) for the instructions. Once you complete the registration, update your bot configuration with the registered config values (See [Debugging locally using ngrok](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok) or [Deploying to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview)
* Register your bot with the Microsoft Bot Framework. Please refer to [this](https://docs.microsoft.com/en-us/bot-framework/portal-register-bot) for the instructions. Once you complete the registration, update your bot configuration with the registered config values (See [Debugging locally using ngrok](https://docs.microsoft.com/en-us/bot-framework/debug-bots-emulator) or [Deploying to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview)
* **[Recommended]** Visual Studio Code for IntelliSense and debugging, download it from [here](https://code.visualstudio.com/) for free.

#### Direct Line API
Expand Down Expand Up @@ -156,4 +156,4 @@ To get more information about how to get started in Bot Builder for Node and Dir
* [Direct Line Swagger file - v3.0](https://docs.botframework.com/en-us/restapi/directline3/swagger.json)
* [Swagger-JS](https://github.com/swagger-api/swagger-js)
* [Send and receive attachments](https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-receive-attachments)
* [Debugging locally using ngrok](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok)
* [Debugging locally using ngrok](https://docs.microsoft.com/en-us/bot-framework/debug-bots-emulator)
2 changes: 1 addition & 1 deletion Node/core-DirectLine/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion Node/core-DirectLineWebSockets/DirectLineBot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.7.0",
"botbuilder": "^3.8.2",
"dotenv-extended": "^1.0.4",
"restify": "^4.3.0"
}
Expand Down
18 changes: 11 additions & 7 deletions Node/core-DirectLineWebSockets/DirectLineClient/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ var directLineClient = rp(directLineSpecUrl)
})
.then(function (client) {
// Obtain a token using the Direct Line secret
// First, add the Direct Line Secret to the client's auth header
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));

// Second, request a token for a new conversation
return client.Tokens.Tokens_GenerateTokenForNewConversation().then(function (response) {
return rp({
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + directLineSecret
},
json: true
}).then(function (response) {
// Then, replace the client's auth secret with the new token
var token = response.obj.token;
var token = response.token;
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
return client;
return client;
});
})
.catch(function (err) {
console.error('Error initializing DirectLine client', err);
throw err;
});

// Once the client is ready, create a new conversation
Expand Down
36 changes: 20 additions & 16 deletions Node/core-DirectLineWebSockets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A sample bot and a custom client communicating to each other using the Direct Li
The minimum prerequisites to run this sample are:
* Latest Node.js with NPM. Download it from [here](https://nodejs.org/en/download/).
* The Bot Framework Emulator. To install the Bot Framework Emulator, download it from [here](https://emulator.botframework.com/). Please refer to [this documentation article](https://github.com/microsoft/botframework-emulator/wiki/Getting-Started) to know more about the Bot Framework Emulator.
* Register your bot with the Microsoft Bot Framework. Please refer to [this](https://docs.microsoft.com/en-us/bot-framework/portal-register-bot) for the instructions. Once you complete the registration, update your bot configuration with the registered config values (See [Debugging locally using ngrok](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok) or [Deploying to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview))
* Register your bot with the Microsoft Bot Framework. Please refer to [this](https://docs.microsoft.com/en-us/bot-framework/portal-register-bot) for the instructions. Once you complete the registration, update your bot configuration with the registered config values (See [Debugging locally using ngrok](https://docs.microsoft.com/en-us/bot-framework/debug-bots-emulator) or [Deploying to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview)
* **[Recommended]** Visual Studio Code for IntelliSense and debugging, download it from [here](https://code.visualstudio.com/) for free.

#### Direct Line API
Expand All @@ -27,14 +27,14 @@ Refer to [this](https://docs.microsoft.com/en-us/bot-framework/portal-configure-
![Configure Direct Line](images/outcome-configure.png)

#### Publish
Also, in order to be able to run and test this sample you must [publish your bot, for example to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview). Alternatively, you can [Debug locally using ngrok](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok).
Also, in order to be able to run and test this sample you must [publish your bot, for example to Azure](https://docs.microsoft.com/en-us/bot-framework/publish-bot-overview). Alternatively, you can [Debug locally using ngrok](https://docs.microsoft.com/en-us/bot-framework/debug-bots-emulator).
Remember to update the environment variables with the `MICROSOFT_APP_ID` and `MICROSOFT_APP_PASSWORD` on the [.env](DirectLineBot/.env) file.

### Code Highlights

The Direct Line API is a simple REST API for connecting directly to a single bot. This API is intended for developers writing their own client applications, web chat controls, or mobile apps that will talk to their bot. In this sample, we are using the [Direct Line Swagger file](https://docs.botframework.com/en-us/restapi/directline3/swagger.json) and [Swagger JS](https://github.com/swagger-api/swagger-js) to create a client for Node that will simplify access to the underlying REST API. Check out the client's [app.js](DirectLineClient/app.js#L22-L46) to see the client setup & initialization.

You'll see that we are using the Direct Line secret to [obtain a token](DirectLineClient/app.js#L32-L42). This step is optional, but prevents clients from accessing conversations they aren't participating in.
You'll see that we are using the Direct Line secret to [obtain a token](DirectLineClient/app.js#L32-L45). This step is optional, but prevents clients from accessing conversations they aren't participating in.
After the token is obtained, the client's auth secret is replaced with this new token.

````JavaScript
Expand All @@ -49,24 +49,28 @@ var directLineClient = rp(directLineSpecUrl)
})
.then(function (client) {
// Obtain a token using the Direct Line secret
// First, add the Direct Line Secret to the client's auth header
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + directLineSecret, 'header'));

// Second, request a token for a new conversation
return client.Tokens.Tokens_GenerateTokenForNewConversation().then(function (response) {
return rp({
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + directLineSecret
},
json: true
}).then(function (response) {
// Then, replace the client's auth secret with the new token
var token = response.obj.token;
var token = response.token;
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
return client;
return client;
});
})
.catch(function (err) {
console.error('Error initializing DirectLine client', err);
throw err;
});
````

Each conversation on the Direct Line channel must be explicitly started using the `client.Conversations.Conversations_StartConversation()` function.
Check out the client's following [function call](DirectLineClient/app.js#L49-L65) which creates a new conversation.
Check out the client's following [function call](DirectLineClient/app.js#L53-L69) which creates a new conversation.

````JavaScript
directLineClient.then(function (client) {
Expand Down Expand Up @@ -112,11 +116,11 @@ client.Conversations.Conversations_PostActivity(

Messages from the Bot are being received using the WebSocket protocol (actually WSS). For this, after the conversation was created a `streamUrl` is also returned and it will be the target for the WebSocket connection.

Check out the client's [startReceivingWebSocketClient](DirectLineClient/app.js#L100-L128) and [startReceivingW3CWebSocketClient](DirectLineClient/app.js#L130-L155) functions which create WebSocket clients hitting the `streamUrl` value returned when the conversation was created (one or other will be called dependening on the `w3c` optional flag when running the console app). Messages are then filtered from anyone but our own client using the [`printMessages`](DirectLineClient/app.js#L160-L175 ) function.
Check out the client's [startReceivingWebSocketClient](DirectLineClient/app.js#L104-L133) and [startReceivingW3CWebSocketClient](DirectLineClient/app.js#L135-L161) functions which create WebSocket clients hitting the `streamUrl` value returned when the conversation was created (one or other will be called dependening on the `w3c` optional flag when running the console app). Messages are then filtered from anyone but our own client using the [`printMessages`](DirectLineClient/app.js#L164-L179) function.

Each of these functions showcase the two ways you can connect to the `streamUrl` using WebSockets (first one using a custom Node.js implementation, while the second one uses W3C one). If you look closely they are very similar and within the `on message` event handler the bot's response is being parsed to JSON in order to print it.

For `startReceivingWebSocketClient` we have the following [handler](DirectLineClient/app.js#L117-L125):
For `startReceivingWebSocketClient` we have the following [handler](DirectLineClient/app.js#L121-L129):

````JavaScript
connection.on('message', function (message) {
Expand All @@ -129,7 +133,7 @@ connection.on('message', function (message) {
});
````

And for `startReceivingW3CWebSocketClient` we have the following [handler](DirectLineClient/app.js#L148-L156):
And for `startReceivingW3CWebSocketClient` we have the following [handler](DirectLineClient/app.js#L152-L160):

````JavaScript
ws.onmessage = function (e) {
Expand All @@ -146,7 +150,7 @@ ws.onmessage = function (e) {
Direct Line v3.0 (unlike version 1.1) has supports for Attachments (see [send and receive attachments](https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-receive-attachments) for more information about attachments).

Check out the [`printMessage`](DirectLineClient/app.js#L177-L196) function to see how the Attachments are retrieved and rendered appropriately based on their type.
Check out the [`printMessage`](DirectLineClient/app.js#L181-L200) function to see how the Attachments are retrieved and rendered appropriately based on their type.

````JavaScript
function printMessage(activity) {
Expand Down Expand Up @@ -196,4 +200,4 @@ To get more information about how to get started in Bot Builder for Node and Dir
* [Direct Line Swagger file - v3.0](https://docs.botframework.com/en-us/restapi/directline3/swagger.json)
* [Swagger-JS](https://github.com/swagger-api/swagger-js)
* [Send and receive attachments](https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-receive-attachments)
* [Debugging locally using ngrok](https://docs.botframework.com/en-us/node/builder/guides/core-concepts/#debugging-locally-using-ngrok)
* [Debugging locally using ngrok](https://docs.microsoft.com/en-us/bot-framework/debug-bots-emulator)
2 changes: 1 addition & 1 deletion Node/core-DirectLineWebSockets/azuredeploy.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
"defaultValue": "6.9.5"
},
"MICROSOFT_APP_ID": {
"type": "string"
Expand Down
Loading

0 comments on commit 82c2622

Please sign in to comment.