Skip to content

Commit

Permalink
Added new ProgressDialog sample for Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevenic committed Apr 18, 2017
1 parent 2fa63e7 commit 2d44e69
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Node/core-ProgressDIalog/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Bot Framework Credentials

MICROSOFT_APP_ID=
MICROSOFT_APP_PASSWORD=
20 changes: 20 additions & 0 deletions Node/core-ProgressDIalog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Progress Dialog Sample

This demo shows how to create a progress dialog that will periodically notify users of the status of a long running task.

[![Deploy to Azure][Deploy Button]][Deploy Node/ProgressDialog]

[Deploy Button]: https://azuredeploy.net/deploybutton.png
[Deploy Node/ProgressDialog]: https://azuredeploy.net

### Prerequisites

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.
* **[Recommended]** Visual Studio Code for IntelliSense and debugging, download it from [here](https://code.visualstudio.com/) for free.

### More Information

To get more information about how to get started in Bot Builder for Node please review the following resources:
* [Bot Builder for Node.js Reference](https://docs.botframework.com/en-us/node/builder/overview/#navtitle)
98 changes: 98 additions & 0 deletions Node/core-ProgressDIalog/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*-----------------------------------------------------------------------------
This demo shows how to create a progress dialog that will periodically notify
users of the status of a long running task.
-----------------------------------------------------------------------------*/

var restify = require('restify');
var builder = require('botbuilder');

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});

// Listen for messages from users
server.post('/api/messages', connector.listen());

// Create your bot with a function to receive messages from the user.
var bot = new builder.UniversalBot(connector, [
function (session) {
// We can wrap a potentially long running operation with a progress dialog. This
// will periodically notify the user that the bot is still working on their
// request.
var options = {
initialText: 'Please wait... This may take a few moments.',
text: 'Please wait... This is taking a little longer then expected.',
speak: '<speak>Please wait.<break time="2s"/></speak>'
};
progress(session, options, function (callback) {
// Make our async call here. If the call completes quickly then no progress
// message will be sent.
setTimeout(function () {
callback('You said: ' + session.message.text);
}, 20000);
});
},
function (session, results) {
session.send(results.response);
}
]);

/**
* Wrapper function to simplify calling our progress dialog.
* @param {*} session Current session object.
* @param {*} options Options to control messages sent by the progress dialog.
* Values:
* * text: (Required) progress text to send.
* * speak: (Optional) ssml to send with each progress message.
* * delay: (Optional) delay (in ms) before progress is sent.
* * initialText: (Optional) initial progress text.
* * initialSpeak: (Optional) initial progress ssml.
* * initialDelay: (Optional) delay before initial progress is sent.
* @param {*} asyncFn Async function to call. Will be passed a callback with a
* signature of (response: any) => void.
*/
function progress(session, options, asyncFn) {
session.beginDialog('progressDialog', {
asyncFn: asyncFn,
options: options
})
}

