How to change replace customData.userId with session_id / sender_id?

Hi @Juste @JiteshGaikwad

Everything working fine with my BOT and I am very happy with it.

But, I am unable to retrieve and store customeData from web-UI. I can’t fine any useful answer for me. No one explain where I need to write code for it ! In webchat-UI documentation…

" If you want to process customData in Rasa Core you have to create a new channel. Use channel rasa_core.channels.socketio as a template for your new channel. In such channel customData can be retrieved via data['customData'] . Then you can modify sender_id , save customData to the database, fill slots or whatever you need to with your additional data."

What that mean ? Please anyone can explain ?!! in my credentials.yml file

socketio:
  user_message_evt: user_uttered
  bot_message_evt: bot_uttered
  session_persistence: false

already added. I think it’s a costume channel. Right ? What I need to do, to store “userId” with “sender_id” and in which file I should write a code. So, I can differentiate conversation with other users. Please explain how to achieve it.

Everything is very simple… Now ! You just need to modify in src/components/Widget’s index.js file. No need to extra editing anywhere.

1st change in this function var:

  trySendInitPayload = () => {
    const {
      initPayload,
      customData,
      socket,
      initialized,
      isChatOpen,
      isChatVisible,
      embedded,
      connected
    } = this.props;

    // Send initial payload when chat is opened or widget is shown
    if (!initialized && connected && (((isChatOpen && isChatVisible) || embedded))) {
      // Only send initial payload if the widget is connected to the server but not yet initialized

      const session_id = this.getSessionId();

      // check that session_id is

      if (!session_id) return
      console.log("sending init payload", session_id)
      console.log("customData is : " + customData.userId);
      socket.emit('user_uttered', { message: initPayload, customData, session_id: customData.userId });
      this.props.dispatch(initialize());
    }
  } 

2nd change in this…

  componentDidMount() {
    const { socket, storage, customData } = this.props;

    socket.on('bot_uttered', (botUttered) => {
      this.messages.push(botUttered);
    });

    this.props.dispatch(pullSession());

    // Request a session from server
    const local_id = this.getSessionId();
    console.log(">>> userId is : " + customData.userId);
    socket.on('connect', () => {
      // console.log("----------------------| local_id : " + local_id);
      socket.emit('session_request', ({ 'session_id': customData.userId }));
    });

    // When session_confirm is received from the server:
    socket.on('session_confirm', (remote_id) => {
      console.log(`session_confirm:${socket.id} session_id:${remote_id}`);

      // Store the initial state to both the redux store and the storage, set connected to true
      this.props.dispatch(connectServer());

      /*
      Check if the session_id is consistent with the server
      If the local_id is null or different from the remote_id,
      start a new session.
      */
      if (local_id !== remote_id) {

        // storage.clear();
        // Store the received session_id to storage

        storeLocalSession(storage, SESSION_NAME, remote_id);
        this.props.dispatch(pullSession());
        this.trySendInitPayload()
      } else {
        // If this is an existing session, it's possible we changed pages and want to send a
        // user message when we land.
        const nextMessage = window.localStorage.getItem(NEXT_MESSAGE);

        if (nextMessage !== null) {
          const { message, expiry } = JSON.parse(nextMessage);
          window.localStorage.removeItem(NEXT_MESSAGE);

          if (expiry === 0 || expiry > Date.now()) {
            this.props.dispatch(addUserMessage(message));
            this.props.dispatch(emitUserMessage(message));
          }
        }
      }
    });

    socket.on('disconnect', (reason) => {
      console.log(reason);
      if (reason !== 'io client disconnect') {
        this.props.dispatch(disconnectServer());
      }
    });

    if (this.props.embedded && this.props.initialized) {
      this.props.dispatch(showChat());
      this.props.dispatch(openChat());
    }
  }

That’s It. Thank you both… And no one answered :smirk: Anyway, thanks. :blush:

2 Likes

hey does this works for current rasa versions?