"TypeError: must be str, not dict" when using attachments

Hi,

I am using the Botfront Webchat UI to render carousels. I simply pass a “Dict” to the attachment in my dispatcher.utter_message (see below).

It works in the Botfront UI but errors out in the rasa shell with the above error i.e.

"Attachment: " + message.get(“attachment”), color=color TypeError: must be str, not dict

If I convert to a string it does not render in the Botfront UI.

Any idea why this is happening?

Thanks

Mark!

        carousel = {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                        "title": "Title1",
                        "image_url": "https://www.worcesternews.co.uk/resources/images/11378712/",
                        "buttons": [{
                            "title": "Hallow",
                            "payload": "hallow",
                            "type": "postback"
                        }]
                    },
                    {
                        "title": "Title2",
                        "image_url": "https://www.worcesternews.co.uk/resources/images/12386979.jpg",
                        "buttons": [{
                            "title": "Bilford Road",
                            "type": "postback",
                            "payload": "bilford"
                        }]
                    }
                ]
            }
        }

        dispatcher.utter_message(response='utter_ask_recycling_center', attachment=carousel)

Since it’s Python, try f-strings:

f"Attachment: {message.get('attachment')}"

This is an error I’m getting in the Rasa Shell. I can’t change that (unless I start changing Rasa code).

However, after more investigation, I think I should be using “json_message” in my dispatcher.utter_message. and not “attachment” i.e. I think it’s a bug in the Botfront RASA UI not picking up the correct attribute.

1 Like

@pomegran can you please update this?

dispatcher.utter_message(template='utter_ask_recycling_center', attachment=carousel)

@pomegran can you share the screenshot with me? and even share color=color code where you have mentioned this?

Hi @nik202

The code I use is in my first post. This works with the Botfront UI. When I run this in “rasa shell” I get this error:

2021-10-28 23:30:09 ERROR    asyncio  - Task exception was never retrieved
future: <Task finished coro=<configure_app.<locals>.run_cmdline_io() done, defined at /home/pomegran/rasa270/rasa270env/lib/python3.6/site-packages/rasa/core/run.py:131> exception=TypeError('must be str, not dict',)>
Traceback (most recent call last):
  File "/home/pomegran/rasa270/rasa270env/lib/python3.6/site-packages/rasa/core/run.py", line 137, in run_cmdline_io
    sender_id=conversation_id,
  File "/home/pomegran/rasa270/rasa270env/lib/python3.6/site-packages/rasa/core/channels/console.py", line 172, in record_messages
    text = get_user_input(previous_response)
  File "/home/pomegran/rasa270/rasa270env/lib/python3.6/site-packages/rasa/core/channels/console.py", line 97, in get_user_input
    button_response = print_bot_output(previous_response, is_latest_message=True)
  File "/home/pomegran/rasa270/rasa270env/lib/python3.6/site-packages/rasa/core/channels/console.py", line 70, in print_bot_output
    "Attachment: " + message.get("attachment"), color=color
TypeError: must be str, not dict

I honestly think the Botfront Chatbot needs to be modified to pick up “json_message” and not “attachment”.

It’s not a big problem. I have coded around this (I can detect using metadata if an input comes from the Botfront UI or not) and used the appropriate “attachment” or “json_message” attribute appropriately.

I changed from “response” to “template” by the way and the error is identical.

Regards

Mark