Last active
July 10, 2017 11:20
-
-
Save jazzedge/3fb981196bc04b0c9eff9c3912beec7c to your computer and use it in GitHub Desktop.
Bot QnA Maker sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //DEMO-QNA2.JS | |
| // Demonstrates use of the QnA Service | |
| // Combines: | |
| //https://stackoverflow.com/questions/42629688/begin-dialog-with-qna-maker-bot-framework-recognizer-node-js | |
| //and | |
| //https://github.com/Microsoft/BotBuilder-CognitiveServices/blob/master/Node/samples/QnAMakerSimpleBot/app.js | |
| 'use strict'; | |
| // 01. Include required files | |
| require('dotenv-extended').load(); | |
| var restify = require('restify'); | |
| var builder = require('botbuilder'); | |
| var cognitiveservices = require('botbuilder-cognitiveservices'); | |
| // --------------------------------------------------------------------------------------------------------------------- | |
| // 02. Setup Restify Server | |
| var server = restify.createServer(); | |
| // 03. Configure listen for messages | |
| server.listen(process.env.PORT || 3978, function() | |
| { | |
| console.log('%s listening to %s', server.name, server.url); | |
| }); | |
| // 04. Create chat bot | |
| var connector = new builder.ChatConnector | |
| ({ | |
| appId: process.env.MICROSOFT_APP_ID, | |
| appPassword: process.env.MICROSOFT_APP_PASSWORD | |
| }); | |
| var bot = new builder.UniversalBot(connector); | |
| // 05. Listen for messages | |
| server.post('/api/messages', connector.listen()); | |
| // 06. Serve static files | |
| server.get(/.*/, restify.serveStatic({ | |
| directory: __dirname, | |
| 'default': 'index.html' | |
| })); | |
| var recognizer = new cognitiveservices.QnAMakerRecognizer({ | |
| knowledgeBaseId: '69b068c4-3487-4f08-9986-6c2e53df8b83', | |
| subscriptionKey: '3f7dd24004444c8489eb23aa2d661102'}); | |
| var basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({ | |
| recognizers: [recognizer], | |
| defaultMessage: 'No match! Try changing the query terms!', | |
| qnaThreshold: 0.3 | |
| }); | |
| bot.dialog('/', [ | |
| function(session){ | |
| session.beginDialog('welcome'); | |
| }, | |
| function(session, results){ | |
| if (results.response == 'Rocco') { | |
| session.say ('Hi boss!'); | |
| } else { | |
| session.beginDialog('dialog'); | |
| } | |
| } | |
| ]); | |
| bot.dialog('welcome', [ | |
| function (session) { | |
| // Send a greeting and show help. | |
| builder.Prompts.text(session, "Hi! How can I help you?"); | |
| } | |
| ]); | |
| bot.dialog('dialog', basicQnAMakerDialog); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment