Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
@jazzedge
jazzedge / gist:d8bc6966fe50766453a7f6fc99471ab3
Last active July 28, 2017 10:24
Bot - Asynchronous operations
// See: https://github.com/Microsoft/BotBuilder-Samples/blob/master/Node/core-MultiDialogs/README.md
//Requires promises (bluebird) et.al.
Inevitably, you're going to want to make some asynchronous network call to retrieve data and then send those results to the user using the session object. This is completely fine but there are a few best practices you'll want to follow.
Use session.send within your callback or event handler.
Do not call session.endDialog() immediately after starting the asynchronous call;
Instead call session.endDialog() from within your callback or event handler.
Check out hotels.js where we are calling an asynchronous method that returns a Promise. Our fulfill method calls session.send() and is also responsible for calling session.endDialog().
@jazzedge
jazzedge / gist:e5f4bb128e1b674a4509d60c42acc9ba
Created July 28, 2017 11:00
Bot - Synchronous dialog example with error trapping
//This waterfall dialog shows how to wait until the content is returned and .then move onto the next part of the waterfall.
//It also shows how to catch erors
bot.dialog('synch', [
function (session, results, next) {
session.sendTyping();
html = getContent('https://api.tfl.gov.uk/Line/Mode/tube/Status?detail=true&app_id=...')
.then((html) => {
session.send(html);
next();
})
@jazzedge
jazzedge / gist:27cd3fb603d8512c8fc5140e44632aaf
Created July 28, 2017 11:10
Bot - Calculate depth of JSON
// Calculates the maximum depth of a JSON object. You must pass the string before converting it to JSON!
function getDepth(obj) { //Pass a string, not a JSON obj
var i = 0;
var depth = 0;
var maxDepth = 0;
while (i < obj.length)
{
switch (obj.substr(i,1)) {
case "{":
depth++;
@jazzedge
jazzedge / gist:eae23fd890fdf22cabdc031493b17b0e
Created July 28, 2017 11:15
Bot - Convert JSON to an array
// This code has been specialised to work only for the API https://api.tfl.gov.uk/Line/Mode/tube/Status?detail=true
// Requires JSON to be passed in as (o). Convert a string to JSON with: var htmlJSON = JSON.parse(html);
var arr = [];
var previousKey = "";
var parentKey = "";
function traverseJSON (o) {
for (var i in o) {
if(typeof (o[i]) =='object') {
//console.log('key : ' + i + ', value is an object'+ o[i]);
@jazzedge
jazzedge / gist:7a1911bbf8a869f5096b730ad432ef99
Created July 28, 2017 11:16
Bot - Examples of using arrays in node/javascript
'use strict';
var myData = [];
// w ww. j a va 2 s. c o m
myData.push(1); // add at the end
console.log(myData); // prints [1]
myData.unshift(2); // add to the top
console.log(myData); // prints [2,1]
// Arrays are zero index based:
//01. Configure Blob storage
var builder = require('botbuilder');
var azure = require('azure-storage');
var request = require('request').defaults({ encoding: null });
var accessKey = 'XdU6cKUYRT ... 5Bf3yzgjY1JKIg==';
var storageAccount = 'roccoblob01';
var containerName = 'roccoblobcontainer1';
@jazzedge
jazzedge / gist:d871b3e38dd4794e5686f041f621180b
Created July 28, 2017 11:30
Bot - Root Dialog variations
// 01. Root Dialog
var bot = new builder.UniversalBot(connector,
function (session) {
// echo the user's message
session.send("You said: %s", session.message.text);
}
);
// 01. Root Dialog
var bot = new builder.UniversalBot(connector, [
@jazzedge
jazzedge / gist:f1f272e9599032a6273aba0e46661242
Last active August 2, 2017 07:53
Bot - Bing Maps example
var locationDialog = require('botbuilder-location'); //Bing Maps
bot.library(locationDialog.createLibrary(process.env.BING_MAPS_KEY));
//Bing Maps Demo
bot.dialog("bingmap", [
function (session) {
var options = {
prompt: "Where should I ship your order?",
useNativeControl: true,
var intents = new builder.IntentDialog();
bot.dialog('/', intents);
intents.matches(/^Hi/i, [
function(session)
{
builder.Prompts.text(session, 'Hi there! How are you today?');
},
function(session, results)
{
session.send('%s! How can I help you?', results.response);
@jazzedge
jazzedge / gist:edc0e887d7762cb4259f4ae0d4ff6c58
Created July 28, 2017 11:40
Bot - Full Example: Loop in a dialog and push results, then display
'use strict';
// 01. Include required files
require('dotenv-extended').load();
var restify = require('restify');
var builder = require('botbuilder');
// ---------------------------------------------------------------------------------------------------------------------
// 02. Setup Restify Server
var server = restify.createServer();
// 03. Configure listen for messages