bot.dialog('progressDialog', function (session, args) {
var asyncFn = args.asyncFn;
var options = args.options;

var count = 0;
function sendProgress() {
if (count++ > 0) {
session.say(options.text, options.speak, { inputHint: builder.InputHint.ignoringInput });
} else {
var text = options.initialText || options.text;
var speak = options.initialSpeak || options.speak;
session.say(text, speak, { inputHint: builder.InputHint.ignoringInput });
}
hTimer = setTimeout(sendProgress, options.delay || 9000);
}

// Start progress timer
var hTimer = setTimeout(sendProgress, options.initialDelay || 1000);

// Call async function
try {
asyncFn(function (response) {
// Stop timer and return response
clearTimeout(hTimer);
session.endDialogWithResult({ response: response });
});
} catch (err) {
session.error(err);
}
});
129 changes: 129 additions & 0 deletions Node/core-ProgressDIalog/azuredeploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"siteName": {
"defaultValue": "BotBuilder-Samples",
"type": "string"
},
"hostingPlanName": {
"type": "string"
},
"siteLocation": {
"type": "string"
},
"sku": {
"type": "string",
"allowedValues": [
"Free",
"Shared",
"Basic",
"Standard"
],
"defaultValue": "Free"
},
"workerSize": {
"type": "string",
"allowedValues": [
"0",
"1",
"2"
],
"defaultValue": "0"
},
"repoUrl": {
"type": "string"
},
"branch": {
"type": "string"
},
"Project": {
"type": "string",
"defaultValue": "Node/core-ProgressDialog"
},
"WEBSITE_NODE_DEFAULT_VERSION": {
"type": "string",
"defaultValue": "5.9.1"
},
"MICROSOFT_APP_ID": {
"type": "string"
},
"MICROSOFT_APP_PASSWORD": {
"type": "string"
}
},
"resources": [
{
"apiVersion": "2014-06-01",
"name": "[parameters('hostingPlanName')]",
"type": "Microsoft.Web/serverFarms",
"location": "[parameters('siteLocation')]",
"properties": {
"name": "[parameters('hostingPlanName')]",
"sku": "[parameters('sku')]",
"workerSize": "[parameters('workerSize')]",
"numberOfWorkers": 1
}
},
{
"apiVersion": "2014-06-01",
"name": "[parameters('siteName')]",
"type": "Microsoft.Web/Sites",
"location": "[parameters('siteLocation')]",
"dependsOn": [
"[concat('Microsoft.Web/serverFarms/', parameters('hostingPlanName'))]"
],
"tags": {
"[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "empty"
},
"properties": {
"name": "[parameters('siteName')]",
"serverFarm": "[parameters('hostingPlanName')]"
},
"resources": [
{
"apiVersion": "2014-04-01",
"type": "config",
"name": "web",
"dependsOn": [
"[concat('Microsoft.Web/Sites/', parameters('siteName'))]"
],
"properties": {
"appSettings": [
{
"name": "Project",
"value": "[parameters('Project')]"
},
{
"name": "WEBSITE_NODE_DEFAULT_VERSION",
"value": "[parameters('WEBSITE_NODE_DEFAULT_VERSION')]"
},
{
"name": "MICROSOFT_APP_ID",
"value": "[parameters('MICROSOFT_APP_ID')]"
},
{
"name": "MICROSOFT_APP_PASSWORD",
"value": "[parameters('MICROSOFT_APP_PASSWORD')]"
}
]
}
},
{
"apiVersion": "2014-04-01",
"name": "web",
"type": "sourcecontrols",
"dependsOn": [
"[resourceId('Microsoft.Web/Sites', parameters('siteName'))]",
"[concat('Microsoft.Web/Sites/', parameters('siteName'), '/config/web')]"
],
"properties": {
"RepoUrl": "[parameters('repoUrl')]",
"branch": "[parameters('branch')]",
"IsManualIntegration": true
}
}
]
}
]
}
27 changes: 27 additions & 0 deletions Node/core-ProgressDIalog/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "botbuilder-sample-progressdialog",
"version": "1.0.0",
"description": "Bot Builder Sample - Progress Dialog",
"scripts": {
"start": "node app.js"
},
"author": "Microsoft Corp.",
"license": "MIT",
"keywords": [
"botbuilder",
"bots",
"chatbots",
"botbuilder-samples"
],
"bugs": {
"url": "https://github.com/Microsoft/BotBuilder-Samples/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/Microsoft/BotBuilder-Samples.git"
},
"dependencies": {
"botbuilder": "^3.8.0-beta2",
"restify": "^4.3.0"
}
}
2 changes: 1 addition & 1 deletion Node/demo-RollerSkill/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ The sample showcases the use of new features designed specifically for speech:

### More Information

To get more information about how to get started in Bot Builder for Node and Attachments please review the following resources:
To get more information about how to get started in Bot Builder for Node please review the following resources:
* [Bot Builder for Node.js Reference](https://docs.botframework.com/en-us/node/builder/overview/#navtitle)
* [SSML Language Reference](https://msdn.microsoft.com/en-us/library/hh378377(v=office.14).aspx)

0 comments on commit 2d44e69

Please sign in to comment.