In my recent work, I was trying to pass additional information as metadata on socketio channel.
Unfortunately, I was not able to see metadata coming through.
On further investigation, I noticed that metadata is not passed for socketio as it does in REST channel.
If you are using rasa 1.10.x, you can try as below.
Implement the get_metadata method in the InputChannel class
def get_metadata(self, request: Request) -> Optional[Dict[Text, Any]]:
"""Extracts additional information from the incoming request.
Implementing this function is not required. However, it can be used to extract
metadata from the request. The return value is passed on to the
``UserMessage`` object and stored in the conversation tracker.
Args:
request: incoming request with the message of the user
Returns:
Metadata which was extracted from the request.
"""
# pass
metadata = request.json.get("metadata", None)
return metadata
And in the socket.io, update the handle_message as below.
async def handle_message(sid: Text, data: Dict) -> Any:
output_channel = SocketIOOutput(sio, self.bot_message_evt)
if self.session_persistence:
if not data.get("session_id"):
raise_warning(
"A message without a valid session_id "
"was received. This message will be "
"ignored. Make sure to set a proper "
"session id using the "
"`session_request` socketIO event."
)
return
sender_id = data["session_id"]
# Adding metadata to message.
metadata = data["metadata"]
else:
sender_id = sid
message = UserMessage(
data["message"], output_channel, sender_id, input_channel=self.name(), metadata=metadata
)
await on_new_message(message)
return socketio_webhook