How to send multiple attachments (video and chart) from custom action?

Hi all,

I wanted to send multiple attachments within one custom action but it will only work if I send one at a time… Is there a way to make this possible?

Here is my custom action:

class ActionCalculateFoodEmission(Action):
    def name(self):
        return "action_calculate"
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:


        result_message = calculate(category, name, freq)

        data={ "title": "Title", "labels": [ name1, name2, name3, name4, name5 ], "backgroundColor": [ "#36a2eb", "#ffcd56", "#ff6384", "#009688", "#c45850"], "chartsData": [ serv, serv2, serv3, serv4, serv4], "chartType": "horizontalBar", "displayLegend": "false" }

        message={ "payload": "chart", "data": data }


        msg = { "type": "video", "payload": { "title": "Link name", "src": "https://youtube.com/embed/9C1Km6xfdMA" } }

        dispatcher.utter_message(text=result_message, json_message=message)  
        dispatcher.utter_message(text="", attachment=msg) 
        return []
  1. What is the response? Are you getting any errors?

  2. Have you tried the below implementation?

    dispatcher.utter_message(text=result_message, json_message=message, attachment=msg)

Hi @rasa_learner, Thanks for your response!

I just get the text and the chart but not the video. I also tried it the way you did but unfortunately, it’s not working as if there would be a limit for attachments.

I’m using this web chat: GitHub - JiteshGaikwad/Chatbot-Widget

Is it maybe because of this?

Hey, can you once check if the “video type” is working properly by commenting out this part?

     # dispatcher.utter_message(text=result_message, json_message=message)  
     dispatcher.utter_message(text="", attachment=msg)

Hi @rasa_learner,

yes it is, so always when I use just one of the two it works for some reason as if there would be a limit for attachments.

Also, when I took the video out of custom actions and put it into an utterance in the domain, then training the story with:

action: action_calculate action: utter_video

it did not work…

Can you try the following?

from rasa_sdk.events import FollowupAction

class ActionCalculateFoodEmission(Action):

    def name(self):
        return "action_calculate"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        result_message = calculate(category, name, freq)
        data={ "title": "Title", "labels": [ name1, name2, name3, name4, name5 ], "backgroundColor": [ "#36a2eb", "#ffcd56", "#ff6384", "#009688", "#c45850"], "chartsData": [ serv, serv2, serv3, serv4, serv4], "chartType": "horizontalBar", "displayLegend": "false" }
        message={ "payload": "chart", "data": data }

        dispatcher.utter_message(text=result_message, json_message=message)  
  
        return [FollowUpAction(name="action_calculate_new")]

register new action “action_calculate_new” in the domain

class ActionCalculateFoodEmissionNew(Action):

    def name(self):
        return "action_calculate_new" 

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        msg = { "type": "video", "payload": { "title": "Link name", "src": "https://youtube.com/embed/9C1Km6xfdMA" } }
        dispatcher.utter_message(text=result_message, json_message=message)  

        return []

@rasa_learner Thanks, I will try this out!

However, I know that using FollowupAction is not so much recommended when I have actions after the intents and action in my story after the action “action_calculate” because it will make the prediction for the policies pretty hard as the action_calculate_new was never in a story. So do you have any alternatives?

Actually, these are not the limitations of Rasa but the Web application (same with Telegram). You’ll find all the expected responses if you check in debug mode. So, it’s up to you to decide if you want to alter the behavior of the bot or the Web application.

Ah I see… Okay that are very helpful information. Then I will try my luck with altering the web application. And if that won’t work I will use your solution :slight_smile:

Cheers, wish you well :slight_smile:

1 Like