I am trying to pass user access token via scoketio and want to get in actions for further API calls, but not able to do that. Any help would be appreciated, thank you!
Hi @yprashant!
How are you passing the token (like what have you tried?) ?
Hi @saurabh-m523,
I tried with the socket emit user_uttered and passing the message, session_id and access_toke as payload. we can catch token into socketio of rasa but not able to add same as extra param in tracker to get in the actions.
Please let me know if you have any solution to pass extra param from front end to actions.
Hi @yprashant!
Sorry for the late reply.
You can customize the socketio channel. You can then catch the token and then pass it as metadata to rasa by editing this line like this:
message = UserMessage(
data["message"], output_channel, sender_id, input_channel=self.name(), metadata = data["access_token"]
)
This will be now added to the tracker and you can extract it from the tracker in your actions with something like this:
def get_access_token(tracker: Tracker):
for event in reversed(tracker.events_after_latest_restart()):
if event.get('event') == 'user' and 'metadata' in event:
access_token = event.get('metadata')
return access_token
Hope that helps.
Hey @saurabh-m523,
It’s really helped and solved my problem. You are savvier . Presently I tried the solution by making changes in rasa core channel and it’s worked.
I was thinking to make it as custom channel and tried also, but not sure where to keep custom channel and connection, still looking on that.
Thank you so much for taking you precious time to give solution.
You are welcome
You can keep your custom channel file in the same directory where your credentials.yml
file is.
Let’s say your custom channel filename is custom_channel.py
and the name of the class which extends the InputChannel
class is SocketIOInput
. Then you can specify your custom channel in the credentials.yml
file like this:
custom_channel.SocketIOInput:
user_message_evt: user_uttered
bot_message_evt: bot_uttered
session_persistence: true
Hey @saurabh-m523,
I tried same but faces errors because I was missing to add socketIOInput further to custom channel. Now its working perfectly.
Thank you buddy.
Hi @saurabh-m523. I did customise the socketio channel. and im able to print it. But my query is i want to send emp_number data which is present in session data in my website. Then pass it on to the api body using customData. Also in customData what should my key-value pairs be? I’m talking about getting them dynamically. Suppose in my website its loggedinEmpNumber and in my api its emp_number. So its should be like? customData : {“emp_number”:“loggedinEmpNumber”} will this mean the number of employee in session be taken in here loggedinEmpNumber and passed to emp_number?
Hi @shanrock, While calling the API you need to pass key-value pair which is defined by API. So if your API expecting key i.e “emp_number” and in your website if it is like “loggedinEmpNumber” then the format which you said customData : {“emp_number”:“loggedinEmpNumber”} is correct one.
In my API its called as emp_number the key. i understood so far. But now the question lies. How can i assign the customData to the API? like i’m able to print the metadata. Now how do i call this in my api. like how do i reference it.
Also how do i send back the variable data to my API to be replaced in the body.