Skip to main content

Request routing bot script

This article is part of a tutorial about creating a multilingual bot.

  1. Basic principles
  2. Request routing (you are here)
  3. Support for English
  4. Support for other languages
  5. Channel deployment

In this section, we will implement a router bot, which will be capable of routing requests from clients to the appropriate monolingual bots.

Log in to Tovie Platform and create a new project, keeping the NLU language and other project settings at their default values. Go to the Editor tab.

Bot descriptor

In accordance with the proposed multilingual bot diagram, the router bot should have access to the following configured properties:

  • The language of the main monolingual bot, which will receive the initial request by default.
  • The message text for replying to requests in unsupported languages.
  • The IDs of all monolingual bots which the context will be switched between.

Configure these properties in the injector section of the chatbot.yaml bot descriptor:

defaultLanguage: en
unknownLanguageMessage: Sorry, this language is not supported.
en: ""
fr: ""

The value of defaultLanguage and the keys in the bots mapping should correspond to ISO codes of languages supported by the bot. Leave the values in the bots mapping empty at this time.

The configured properties will be available from the script via the $injector object.


Create a file named router.js in the src directory. It will contain the JavaScript code implementing the router bot behavior.

function detectLanguage(ctx) {
if (ctx.request.query === "/start") {
return ctx.injector.defaultLanguage;
return $caila.detectLanguage([ctx.request.query])[0];

function redirectToBot(botId, ctx, targetState) {
targetState = targetState || "/Hello";
var params = ctx.session || {};

ctx.response.replies = ctx.response.replies || [];
type: "context-switch",
targetBotId: botId,
targetState: targetState,
parameters: params

function processRequest(ctx) {
var lang = detectLanguage(ctx);
var botId = ctx.injector.bots[lang];

if (botId) {
redirectToBot(botId, ctx);
} else {

All functions given above accept the $context object among their arguments, which represents the context of the current request processing step. This is what each of these functions does:

  • detectLanguage uses the $caila.detectLanguage method to detect the language of the request sent by the client. The only exception is the /start request: in this case, the function returns the default language as set in the bot descriptor.

  • redirectToBot switches the context to the necessary monolingual bot using context-switch. By default, the bot makes a transition to the /Start state after switching. The contents of the $session object are passed as data shared by the bots.

  • processRequest is the generic function used for processing all requests handled by the router bot. It detects the request language and switches the context to the necessary monolingual bot. If the request language is not supported, the bot answers with the appropriate message.

Script code

In the file, import router.js using the require tag and define two states:

  1. The /Request state for handling all requests sent directly to the router.
  2. The /Redirect state, which will be used for routing requests from monolingual bots.
The processRequest function should be bound to both states as a handler.
require: router.js

bind("preMatch", processRequest);
bind("postProcess", processRequest, "/Redirect");

theme: /

state: Request
q!: *

state: Redirect

We will now go on to create our first monolingual bot.