gpt4 book ai didi

node.js - Microsoft Bot Framework,可以启动对话吗

转载 作者:搜寻专家 更新时间:2023-10-31 23:58:46 26 4
gpt4 key购买 nike

我正在 Azure 中使用 Microsoft Bot Framework,并且有一个工作机器人。然而,到目前为止,它处于空状态等待,只是使用react。我想添加/开始这样的对话:“嗨,我能为您做什么?”

这是我的代码:

"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() }
}

最佳答案

您可以尝试以下代码并更改它以满足您的需要。您可以在 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/ 

关于node.js - Microsoft Bot Framework,可以启动对话吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45084057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com