botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Skype chat bot using node SDK with tree-model - done.js - choice.trim is not a function

Open mukeshps25 opened this issue 9 years ago • 5 comments

I've done something like this:

var TreeModel = require('tree-model');
var tree = new TreeModel();

treeName = tree.parse({
    "id": 1,
    "title": "menu",
    "children": [
        {
            "id": 11,
            "title": "company _ data",
            "children": [{"id": 111}]
        },
        {
            "id": 12,
            "title": "adhoc data test ",
            "children": [{"id": 121}, {"id": 122}]
        },
        {
            "id": 13,
            "title": "quit",
            "children": [{"id": 131}, {"id": 132}]
        }
    ]
});

var node1 = treeName.first(function (node) {
    return node.model.id === 1;
});
bot.dialog('/menu', [
    function (session, args) {
        session.send('node place:'+node1.model.title);
        var firstChild = [];
        for (var i = 0; i < node1.model.children.length; i++) {
            firstChild.push(node1.model.children[i].title);
        }
        builder.Prompts.choice(session, "What scenario would you like to run? ",firstChild );
    },
    function (session, results) {
        if (results.response && results.response.entity != '(quit)') {
            // Launch demo dialog
            session.beginDialog('/' + results.response.entity);
        } else {
            // Exit the menu
            session.endDialog();
        }
    }
]).reloadAction('reloadMenu', null, {matches: /^menu|show menu/i});

If I go with the id firstChild.push(node1.model.children[i].id); it will work. if I change id to title it will not work. I get:

$ node start.js
restify listening to http://[::]:3978
/home/marcus/advbot/node_modules/promise/lib/done.js:10
      throw err;
      ^

TypeError: choice.trim is not a function
    at /home/marcus/advbot/node_modules/botbuilder/lib/dialogs/EntityRecognizer.js:158:32
    at Array.forEach (native)
    at Function.EntityRecognizer.findAllMatches (/home/marcus/advbot/node_modules/botbuilder/lib/dialogs/EntityRecognizer.js:156:49)
    at Function.EntityRecognizer.findBestMatch (/home/marcus/advbot/node_modules/botbuilder/lib/dialogs/EntityRecognizer.js:143:40)
    at findChoice (/home/marcus/advbot/node_modules/botbuilder/lib/dialogs/Prompts.js:38:50)
    at SimplePromptRecognizer.recognize (/home/marcus/advbot/node_modules/botbuilder/lib/dialogs/Prompts.js:84:28)
    at Prompts.recognize (/home/marcus/advbot/node_modules/botbuilder/lib/dialogs/Prompts.js:147:36)
    at Session.recognizeCurDialog (/home/marcus/advbot/node_modules/botbuilder/lib/Session.js:468:20)
    at Session.routeMessage (/home/marcus/advbot/node_modules/botbuilder/lib/Session.js:430:18)
    at next (/home/marcus/advbot/node_modules/botbuilder/lib/Session.js:49:23)

My bot app no longer working for the skype client for my account. I have to create the another account on skype client to check. Can you pls guide me what is wrong...!!

mukeshps25 avatar Dec 12 '16 04:12 mukeshps25

Hello,

I just tried a small sample based on the code you posted and got it to work. Please see the code below. This code is just a prototype to use as a guide, and not production-ready code, but it shows how to call a dialog based on your tree structure as you were trying to do.

var restify = require('restify');
var builder = require('botbuilder');
var TreeModel = require('tree-model');
var tree = new TreeModel();

treeName = tree.parse({
    "id": 1,
    "title": "menu",
    "children": [
        {
            "id": 11,
            "title": "company _ data",
            "children": [{"id": 111}]
        },
        {
            "id": 12,
            "title": "adhoc data test ",
            "children": [{"id": 121}, {"id": 122}]
        },
        {
            "id": 13,
            "title": "quit",
            "children": [{"id": 131}, {"id": 132}]
        }
    ]
});

var node1 = treeName.first(function (node) {
    return node.model.id === 1;
});
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url); 
});
  
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());

bot.dialog("/", [
    function(session) {
         session.beginDialog('/menu');
    }
]);

bot.dialog('/menu', [
    function (session, args) {
        session.send('node place:'+node1.model.title);
        var firstChild = [];
        for (var i = 0; i < node1.model.children.length; i++) {
            firstChild.push(node1.model.children[i].title);
        }
        builder.Prompts.choice(session, "What scenario would you like to run? ",firstChild );
    },
    function (session, results) {
        if (results.response && results.response.entity != '(quit)') {
            // Launch demo dialog
            session.beginDialog('/' + results.response.entity);
        } else {
            // Exit the menu
            session.endDialog();
        }
    }
]).reloadAction('reloadMenu', null, {matches: /^menu|show menu/i});

bot.dialog("/company _ data", [
    function(session) {
         session.endDialog('/company _ data');
    }
]);

bot.dialog("/adhoc data test ", [
    function(session) {
         session.endDialog('/adhoc data test ');
    }
]);

bot.dialog("/quit", [
    function(session) {
         session.endDialog('/quit');
    }
]);

Hope this helps you! Let us know if you have further questions.

carlosscastro avatar Dec 16 '16 21:12 carlosscastro

@carlosscastro what was the issue? The only thing I noticed in looking through the original code was that not all of the children had titles.

Stevenic avatar Dec 17 '16 19:12 Stevenic

@Stevenic I actually didn't run into any problem, maybe the problem was in code that was not shared in the issue. However I wanted to share a complete prototype so that they could get unblocked :)

carlosscastro avatar Dec 17 '16 20:12 carlosscastro

@Stevenic @carlosscastro Thanks for your reply Shared code is the same code where I'm facing the issue. Now its running for my new skype account but not the previous account.

  1. I have abc skype account. When I run the code with the firstChild.push(node1.model.children[i].id); it will work.
  2. If we stop the node (by Ctrl + C) and again change the line firstChild.push(node1.model.children[i].title); then its show the above mention error ^throw err;
  3. for now I will rollback all my code to firstChild.push(node1.model.children[i].id); then its still showing above mention error ^throw err;
  4. Now here I logout with the abc skype account and create another xyz skype account and add bot to my contact.
  5. Run the node file and its work with firstChild.push(node1.model.children[i].title); for new account (xyz) but not to the previous (abc account).

I don't know what going on but it should work to my previous account. is any flag set to my abc account from the bot or anything. which is not related to the my code.

mukeshps25 avatar Dec 19 '16 10:12 mukeshps25

Nice! Thanks... Closing this issue for now.

Stevenic avatar Feb 23 '17 00:02 Stevenic