In custom action return utter with elements

Hi I was not able to return a utter with extra information as elements, however, I was not able to find how. Any one can give me an example

what kind of custom elements?

Hi I tried to use utter_custom_element to send information to the front end for example

elements=[ {“page”: “abc”, title:“xxxx”, “id”: 1 } ]

dispatcher.utter_custom_message( elements)

I do not have any error, but I cannot see the information

You have to extract them in your front end and do something with them

1 Like

hey @akelad i tried same thing but i din’t found any response in my client side(front end),i have tested it in postman,but i am not able to get the response i have attached the screenshot of my code:

Try dispatcher.utter_custom_message(*elements=elements)

hey @ruslan thanks for the suggestion but it dint worked

Hey @JiteshGaikwad. Glad you solved this :slight_smile:

Is there anyway to implement this without changing the source code? I am also using a non python backend and I am having trouble getting elements to work and return custom information

@mickeybarcia it should work if you have an elements array like this (pseudo-code):

elements = [
    {
        "title": "some title",
        "buttons": [ maybe some buttons ]
    },
    { maybe another element }
] 

And then you have to use utter_custom_message like this:

dispatcher.utter_custom_message(*elements)

However, you’ll need to look into your channel’s send_custom_message method, e.g. Facebook’s method looks like this:

def send_custom_message(self, recipient_id, elements):
    # type: (Text, List[Dict[Text, Any]]) -> None
    """Sends elements to the output."""

    for element in elements:
        self._add_postback_info(element['buttons'])

    payload = {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": elements
            }
        }
    }
    self.messenger_client.send(payload,
                               self._recipient_json(recipient_id),
                               'RESPONSE')

So the elements you pass to utter_custom_message are wrapped in a payload JSON object, which will then look like this:

{
    "attachment": {
        "type": "template",
        "payload": {
            "template_type": "generic",
            "elements": elements = [
			    {
			        "title": "some title",
			        "buttons": [ maybe some buttons ]
			    },
			    { maybe another element }
			]
        }
    }
}

And that’s what your frontend should receive.

2 Likes

Thanks for the reply @smn-snkl! However I am not using python to run my actions. So if I respond to the “/nlg” call rasa makes with a bot response:

{
    "text": "hello",
    "elements": [
        {
            "title": "custom msg object",
            "buttons": [],
            "myMsgObject": {}
        }
    ]
}

and then my send_custom_message returns this, then will the custom object be the response to the request I make of the user message to “webhooks/rest/webhook”? It will just be that exact bot response?

@mickeybarcia responded to you on your thread: Confused how elements /custom message objects work

How did you solved it can you share your solution please? I didn’t get any error even log shows that response sent from system but it is not displayed on messenger.

hey @abhishakskilrock the solution which i found works for custom UI, i’m not sure about Messenger whether it will work or not anyway i will upload that code to git soon. :wink:

Sure can you share your git link so that I can take help from it.

Hey can you please share the solution.

Hey I’m still getting same issue. Can you please help

@JiteshGaikwad I also used the same method utter_custom_message(). But, this method only accepts “title, subtitle, image_url, buttons” as object properties. How can we send custom properties or payload?? Pls help. A lot of ppl are looking for this solution.

hey @gokulanv @Purushottam sorry for the late reply, you can read this post might be this can help you:

Sending Custom JSON Payload from actions(workaround for utter_custom_message)

Thanks @JiteshGaikwad , I have implemented the same previously. I thought that we could use something like utter_response by extending the Dispatcher class and using that to utter custom payload, as this looks cleaner to me. You have any idea how that can be done?