node.jsstringif-statementbotframeworkskype-bots

Compare string with user input from skype chat


So I wanted to create a skype bot that replies to certain phrases sent to me. A particular phrase or text will have a different reply. The problem is that I am stuck with comparing the user input with the particular string. For some reason the string parameter that I fetch from the chat input is not a string variable as any form of string operation does not work on it although typeof shows its a string.

I am coding the bot using node.js and using the Bot Framework emulator to test it.

Following is the code sample:

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);
});

If I initialize a new string var inside the program i.e comp1 and use it as below to compare then it works, so this rules out that my if statement is wrong.

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

Solution

  • BotBuilder has the .triggerAction that I think would serve you better (docs here). You would create separate dialogs to manage the flow from matches that are made. Additionally, you can set a threshold (score) to determine how close of a match you want to make.

    In the example below, the secondary dialog is triggered when the word 'person' is mentioned. The 0.8 threshold allows 'person' and 'a person' but not 'I'm a person'.

    I don't know your exact needs, but you may consider using LUIS and/or QnAMaker as other options.

    Lastly, I included contactRelationUpdate and firstRun as options for bot introduction. contactRelationUpdate runs only when the bot is first added to Skype (or is deleted and re-added). firstRun is only when the user first interacts with the bot.

    Hope of help!

    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
    });