将字符串与skype聊天中的用户输入进行比较

时间:2018-06-15 10:45:55

标签: node.js string if-statement botframework skype-bots

所以我想创建一个skype机器人,回复发给我的某些短语。特定短语或文本将有不同的答复。问题是我坚持将用户输入与特定字符串进行比较。由于某种原因,我从聊天输入中获取的字符串参数不是字符串变量,因为任何形式的字符串操作都不起作用,尽管typeof显示了它的字符串。

我使用node.js编写机器人并使用Bot Framework模拟器对其进行测试。

以下是代码示例:

var restify = require('restify');
var builder = require('botbuilder');

// Setup Restify Server
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 connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
    var comp = "%s"
    var comp1 = "hi"
    //comp1 == comp && msg1 = "Hi, how may I help you."
    var msg1 = ""
    if (comp == "hi") msg1 = "Hi, how may I help you."

    session.send(msg1, session.message.text);
});

如果我在程序中初始化一个新的字符串var,即comp1并使用它如下进行比较那么它是有效的,所以这排除了我的if语句错误。

if (comp1 == "hi") msg1 = "Hi, how may I help you."

1 个答案:

答案 0 :(得分:1)

BotBuilder具有.triggerAction,我认为它可以为您提供更好的服务(文档here)。您将创建单独的对话框来管理来自匹配项的流程。此外,您可以设置阈值(分数)来确定您要进行的匹配程度。

在下面的示例中,当提到单词“ person”时,将触发辅助对话框。 0.8阈值允许“人”和“一个人”,但不允许“我是一个人”。

我不知道您的确切需求,但是您可以考虑使用LUIS和/或QnAMaker作为其他选择。

最后,我包括contactRelationUpdate和firstRun作为机器人介绍的选项。 contactRelationUpdate仅在首次将漫游器添加到Skype(或将其删除并重新添加)时运行。只有用户首次与机器人互动时,firstRun才会运行。

希望有帮助!

var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

// Setup Restify Server
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 connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
    appId: process.env.MicrosoftAppId,
    appPassword: process.env.MicrosoftAppPassword,
    openIdMetadata: process.env.BotOpenIdMetadata
});

// Listen for messages from users 
server.post('/api/messages', connector.listen());

var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);

// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);

bot.on('contactRelationUpdate', function (message) {
    if (message.action === 'add') {
        name = message.user ? message.user.name : null;
        var reply = new builder.Message()
            .address(message.address)
            .text("Hello %s... Thanks for adding me. Say 'hello' to see some great demos.", name || 'there');
        bot.send(reply);
    } else {
        // delete their data
    }
});

// Add first run dialog
bot.dialog('firstRun', function (session) {    
    session.userData.firstRun = true;
    session.send("Hi. How may I help you?").replaceDialog('/');
}).triggerAction({
    onFindAction: function (context, callback) {
        // Only trigger if we've never seen user before
        if (!context.userData.firstRun) {
            // Return a score of 1.1 to ensure the first run dialog wins
            callback(null, 1.1);
        } else {
            callback(null, 0.0);
        }
    }
});

bot.dialog('/', function (session) {
    session.send('You said ' + session.message.text);
});

bot.dialog('/matchDialog', [
    function (session) {
        session.send('Looks like you made it');
    }
])
.triggerAction({
    matches: /person/i,
    intentThreshold: 0.8
});