Customising sender_Id

(Abhishak Varshney) #1

It’s been a great thing that rasa community added MongoDB to store conversation. I am feeling beneficial for that but I want to customize sender_Id of stored conversation based on User_Id which I already provided to them. Can you help me how can I do this?

(Souvik Ghosh) #2

What channel are you using?

(Abhishak Varshney) #3

I am using Rest channel for deploying it on my website.

(Souvik Ghosh) #4

you send the input as

{
  "sender": "your-custom-sender-id",
  "message": "Hi there!"
}
(Abhishak Varshney) #5

I want to change that sender_Id equal to user_id for every user without changing it manually. How can I doing it by changing some code lines. I am saving user_id in slot values too.

(Souvik Ghosh) #6

You want to change the key ‘sender_id’ to ‘user_id’ or just map the values of user_id in the tracker_store.

Why would you change the keys? if your sender_id and user_id is the same value, you can’t simply use sender_id instead.

Don’t change the core system instead adapt the system around it

(Abhishak Varshney) #7

I know that it will be better to map sender_id with user_id but my bot application has requirement to change sender_id to user_id. Can you help me with that? and also plz tell me how can I map sender_Id with user_Id.

(Souvik Ghosh) #8

Go to this file

Customise this class

class MongoTrackerStore(TrackerStore):

Update these two methods only

def save(self, tracker, timeout=None):
        if self.event_broker:
            self.stream_events(tracker)

        state = tracker.current_state(EventVerbosity.ALL)

        self.conversations.update_one(
                {"user_id": tracker.sender_id},
                {"$set": state},
                upsert=True)

    def retrieve(self, sender_id):
        stored = self.conversations.find_one({"user_id": sender_id})
        if stored is not None:
            if self.domain:
                return DialogueStateTracker.from_dict(sender_id,
                                                      stored.get("events"),
                                                      self.domain.slots)
            else:
                logger.warning("Can't recreate tracker from mongo storage "
                               "because no domain is set. Returning `None` "
                               "instead.")
                return None
        else:
            return None

and when you pass the tracker_store to your agent

instead of

from rasa_core.tracker_store import MongoTrackerStore

use your custom

from your_custom.tracker_store import MongoTrackerStore

p.s i will highly recommend NOT to do this since in pure IS guidelines adapting external systems is never a good idea but anyway

(Souvik Ghosh) #9

Forgot about these two methods as well

def _ensure_indices(self):
        self.conversations.create_index("user_id")
def keys(self):
        return [c["user_id"] for c in self.conversations.find()]
(Abhishak Varshney) #10

Hey, Thanks for help but from this I am able to equalise senderId with userId but userId stored in slot has a different value and userId/senderId stored in MongoDb database have different values which is equal to previously set senderId. How can I resolve this?

(Abhishak Varshney) #11

@souvikg10 Do you have any alternative approach? It didn’t work for me.

(Souvik Ghosh) #12

You can use SlotSet([userid:senderid])

(Abhishak Varshney) #13

But I already have different userId which I got through API hit. I think there is a confusion I have to set senderId= userId not userId=senderId

(Souvik Ghosh) #14

First of all, i am not 100% sure why the key name is so important in this case, given that you really should abstract your inference logic from your functional one. Don’t use the ML models to abstract functional code for you.

I think the Rasa package here is detecting a sender-id (be it Facebook, Slack, or whatever) automatically filled in by the system.

Now if i understand well, you have a userID that comes from your system and there is a sender id that comes from Facebook let’s say, Can you explain now what do you want to do now?

Can you please give a step by step explanation?

(Abhishak Varshney) #15

I want to update senderId value as userId value. You can say that I want to override the senderId value to userId value.

(Ashukrishna100) #16

@abhishakskilrock @souvikg10 @akelad
You can do this by following method.

from rasa_core.agent import Agent

s_id=user_id

print(“start the conversation”)`

while True:

a=input()

responses=agent.handle_text(a,sender_id=s_id)

for response in responses:

    print(response['text'])`
1 Like
(Oldmonk Aba) #17

Hi @souvikg10 I am using SocketIOChannel along with webchat. I am deploying the chatbot in a website where a particular user logs in I checked the code in socketio.py and saw that session_id is tagged as sender_id. Along with this can I send an additional user_id from the front end itself so that in mongodb the xtra field is added

(Mouna Ben Ayed) #18

Hi @oldmonkABA did you find a way to do that ?

(Oldmonk Aba) #19

webchat has a customData: {“userId”: user},. U will have to use this to handle the sender id