Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2018 06:27
Show Gist options
  • Save amay077/5f1a7fb82002a3eac5635b902bdc63eb to your computer and use it in GitHub Desktop.
Save amay077/5f1a7fb82002a3eac5635b902bdc63eb to your computer and use it in GitHub Desktop.
Azure Function Bot with LUIS sample
* Azure Functions bot templates use Azure Functions Pack for optimal performance, get
* familiar with Azure Functions Pack at
* This template demonstrates how to use an IntentDialog with a LuisRecognizer to add
* natural language support to a bot.
* For a complete walkthrough of creating this type of bot see the article at
* ---------------------------------------------------------------------------------------- */
"use strict";
var builder = require("botbuilder");
var botbuilder_azure = require("botbuilder-azure");
var path = require('path');
var useEmulator = (process.env.NODE_ENV == 'development');
var connector = useEmulator ? new builder.ChatConnector() : new botbuilder_azure.BotServiceConnector({
appId: process.env['MicrosoftAppId'],
appPassword: process.env['MicrosoftAppPassword'],
openIdMetadata: process.env['BotOpenIdMetadata']
* Bot Storage: This is a great spot to register the private state storage for your bot.
* We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
* For samples and documentation, see:
* ---------------------------------------------------------------------------------------- */
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
// Create your bot with a function to receive messages from the user
// This default message handler is invoked if the user's utterance doesn't
// match any intents handled by other dialogs.
var bot = new builder.UniversalBot(connector, function (session, args) {
session.send('You reached the default message handler. You said \'%s\'.', session.message.text);
bot.localePath(path.join(__dirname, './locale'));
bot.set('storage', tableStorage);
// Make sure you add code to validate these fields
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || '';
const LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v2.0/apps/' + luisAppId + '?subscription-key=' + luisAPIKey;
// Create a recognizer that gets intents from LUIS, and add it to the bot
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
// Add a dialog for each intent that the LUIS app recognizes.
// See
(session) => {
session.send('You reached the Greeting intent. You said \'%s\'.', session.message.text);
matches: 'Greeting'
(session) => {
session.send('You reached the Help intent. You said \'%s\'.', session.message.text);
matches: 'Help'
(session) => {
session.send('You reached the Cancel intent. You said \'%s\'.', session.message.text);
matches: 'Cancel'
if (useEmulator) {
var restify = require('restify');
var server = restify.createServer();
server.listen(3978, function() {
console.log('test bot endpont at http://localhost:3978/api/messages');
});'/api/messages', connector.listen());
} else {
module.exports = connector.listen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment