Bot Composition

Bot Composition

To get started with InterBot, lets understand the operators

Operator Operator Name Definition
@ Dereference

This operator is used to dereference a Bot

Usage : @A where A is Bot.

= assign

This operator is used to compose a composite bot. User's can assign a botname to a concatenated set of bots.

Usage: @C = @A | @B

Here, @C is a composite bot of 2 bots A & B concatenated serially.

| IO

The pipe operator is an input-output operator. This operator is used concatenate bots serially, where the input to the next bot is the output of the previous bot.

Usage: @D = @A | @B | @C

Bot D is a composed of Bot A, B & C.

Let's walkthrough an example of how to use the operators and create a bot on InterBot

Assume you have an existing online pizza bot that understands English input and responds to its customers in English too. If the Bot had to cater to Spanish speaking audience, then you would have to build a separate Spanish pizza bot for your Spanish customer base.

With InterBot, you can connect 2 bots to your pizza bot, one that translates Spanish input to English output and then the reverse, English input to Spanish output, and voila! You have your Spanish Pizza bot. This is a classic InterBot usecase.

Hence, the Spanish pizza bot will comprise of 3 bots:

  1. @pizzabot (Pizza ordering bot) - This bot the takes the pizza orders in english

  2. @eng2spbot (English to spanish translator bot) - This bot translates english text inputs to spanish text output

  3. @sp2engbot (Spanish to English translator bot) - This bot translates spanish text input to english text output

To compose your Spanish pizza Bot, you would connect the pizza bot between them. The English output of the spanish to English bot would go as input to the pizza bot and the English output of the pizza bot will be input to the English to spanish bot. Hence, your construct would be:

Spanishpizzabot = spanishtoenglish bot + pizzabot + englishtospanish bot

In the InterBot Console, you would type

 >  @spanishpizzabot = @sp2engbot | @pizzabot | @eng2spbot

where @spanishpizzabot is ournew Spanish pizza bot, created on InterBot.

Here's how the messages would flow between the bots.

  1. The @spbot sends the Spanish text message to the @sp2engbot. Let's say the user says 'Hola'

  2. The @sp2engbot takes the Spanish input 'Hola' and translates it to english & gives it to the @pizzabot as 'Hello'

  3. The @pizzabot responds in English with "Size of pizza?" & gives this as input to the @eng2spbot.

  4. The @eng2spbot translates the english input to spanish "tamano de la pizza?" and responds back to the @spanishpizzabot

  5. The @spanishpizzabot passes this message to the user

function MessageHandler(context, event) { } function EventHandler(context, event) { } function HttpResponseHandler(context, event) { } function DbGetHandler(context, event) { } function DbPutHandler(context, event) { }
function MessageHandler(context, event) { var channelName = event.contextobj.channeltype; var senderName = event.senderobj.channelid; context.console.log("refid : "+refid+" ; senderName : "+senderName); var ibcbots = botmap.split("|"); var position = ibcbots.indexOf("@"+senderName); var nextBot = ibcbots[position+1] var lastBot = ibcbots[ibcbots.length-1].replace(/@/g,""); context.console.log("nextBot : "+nextBot); if(channelName == "ibc"){ var refid = event.contextobj.refid; if(position < 0 && senderName!=botName){ //IBC Init Point if(!context.simpledb.botleveldata[refid]){ context.simpledb.botleveldata[refid]=senderName; context.simpledb.saveData(sendIBCMessage(context,ibcbots[0].replace(/@/g,""),refid,event.message)); }else{ delete(context.simpledb.botleveldata[refid]) return; } }else if(nextBot){ sendIBCMessage(context,nextBot.replace(/@/g,""),refid,event.message); }else{ recBotname = context.simpledb.botleveldata[refid]; try{ var userContext = JSON.parse(recBotname); delete context.simpledb.botleveldata[refid]; context.simpledb.saveData(sendMsg(context,recBotname,event.message)); }catch(err){ delete context.simpledb.botleveldata[refid]; context.simpledb.saveData(sendIBCMessage(context,recBotname,refid,event.message)); } } }else{ var _event = event; var _context = context; generateRefid(function(context,event){ var refid = String(event.getresp); context.simpledb.botleveldata[refid] = JSON.stringify(_event.contextobj); context.simpledb.saveData(sendIBCMessage(context,ibcbots[0].replace(/@/g,""),refid,_event.message)); }); } } function generateRefid(cb){ var url = envurl+botName+'/refid'; var headers = { 'content-type': 'application/x-www-form-urlencoded', 'apikey': apikey } context.simplehttp.makePost(url, "", headers,cb); } function sendIBCMessage(context,recipientName,refid,message) { var url = envurl+botName+'/sendmsg'; var headers = { 'content-type': 'application/x-www-form-urlencoded', 'apikey': apikey } var formData = 'refid=' + refid + '&message=' + message+'&destbotname='+recipientName; context.simplehttp.makePost(url, formData, headers); } function sendMsg(context,userContext,message){ var url = botenvurl+botName+'/msg'; var headers = { 'content-type': 'application/x-www-form-urlencoded', 'apikey': apikey } var formData = 'context=' + userContext + '&message=' + message+"&bypass=true"; context.simplehttp.makePost(url, formData, headers); } /** Functions declared below are required **/ function EventHandler(context, event) {} function HttpResponseHandler(context, event) {} function DbGetHandler(context, event) {} function DbPutHandler(context, event) {}