LUIS Wiki Sample
var restify = require('restify'); | |
var builder = require('botbuilder'); | |
var request = require('request'); | |
var querystring = require('querystring'); | |
// Setup restify | |
var server = restify.createServer(); | |
server.listen(3990, function () { | |
console.log('%s listening at %s', server.name, server.url); | |
}); | |
var connector = new builder.ChatConnector(); | |
// Define REST endpoint for Bot Framework | |
server.post('/api/messages', connector.listen()); | |
// Creates our bot. | |
var bot = new builder.UniversalBot(connector, (session, args) => { | |
session.send("I'm sorry, I'm afraid I don't understand %s.", session.message.text); | |
}); | |
/* | |
* LUIS Recogniser Setup | |
* Sets up a recogniser that uses LUIS to deduce intents. | |
*/ | |
var luisEndpoint = "LUIS_ENDPOINT"; // Endpoint on the Publish page, without the trailing &q=. | |
var luisRecogniser = new builder.LuisRecognizer(luisEndpoint); | |
bot.recognizer(luisRecogniser); // Assigns the LUIS recogniser to the bot | |
// Dialogs for each intent that our bot responds to | |
// Describe intent - uses the Wikipedia Summary API to pull information about the search term. | |
bot.dialog("WikiSearchDialog", (session, args) => { | |
var intent = args.intent; // Extract predicted intent information | |
var subject = builder.EntityRecognizer.findAllEntities(intent.entities, "SearchObject"); // Get all entities matching SearchObject | |
var subjectValue = (subject.map((entity) => {return entity.entity}).join(" ")).replace("?", ""); // Join them into one string, ignoring ? | |
if(subjectValue == "") { | |
//No entities were found. | |
session.send("Sorry, I couldn't find a search object in your message :("); | |
} | |
subjectValue = querystring.escape(subjectValue); | |
// Query the Wikipedia summary API: https://en.wikipedia.org/api/rest_v1/#!/Page_content/get_page_summary_title | |
var wikiGetEndpoint = "https://en.wikipedia.org/api/rest_v1/page/summary/" + subjectValue; | |
request(wikiGetEndpoint, (err, resp, body) => { | |
var resultObject = JSON.parse(body); | |
if(resultObject.title == "Not found.") { // No Wikipedia page or redirect exists. | |
session.send("Oops, I can't find that."); | |
} else { | |
session.send("Here's what I found:"); | |
session.send(resultObject.extract); // Get summary | |
if(resultObject.thumbnail) { | |
//If the Wikipedia summary came with a thumbnail, send it. | |
session.send("I also found this picture:"); | |
session.send({ | |
attachments:[ | |
{ | |
contentType: "image/jpg", | |
contentUrl: resultObject.thumbnail.source | |
} | |
] | |
}) | |
} | |
} | |
}); | |
}).triggerAction({ | |
matches: "Describe" | |
}); | |
// Default dialog for the None intent - cases when we don't want our bot to do a search. | |
bot.dialog("DefaultDialog", (session, args) => { | |
session.send("Hello! I'm WikiBot. Try asking me a question like: \"what is football?\""); | |
}).triggerAction({ | |
matches: "None" | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment