Rasa Webchat || get userID from script

Hi , I want to use the userID from below script to make API calls I want to run the chatbot after the user login.

i am using :GitHub - botfront/rasa-webchat: A feature-rich chat widget for Rasa and Botfront

index.html (950 Bytes)

HI @Aashay1, can you please add more description of what you’re trying to achieve and where you’re getting stuck?

1 Like

hi @erohmensing , I have created a bot to ans different leave balance . For now I have hardcoded the used ID , But I want to fetch the userID from custom data within the rasa-webChat script

div id=“webchat”/ script src=“https://storage.googleapis.com/mrbot-cdn/webchat-latest.js”></script script WebChat.default.init({ selector: “#webchat”, initPayload: “/get_started”, customData: {“language”: “en”, "userID ":“1234”}, // arbitrary custom data. Stay minimal as this will be added to the socket socketUrl: “http://localhost:5005”, socketPath: “/socket.io/”, title: “Title”, subtitle: “Subtitle”, }) script

I dont want to ask userID , I want to used userID directly in actions.py file

Thank you Aashay

1 Like

You don’t need to do anything fancy to get the sender id :slight_smile:

A random sender ID is assigned to each user, you don’t have to define one in the customData. To access that sender id in your actions file, you can get the tracker.sender_id. More info here

1 Like

hey @erohmensing, can you please check this thread, I would like to know your thoughts on this because if I try to extract the sender_id using tracker.sender_id I get some randomly generated number but If I pass the userId in customData from the rasa-webchat I can easily get the proper useId since I didn’t find a proper way where I can directly set the userId from the front-end :sweat_smile:

The EmployeID is added to custom data from webpage once the user login. This employeId is unique and , i need to use it in action to run api calls

1 Like

Gotcha. As far as I can see, the customData is added to the socket but is not handled by the rasa connector. Though that is something you may want to confirm with botfront (I don’t know much about how they format the request to rasa internally).

You could subclass the socket connector and rewrite this function to make use of the customData and send it as metadata in the UserMessage. (Custom Connectors)

Hey @erohmensing, I have rewritten the function and it works :slight_smile:

socketChannel_RasaBot/socketChannel.py at e4db7fe61ab69a56e1a6e751ca4ffd906a331647 · JiteshGaikwad/socketChannel_RasaBot · GitHub

Thanks

2 Likes

Aha, perfect @JiteshGaikwad!

2 Likes

Thanks @erohmensing :blush:

Hey @JiteshGaikwad,

Do you have a git repo where i can see how its implemented ?

Hey @Aashay1, ya I have it, check it out here:

https://github.com/JiteshGaikwad/socketChannel_RasaBot.git

hi @JiteshGaikwad,

I am getting empty metaData

Hi Guys,

I am able to solve, How to pass Custom data and set slot with rasa web

#################### you html webchat scrip #####################

div id=“webchat” script src=“https://storage.googleapis.com/mrbot-cdn/webchat-latest.js”></script // Or you can replace latest with a specific version script WebChat.default.init({ selector: “#webchat”, initPayload : ‘/get_started{“userID”:“1”}’, customData:{}, // arbitrary custom data. Stay minimal as this will be added to the socket socketUrl: “http://localhost:5005”, socketPath: “/socket.io/”, title: “Title”, subtitle: “Subtitle”, }) /script

#################### your actions.py ############# from future import absolute_import from future import division from future import unicode_literals

from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher

from rasa.core.actions.action import Action from rasa.core.events import SlotSet from rasa_sdk import Tracker from rasa_sdk.executor import CollectingDispatcher

from typing import Dict, Text, Any, List import requests from rasa_sdk import Action from rasa_sdk.events import SlotSet, FollowupAction from rasa_sdk.forms import FormAction

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher from rasa.core.events import UserUttered

from rasa_core.trackers import DialogueStateTracker

class GetLeaveBalance(Action):

def name(self):
    return 'get_leave_balance'


def run(self, dispatcher, tracker, domain):
    
    
    #importing librires to connect with wsdl 
    from suds.client import Client
    from suds.sax.element import Element
    
    userID = tracker.get_slot("userID")
     
    print("userID :",userID)

Thank you

1 Like

is this possible to get sender id, firstname, lastname, username and phone number from telegram bot without asking the user? Coz. when I tried to store conversation using tracker store, I was able to get sender id? And there is a bot caled uerinfobot, by which they provide our firstname, lastname and id. Is this possible to get that on rasa? Please let me know.

1 Like

Hello , I used the below to send user id to rasa :

class ActionHelloWorld(Action):

def name(self) -> Text:
    return "action_hello_world"

def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

    
    employee.Email = tracker.latest_message['metadata']['email'].lower()
    print(tracker.latest_message['metadata']['title'].lower())

  
    print(tracker.get_latest_input_channel())

    return [] 

success: function(response){ WebChat.default.init({ selector: “#webchat”, initPayload: “/get_started”, customData: {“language”: “en”, “email”: response[“Email”], “title”: response[“Title”]}, // arbitrary custom data. Stay minimal as this will be added to the socket socketUrl: rasa_url, socketPath: “/socket.io/”, title: “Test”, subtitle: “Hello! I am Digital Employee :)”, params: {“storage”: “local”} // can be set to “local” or “session”. details in storage section. }); }

you just added userID with payload and accessed in actions.py file? Right?