Skip to content

Instantly share code, notes, and snippets.

View jazzedge's full-sized avatar

Rocco Labellarte jazzedge

View GitHub Profile
// Setup help system
bot.recognizer(new builder.RegExpRecognizer('HelpIntent', /^(help|options)/i));
bot.dialog('helpDialog', function (session, args) {
switch (args.action) {
default:
// args.action is '*:/help' indicating the triggerAction() was matched
session.endDialog("You can say 'flip a coin' or 'roll dice'.");
break;
case 'flipCoinHelp':
session.endDialog("Say 'heads' or 'tails'.");
@jazzedge
jazzedge / gist:df3f81d58467e9a8c2337e6629ad97e9
Created July 8, 2017 13:21
Bot check if dialog already active
// https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-help/apps.js
function switchTasks(session, args, next, alreadyActiveMessage) {
// Check to see if we're already active.
// - We're assuming that we're being called from a triggerAction() some
// args.action is the fully qualified dialog ID.
var stack = session.dialogStack();
if (builder.Session.findDialogStackEntry(stack, args.action) >= 0) {
session.send(alreadyActiveMessage);
} else {
// Clear stack and switch tasks
@jazzedge
jazzedge / gist:06887bc844cb3a76eb003548e88a6151
Last active July 28, 2017 11:33
Bot logging middleware
// 01. First example
// https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-logging/app.js
// Install logging middleware
bot.use({
botbuilder: function (session, next) {
if (/^log on/i.test(session.message.text)) {
session.userData.isLogging = true;
session.send('Logging is now turned on');
} else if (/^log off/i.test(session.message.text)) {
session.userData.isLogging = false;
@jazzedge
jazzedge / gist:b71190c2299d50e7aefa1d79bd06197f
Last active August 2, 2017 06:59
Bot - Loop to fill a form
// https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-loops/app.js
//This Bot demonstrates how to use session.replaceDialog() to create a simple
// loop that dyanmically populates a form.
// Add Q&A dialog
bot.dialog('q&aDialog', [
function (session, args) {
// Save previous state (create on first call)
session.dialogData.index = args ? args.index : 0;
session.dialogData.form = args ? args.form : {};
@jazzedge
jazzedge / gist:223937f6a242bc77cdba3d5130651336
Created July 8, 2017 13:27
Bot - choice menu and reload menu
// https://github.com/Microsoft/BotBuilder/blob/master/Node/examples/basics-menus/app.js
//This Bot demonstrates how to create a simple menu for a bot. We've also added a
//reloadAction() to the menus dialog which lets you return to the menu from any
//child dialog by simply saying "menu" or "back".
bot.dialog('rootMenu', [
function (session) {
builder.Prompts.choice(session, "Choose an option:", 'Flip A Coin|Roll Dice|Magic 8-Ball|Quit');
},
function (session, results) {
// https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/demo-ContosoFlowers
Bot Libraries for Creating Reusable Dialogs
Libraries of reusable parts can be developed by creating a new Library instance and adding dialogs just as you would to a bot.
Your library should have a unique name that corresponds to either your libraries website or NPM module name.
Bots can then reuse your library by simply adding your parts Library instance to their bot using UniversalBot.library().
To invoke dialogs within the bot, we use session.beginDialog() with a fully qualified dialog id in the form of ':'.
E.g.: To start the shopping's experience root dialog we use session.beginDialog('shop:/').
@jazzedge
jazzedge / gist:753178e0958c0092235b186f47a2620b
Last active August 10, 2017 11:45
Bot - Libraries #2 email validation as an external library
https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/demo-ContosoFlowers
This is how you could package an email validation:
var EmailRegex = new RegExp(/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
var lib = new builder.Library('validators');
lib.dialog('email',
new builder.IntentDialog()
var welcomeCard = new builder.HeroCard(session)
.title('welcome_title')
.subtitle('welcome_subtitle')
.images([
new builder.CardImage(session)
.url('https://placeholdit.imgix.net/~text?txtsize=56&txt=Contoso%20Flowers&w=640&h=330')
.alt('contoso_flowers')
])
.buttons([
builder.CardAction.imBack(session, session.gettext(MainOptions.Shop), MainOptions.Shop),
@jazzedge
jazzedge / gist:808c73a710d8ebc540723a9b6e1b29d1
Last active July 28, 2017 16:35
Bot Core start-up code
'use strict';
// 01. Include required files
require('dotenv-extended').load();
var restify = require('restify');
var builder = require('botbuilder');
var rp = require('request-promise');
var azure = require('botbuilder-azure');
var Request = require('tedious').Request;
var Connection = require('tedious').Connection;
@jazzedge
jazzedge / gist:453e160fbf5d44d2124e68100bb5bbbd
Created July 10, 2017 08:08
Bot Clear UserData specific to the questions
function clearData(session) {
var name = session.userData.myName;
var email = session.userData.myEmail;
session.userData = {};
session.userData.myName = name;
session.userData.myEmail = email;
}