How to get response to a Rasa query sent via REST call to display in Facebook Messenger

Hi - I have a Rasa bot that uses Facebook Messenger as a front end and incorporates webview to show details (images, extended text, etc) that are not possible in native Facebook Messenger.

I have everything working including the basic content in webview, but now I need to allow the user to click on a button in webview to trigger a Rasa query and have the result of the query show up back in native Facebook Messenger, a shown in this diagram

I have a Javascript function in the web page displayed in webview (UserAction(), see below) that successfully sends a query to Rasa via a REST call when the user presses a button in webview. The problem is that while the correct custom action catches the query and returns the correct response (I can see the response in log output), the response does not get displayed in Facebook Messenger. The dispatcher.utter_message statement that displays the response behaves differently depending on how the query was entered:

  • if the query was entered by the user in Facebook Messenger, dispatcher.utter_message displays the result in Facebook Messenger
  • if the query came from a REST call, dispatcher.utter_message does not display the result in Facebook Messenger

I note that in the first case, tracker.get_latest_input_channel() = “facebook”; in the second case tracker.get_latest_input_channel() = “rest”.

Is there any way to control the behaviour of dispatcher.utter_message so that it displays in Facebook Messenger even if the query that invoked the custom action that led to the dispatcher.utter_message came from a REST call as opposed to user input in the Facebook Messenger interface?

function UserAction() {
var xhttp = new XMLHttpRequest();
let json_val = JSON.stringify({
	sender: "default",
	message: <query text for Rasa>
});"POST", '', true);
xhttp.setRequestHeader('Content-type', 'text/plain');


For example, is there a way to set output_channel explicitly in dispatcher.utter_custom_json() or dispatcher.utter_message ?

Or, if that’s not possible, is there a complete example of how to use " Inject an intent into a conversation" REST call (as described in HTTP API)? This API looks like it might allow me to do what I want since it allows you to set output_channel explicitly, but I have not been able to get it to work, and I haven’t been able to find any actual, detailed examples of how to use it.

Hey @ryanmark, I have used the above api which you have mentioned but its deprecated but you can see my code for reference, I have used it in my frontend to greet the user as soon as he opens the chatbot widget.

Thanks for the response @JiteshGaikwad - much appreciated. I hadn’t tried the API call with “execute” in the path as opposed to “trigger_intent”. I can now get a response posting the API call in Postman, but I am still getting the error below. In your code, is “action_name” just a string or is it a nested JSON object?

   action_to_execute = request_params.get("name", None)

AttributeError: ‘NoneType’ object has no attribute ‘get’

To follow up on my original question, thanks to the hint from @JiteshGaikwad I was able to get REST calls working in Postman that set slots and then triggered a custom action:

  1. call to http://localhost:5005/conversations/default/tracker/events to set the slots
  2. call to to actually trigger the custom action

Unfortunately, even though there is an output_channel query parm for the execute call per HTTP API, I was not able to get the results to show up in Facebook Messenger with output_channel = “facebook”.

I’ve spent several days trying to solve this problem the direct way, so now I’m planning to get the intended effect (a click in webview closes the webview window and also launches a carousel in Facebook Messenger) by doing a series of REST calls in the Javascript for the page displayed in webview.

hey @ryanmark, sorry for the late reply, is your problem solved?

Thanks @JiteshGaikwad - the problem with calling REST API to do a turn in a chat is solved, thanks, but the problem of getting the output to appear in Facebook Messenger (when the query is triggered by a REST API call) is not solved.

As a workaround I plan to pass the output of the custom action in Python to Javascript and make a call directly to Facebook Messenger (FM) from there since I have not been able to find a way within Rasa to force the output to go to FM when the query originated from a REST call.

Ok @ryanmark, I could have helped you out but I am not there on Facebook, so can’t tell what’s the exact issue in renedering the messages on FB Messenger :sweat_smile:

1 Like

Thanks - I got what I wanted - smooth transition between webview page and result showing in Facebook Messenger - but it was heavy lifting.

In Javascript in the page showing in webview, I had to implement functions to:

  1. make REST API call to Rasa to set slot value for the action
  2. make REST API call to Rasa to execute action and save & massage output of action to prepare it for step 3
  3. make REST API call to Facebook Messenger using payload created from the output of step 2. This payload is essentially the same as the payload generated by the action in Pyton for the dispatcher.utter_custom_json call

It would be good to have this function (the ability to direct the result of a REST API call to Rasa to a particular output_channel, such as Facebook Messenger) built in to the behaviour of Rasa REST API calls to avoid all this JS code.

Hi @JiteshGaikwad, I have seen you helping out on this forum, I would really appreciate your help on a scenario that I am stuck with. Frontend → Nodejs → Rasa → Nodejs → CRM system. Suppose a user wants to track the status of shipment, Nodejs processes the data in the backend and returns it to Rasa which in turn displays in the frontend. Now it’s up to Rasa to keep the conversation going. For example: if the nodejs checks in the backend that the status = delivered, Rasa should then ask the user if he would like to download the receipt or if status = undelivered, Rasa should ask the user if he would like to raise a complaint. How would Rasa know which direction to go based on the output of the nodejs? Is there a way we can connect these two?