wit.ai + slot based bot + save entities values in client

自作多情 提交于 2019-12-09 20:48:59

问题


I am trying out a sample in wit.ai, here is the link : https://wit.ai/Nayana-Manchi/CreditCardApp/stories

The first story "BalanceEnquiry” is a slot based story. The happy scenario works fine. To test “cardnumbermissing” branch, I would type in “I want my credit card balance on the card and my name is Nayana”. Here the card number last 4 digits are missing. It ask for the last 4 digits of the card and then I would enter the last 4 digits of the card. But here it does not get name entity which was sent in earlier message.

How do I save the entity value “name” which was sent in the previous step? It should retain the entity value name and it should get the entered last 4 digits from the current conversation and then display the balance which is hard coded.

Code and the images attached.

It will be helpful if you share the client code (javascript) for the recipe- Build a slot-based bot.

'use strict';

let Wit = null;
let interactive = null;
try {
    // if running from repo
    Wit = require('../').Wit;
    interactive = require('../').interactive;
} catch (e) {
    Wit = require('node-wit').Wit;
    interactive = require('node-wit').interactive;
}

const accessToken = (() => {
    if (process.argv.length !== 3) {
        console.log('usage: node examples/creditcardbalance.js <wit-access-token>');
        process.exit(1);
    }
    return process.argv[2];
})();

// Quickstart example
// See https://wit.ai/ar7hur/quickstart

const firstEntityValue = (entities, entity) => {
    const val = entities && entities[entity] &&
      Array.isArray(entities[entity]) &&
      entities[entity].length > 0 &&
      entities[entity][0].value
    ;
    if (!val) {
        return null;
    }
    return typeof val === 'object' ? val.value : val;
};

const actions = {
    send(request, response) {
        const {sessionId, context, entities} = request;
        const {text, quickreplies} = response;
        return new Promise(function(resolve, reject) {
            console.log('sending...', JSON.stringify(response));
            return resolve();
        });
    },
    getBalance({context, entities}) {
    return new Promise(function(resolve, reject) {
        var name = firstEntityValue(entities, "name");
        var last4digit = firstEntityValue(entities, "Last4digits");
        
        if (name && last4digit) {
            context.name = name;
            context.last4digit = last4digit;
            context.balance = 'Rs.10000' + name + last4digit; // we should call a credit card API here
            delete context.cardnumbermissing;
        }
        else {
            context.cardnumbermissing = true;
            context.name = name;
            delete context.balance;
        }
        return resolve(context);
    });
},
};

const client = new Wit({accessToken, actions});
interactive(client);
// JavaScript source code

回答1:


I had a similar issue and what was happening was firstEntityValue only pulls from the initial conversation, so I was actually erasing previous variables each time I called it to set a variable. I had to make the function aware of the context and not set those variables using firstEntityValue if there was already a context.



来源:https://stackoverflow.com/questions/39446666/wit-ai-slot-based-bot-save-entities-values-in-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!