Skip to content

Instantly share code, notes, and snippets.

@TsuyoshiUshio
Created March 22, 2017 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TsuyoshiUshio/5e5a3de72996c3a9d46ae7690a250505 to your computer and use it in GitHub Desktop.
Save TsuyoshiUshio/5e5a3de72996c3a9d46ae7690a250505 to your computer and use it in GitHub Desktop.
DX hackathon Osaka Japanese Bot application using LUIS, Azure Search, and Docker container with go.
var builder = require('botbuilder');
var restify = require('restify');
var async = require('async');
var appInsights = require("applicationinsights");
appInsights.setup("d1eca9d7-5a5f-42ad-b78f-4836114ca0c1").start();
var AzureSearch = require('azure-search');
var client = AzureSearch({
url: "https://debot.search.windows.net",
key: "84EF48A2934F8BEFA2B1D71883A48EA4"
})
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// 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);
server.post('/api/messages', connector.listen());
// Create LUIS recognizer that points at our model and add it as the root '/' dialog for our Cortana Bot.
var model = '{LUIS model URL is here}';
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
dialog.matches('セッションの種類を知りたい', [
function (session, args, next) {
var sessionType = builder.EntityRecognizer.findEntity(args.entities, 'sessionType');
sessionType = sessionType ? sessionType.entity : null;
console.log("TYPE:" + sessionType);
if (!sessionType || sessionType == '種類' || sessionType == '種別') {
builder.Prompts.text(session, 'どんなセッション見たいんでっか?');
} else {
session.dialogData.sessionType = sessionType;
next();
}
},
function (session, results) {
console.log("TYPE1.5" + session.dialogData.sessionType);
if (!session.dialogData.sessionType){
var keyword = results.response;
session.dialogData.sessionType = keyword;
}
console.log("TYPE2" + session.dialogData.sessionType);
client.search("sessionsindex",{search: session.dialogData.sessionType, top: 5}, function(err, results){
session.send("そら、あんたはこれみなあかんですわ");
var cards = [];
async.each(results, function(entity, callback) {
console.log("step:");
cards.push(getCardsAttachment(session, entity));
callback();
}, function(err) {
if (err) throw err;
var reply = new builder.Message(session)
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(cards);
session.send(reply);
}
);
console.log(results);
});
}
]);
dialog.matches('ちゃうちゃうとちゃう', [
function (session, args, next) {
next();
},
function (session, results) {
session.send("ちゃうちゃうとちゃうんちゃいます?");
}
]);
function getCardsAttachment(session, entity) {
var speakerUrl = "http://13.73.6.205/convertedimage?url=" + encodeURLComponent(entity.speakerphoto)
return new builder.HeroCard(session)
.title(entity.title)
.subtitle(entity.translate)
.text(entity.abstract)
.images([
new builder.CardImage(session).url(speakerUrl)
])
.buttons([
builder.CardAction.openUrl(session, 'https://www.microsoft.com', '詳細やで')
]);
}
dialog.onDefault(builder.DialogAction.send("おいでやす"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment