Initiate conversation

Hello,

You will need to implement that in your frontend (the interface the user will use to talk to the bot, like a web widget or a mobile application):

You will surely have a send(message) function to send the user messages via API. Upon startup of your application, you will just need to automatically send a hidden message or payload as if it’s from the user, which will trigger the action.

For example, if utter_greet is the answer to the intent greet, you could execute send("Hello") or send("/greet") on startup.


In more detail, there are two ways to do this:

  1. In that application, you will have a send() function for sure, to send the user message to the Rasa server when the user clicks on the “send” button.

    You will just need to call this function on startup, which means when the user opens the webpage or the mobile application.

    Let’s say in your domain, the phrase “Hey there, how can I assist you?” is called utter_greet, and in the stories, it is a response to the greet intent.

    So you can send a message with the greet intent, like “Hello” on startup, so when the bot receives it, it will reply “Hey there, how can I assist you?”.

  2. You will also have a display_message() function which you will use to display the message received by the bot.

    You can also call this function on startup, to display the message directly without sending anything first.

There are multiple frontends you can use:

All of them have a feature to do what was mentioned above, except the Rasa Android app (I think).


A small JavaScript code example for solution 1:

$(document).ready(() => {
  send("Hello")
})

function send(message) {
  $.ajax({
    url: rasa_server_url,
    type: "POST",
    contentType: "application/json",
    data: JSON.stringify({ message, sender: sender_id }),
    success(botResponse, status) {
      setBotResponse(botResponse);
    },
    error(xhr, textStatus) {
      setBotResponse('Error');
    },
  });
}

And for solution 2:

$(document).ready(() => {
  display_message("Hey there, I am a bot. How can I assist you?")
})

function display_message(message) {
  $(`<p class="botMsg">${message}</p>`).appendTo(".chats").hide().fadeIn(500);
}
1 Like