Microsoft Bot Framework,它可以启动对话

时间:2017-07-13 14:45:07

标签: node.js azure botframework

我在Azure中使用Microsoft Bot Framework并且有一个工作机器人。但是,截至目前,它处于空状态,只是作出反应。我想用以下内容添加/开始对话:"嗨,我该如何帮助你?"

这是我的代码:

"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'],
    stateEndpoint: process.env['BotStateEndpoint'],
    openIdMetadata: process.env['BotOpenIdMetadata']
});

var bot = new builder.UniversalBot(connector);
bot.localePath(path.join(__dirname, './locale'));

// Make sure you add code to validate these fields
var luisAppId = process.env.LuisAppId;
var luisAPIKey = process.env.LuisAPIKey;
var luisAPIHostName = process.env.LuisAPIHostName || 'westus.api.cognitive.microsoft.com';

const LuisModelUrl = 'https://' + luisAPIHostName + '/luis/v1/application?id=' + luisAppId + '&subscription-key=' + luisAPIKey;

// Main dialog with LUIS
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
var intents = new builder.IntentDialog({ recognizers: [recognizer] })
/*
.matches('<yourIntent>')... See details at http://docs.botframework.com/builder/node/guides/understanding-natural-language/
*/

.matches('None', (session, args) => {
    session.send('Hi, this is the None handler. You said: \'%s\'.', session.message.text);
})

.matches('get_price', (session, args) => {
    session.send('Hi, you asked about the cost of a service: oil change: $10, brakes: $50, transmission: $200: \'%s\'.', session.message.text);
})

.matches('get_service', (session, args) => {
    session.send('Hi, you asked about car service options, here they are: oil change, brakes, and transmissions');
})

.matches('cant_service', (session, args) => {
    session.send('Sorry, we do not offer that service: \'%s\'.', session.message.text);
})

.matches('schedule_apt', (session, args) => {
    session.send('Hi, you asked about scheduling an appointment, please call 1-800-fix-cars to schedule');
})

.matches('greeting', (session, args) => {
    session.send('Hi you!');
})


.onDefault((session) => {
    session.send('Sorry, I did not understand \'%s\'.', session.message.text);
});

bot.dialog('/', intents);    

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');
    });
    server.post('/api/messages', connector.listen());    
} else {
    module.exports = { default: connector.listen() }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码并根据自己的需要进行更改。你可以找到node SDK here

的这个和许多其他有用的片段
bot.on('conversationUpdate', function (message) {
    if (message.membersAdded) {
         message.membersAdded.forEach(function (identity) {
            if (identity.id == message.address.bot.id) {                
                var reply = new builder.Message()
                        .address(message.address)
                        .text("Welcome to my page");
                bot.send(reply);
            } else {
                var address = Object.create(message.address);
                address.user = identity;
                var reply = new builder.Message()
                        .address(address)
                        .text("Hello %s I\'m botty McBotface", identity.name);
                bot.send(reply);
                bot.loadSession(address)
                session.send(
                   "test" 
                )
            }
        });
    }
});  

同样无关,您应该将您的luis端点更新为v2端点,如下所示:

westus.api.cognitive.microsoft.com/luis/v2.0/apps/