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:
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 ):
@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.
@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.
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),
]