microsoft / botframework-sdk

Bot Framework provides the most comprehensive experience for building conversation applications.
MIT License
7.5k stars 2.44k forks source link

[Question] Can I programmatically add dialogs to a bot library? #3093

Closed rameshananth closed 7 years ago

rameshananth commented 7 years ago

System Information (Required)

Issue Description

I am currently developing a bot where the dialogs are stored as Javascript hashes and the dialog-intent maps are stored as a JSON objects, and I try and create the dialog entries for these separately, and I am trying to add the dialogs to the library dynamically through a for loop. Is this possible and is it supported?

Code Example

For e.g. in the file :

ServiceDeskLibrary.js

I have the following code:

var gjGetAndDisplayOneTicket={
    name:"MSBotFramework:/CheckPrereqs",
    parameters:{
        check:{
            name:"ServiceNow:/GetTicket",
            parameters:{
                message:null,
                persistResponse:true,
                persistVariable:'Tickets'
            }
        },
        success:{
            name:"ServiceNow:/MakeIncidents",
            parameters:{message:null}
        },
        failure:{
            name:gjNewTicketConv.name,
            parameters:gjNewTicketConv.parameters
        }
    }
};

and later on


var gjConversation=[
{
 dialogName:'/GetATicketFromServiceNow',
 intentName:'ServiceDesk.GetUpdates',
 dialogEntryPoint:gjGetAndDisplayOneTicket
}
]

later on in the same file

ServiceDeskHelper.js

I want to add:


//create library
var lib=new builder.Library('ServiceDesk');

//Create dialogs
for(var i=0;i<gjConversation.length;i++){
   dialogMap=gjConversation[i]
   lib.dialog(dialogMap.dialogName,[
                                 function(session,args,next){
                                                   session.beginDialog(
                                                                                    dialogMap.dialogEntryPoint.name,
                                                                                    dialogMap.dialogEntryPoint.parameters
                                                    );
                                                   session.endConversation();
                                   }
   ]).triggerAction({matches:dialogMap.intentName});
}

And much later in

server.js


bot.library(require('./convs/ServiceDesk/helper').createLibrary());
 

Unfortunately, when I do this and add the library to the bot with a require the intent is not recognized and the bot does not branch to this conversation at all.

Steps to Reproduce the Issue

Expected Behavior

Bot branches to the dialog

Actual Results

Bot does not branch to the dialog at all.

nwhitmont commented 7 years ago

@rameshananth Check out the Contoso Flowers Bot example code and look into the section called Multi-Dialogs Approach - this shows some examples how to bundle dialogs into reusable libraries.

Dialogs can be composed with other dialogs to maximize reuse, and a dialog context maintains a stack of dialogs active in the conversation. In this sample, the main flow is implemented in the shop dialog and it is composed of several other dialogs that may also break-down into sub-dialogs.

Each of these dialogs are implemented as a BotBuilder Library (more information below). The important thing is that each library manages a small step in the flow, and the result of each is passed back to the dialog stack using session.endDialogWithResult().

For more information on the Library class in Node SDK, see: https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.library.html

A library of related dialogs used for routing purposes. Libraries can be chained together to enable the development of complex bots. The UniversalBot class is itself a Library that forms the root of this chain.

Libraries of reusable parts can be developed by creating a new Library instance and adding dialogs just as you would to a bot. Your library should have a unique name that corresponds to either your libraries website or NPM module name. Bots can then reuse your library by simply adding your parts Library instance to their bot using UniversalBot.library(). If your library itself depends on other libraries you should add them to your library as a dependency using Library.library(). You can easily manage multiple versions of your library by adding a version number to your library name.