Return the message id in the bot answer

Dear,

The default answer of the RASA chatbot is the sender id and the answer text, but I want to return more metadata, like the message id. I saw to do that, it is necessary to develop a channel for each type I want (Rest and SocketIO), if I’m not wrong. But I think it is too complicated for that small thing. Is it already the only way? There is any tutorial to do that?

Thank you very much, José Manuel

@leto_ii Hello, can you confirm to me, you only want message id or whole conversation between bot and user?

I only need the text, the sender id and the message id of a given message, not whole conversation.

You can access it all with the variable tracker inside a Custom Action.

Do print(tracker) and you will see, on the same terminal where you ran rasa run actions, what it contains. You can use it like a normal Python dictionary to get whatever you want.

For example, tracker.latest_message contains, among others, text and message_id. Access them like so:

msg = tracker.latest_message
text = msg['text']
msg_id = msg.message_id
2 Likes

Thanks, it was my first idea but I want for all the messages, not only for the actions ones. Finally I did with the following custom channel:


from mysutils.collections import add_keys
...

@custom_webhook.route("/webhook", methods=["POST"])
        async def receive(request: Request) -> HTTPResponse:
            sender_id = request.json.get("sender") # method to get sender_id
            message = request.json.get("message") # method to fetch text

            collector = CollectingOutputChannel()

            msg = UserMessage(
                    message,
                    collector,
                    sender_id
                )

            await on_new_message(msg)

            return response.json([add_keys(m, message_id=msg.message_id) for m in collector.messages])
1 Like

The tracker works on user inputs, not actions, aka all messages.

Glad you solved it anyway :slight_smile:

1 Like

Amazing response Chris

Hi there! I have a question related with the tracker. I’m doing a bot with rasa and telegram and I want to delete the conversation with the bot when it finishes. Telegram has a function to do so but I need the chat id and the message id. I can get the chat_id using tracker.sender_id but the message id of the sent messages by the user are not correct, and I cannot track the messages sent by the bot. Do you know how could I do that?

Hi José Manuel, I also have the same problem, can you share the solution with me.

In your code, it looks like you are adding keys to each of the bot’s responses. So where are these keys stored? Do you have a custom tracker store to store the keys?

Did you manage to get the correct telegram message id?