Sending Custom JSON Response
Normally a bot sends a text message (string) as a response (Refer to the above-given screenshot) but the challenge comes when we have to send some custom response in the form of JSON data to satisfy the needs of the end-users of the bot.
Let’s define a Use case where we will have to use a Custom response and send the JSON data back to the UI channel.
Assume the Rasa-bot stored some user data in between the conversation and now the User wants a list of his/her provided details. Here comes the task of sending Custom Response in the form of JSON data which will help the front-end developer to segregate the response and easily populate the data in the UI.
Below is the Python code to write the Custom Action method, which will retrieve the user stored entities and return the appropriate values:
Python3
class ListUserDetails(Action): def name( self ) - > Text: # Name of the action mentioned in the domain.yml file return "action_list_user_details" def run( self , dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict [Text, Any ]) - > List [ Dict [Text, Any ]]: # It will return array of entities entities = tracker.latest_message[ 'entities' ] # Iterating through the array to retrieve the desired entity for e in entities: if e[ 'entity' ] = = "user_name" : entity_name = e[ 'value' ] elif e[ 'entity' ] = = "city_name" : entity_city = e[ 'value' ] elif e[ 'entity' ] = = "designation" : entity_designation = e[ 'value' ] dispatcher.utter_message( template = "utter_user_details" , name = entity_name, city = entity_city, designation = entity_designation ) return [] |
Written above is a sample custom action Python code of the Rasa chat-bot to perform a particular desired action on understanding the exact intent from the user. Here Rasa NLU takes the user’s message and finds out the correct intent which in the above case is “Listing all the user details”.
After this Rasa calls the custom action (action_list_user_details) associated with the particular intent and performs the task. In the above code we are fetching the user details stored as an entity inside the local memory of the bot such as name, city, and designation with the help of the tracker and sending the fetched result as a single response to the template “utter_user_details” with the help of dispatcher.utter_message( )
The above code will call the “utter_user_details” response mentioned in the domain.yml file with the fetched user details as arguments. Lastly, we have to write the response “utter_user_details” in the domain.yml file using the custom keyword to send a JSON object to the channel as a response to the user’s message. Below is the format of the response:
Following this method, a JSON response with the provided attribute will be sent to the respective output channel. The output of the below code will look similar to this:
Similarly using the Custom response we can send the required data in the JSON format which can be handled well by the front-end developer.
How to send Custom Json Response from Rasa Chatbot’s Custom Action?
Rasa is an open-source Machine Learning framework to automate contextual text-voice-based Assistant. Rasa NLU understands the correct Intent with the help of its pre-trained nlu data. Rasa Core decides what to do next and reply according to in sync with the training stories. It also has the capability to store Entities (Noun type-specific Information stored for future use). By default, it can respond with texts, image links, button objects etc.