Where can i access metadata from Tracker?

hi. i was wondering how and where i can access the metadata from the tracker variable. i used to access it before through:

tracker.latest_message[“metadata”]

i need the metadata so i can get the message id of the inlinekeyboardbutton (telegram) in order for me to update the same message. i used to access it before through:

tracker.latest_message[“metadata”][“callback_query”][“message”][“message_id”]

but now tracker.latest_message has empty metadata {}

did rasa change this? my chatbot hasnt been functioning ever since i updated rasa ): and now the tracker.latest_message[“message_id”] variable gives a different message id when i click the button ):

please help. thank you

1 Like

@ccloud Hey! If I get you correctly, you want to get the session id, conversation and metadata of both bot and user? right

I guess tracker.events with give you all the details of the bot/user conversation or logs.

OR

Do check these codes, I used it before but optimised as per your need.

```
for e in tracker.events[::-1]:
    if e["event"] == "user":
        message_metadata = e["metadata"]
```

OR

chatbot_events = next(e for e in reversed(tracker.events) if e["event"] == "bot")
chatbot_events = next(e for e in reversed(tracker.events) if e["event"] == "user")
chatbot_events = next(e for e in reversed(tracker.events) if e["event"] == "actions")

This will go through the reversed list of events (making it from latest to oldest) and pick the first bot event using the next() function.

Tip: Do remember the metadata are is JSON and if its store in database; even the column still be JSON.

Good Luck!

3 Likes

thank u so much! ill try this :slight_smile:

@ccloud Is my suggestion helped you?

hi! i havent tried this yet but i found a workaround by using get_metadata() function in my custom channel!

@ccloud Ok, No worries. If you able to solve this query, I request close this thread with the solution or share the code you used for this query for other seekers.

Hi @ccloud , how do I use this get_metadata() function?

Working solution Telegram integration with rasa - #2 by fabiolamfleury

Going to post the solution i found:

Implemented a custom input channel in a file called custom_telegram.py the same folder that the credentials.yml is, with the content:

from rasa.core.channels import TelegramInput
from typing import Text

class TelegramInputChannel(TelegramInput):
    def get_metadata(self, request):
        metadata=request.json
        return metadata

Then, edited my credentials.yml using:

custom_telegram.TelegramInputChannel:
  access_token: "token provided by father bot"
  verify: "bot_username"
  webhook_url: "https://address/webhooks/telegram/webhook"

After that, it was possible to access in actions file:

    telegram_username = tracker.latest_message["metadata"]["from"]["username"] #user that sent the message
    telegram_chat_type = tracker.latest_message["metadata"]["chat"]["type"]
    if telegram_chat_type == 'group':
        is_group = True
    return [
                SlotSet("is_in_telegram_group", is_group),
            ]
1 Like