Skip to content

Instantly share code, notes, and snippets.

@Samarth26
Forked from PurpleBooth/README-Template.md
Last active July 9, 2018 04:18
Show Gist options
  • Save Samarth26/b4d91745194086c56ac327a0b36139fb to your computer and use it in GitHub Desktop.
Save Samarth26/b4d91745194086c56ac327a0b36139fb to your computer and use it in GitHub Desktop.
Implementing Wit.Ai

Wit.Ai implementation

Implementing Wit.Ai with Azure for faster responses.

Getting Started

Wit.Ai and sign into singaporeis and Azure.

Prerequisites

Credentials for singaporeis and Azure. Node.js.

Working with wit

How to add intents to wit

Start by typing the sentence. Add the intent where it says add new entity. Create a new Value by typing the name.

screen shot 2018-07-09 at 11 13 32 am

How to add Entities

Start by typing the sentence. Highlight a part of a sentence or a word which will be used to understand the context. Start by creating a unique name for the entity and then just confirm.

screen shot 2018-07-09 at 11 22 32 am

Working with Azure

How to send the query to the correct intent.

Enter bot_setup.js and the following code allows to match the query to the correct intent.

bot.dialog('doSomeIntent', [dialog_intent.Query, dialog_intent.Results]).triggerAction({
  matches: 'intent_name'
});

Dialog File

Enter the working dialog file or create a new one.

Prerequisite

Basic starting requirements.

var builder = require('botbuilder');
var functions_refined = require('../functions_refined/code');
var fs = require('fs');

Use module.exports.Query to match entities in Azure.

module.exports.Query = function (session, args, next) { 
        fs.readFile('./static/qnamaker.txt', 'utf8', function (err, data) { //If you are using qnamaker.txt
        if (err) throw err;
        list = data.split('\n');
        list.forEach(function (value) {
            splitlist.push(value.split("[;]"));
        });
        var entityReception = builder.EntityRecognizer.findEntity(args.intent.entities, 'visitor');
        next({ entityReception: entityReception});
        var entityDepartmentReception = builder.EntityRecognizer.findEntity(args.intent.entities, 'department');
}

Use module.exports.Requests to respond to the query.

module.exports.Results = function (session, results) {
    //Incase visitorReception is empty. 
    var visitorReception = '';
    try {
        console.log(results.entityReception);
        if (results.entityReception == null) {
            visitorReception = results.response;
            console.log(results.response);
        }
        else {
            visitorReception = results.entityReception.rawEntity.value.toLowerCase();
        }
    new Promise((resolve, reject) => {
    splitlist.forEach(function (value) { //If using qnamaker.txt
                if (session.message.text == value[0] || value[0].includes(visitorReception)) {
                    sendMessage(session, value[1]);
                    resolve(true);
                    throw new Error("Done");
                }
                else if (!(departmentReception == undefined)) {
                    sendMessage(session, `To get to ${departmentReception}. You can fill out the Virtual Management System on my touch screen or you will have to contact the reception with the number 6592 2727 ${departmentReception}`);
                    resolve(true);
                    throw new Error("Done");
                }
                });
            reject(false);
        }).then((res) => {
            console.log(res);

        }).catch((err) => {
            sendMessage(session, "Default Reception Faliure Message");
        });
    }
    catch (err) {
        //do nothing for now 
        session.endDialog();
        session.clearDialogStack();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment