Save user IP address and user agent in database

How to save user IP address and user agent in Botpress sqlite database in srv_channel_users table in attributes column?

Or maybe there is another good way to do this?

Thank you

UPDATE: I used Call API and calling ipinfo.io but I’m getting not user IP but server IP because it’s not on client side, any thoughts how to call it on client side? maybe custom component?

You are right that using the Call API skill will call the api from the Back end as it uses an action behind the scene.

You could indeed create a custom component and inject it in the webchat (assuming you re using the webchat) and call your api from there. There’s another option if you want, every events are posted to the parent page via iframe message posting. You could simply listen to events on the website where you embed the webchat, check if the event type is user_visit and perform your api call from there if you feel more comfortable. Whatever option you choose will allow you to query your external api from the client side but you still need a way to update userAttributes (you can do this in many ways).

I suggest you create a basic custom module with a sigle api route that you can post your results along with the userId (you could even check check user IP and user agent straight from the express request obj). In your module you’ll have access to botpress sdk with which you can use to setUserAttributes

You could also use the webchat sdk to send a custom event with a special type, create a hook in which you check for this event type, from there use botpress sdk to update user attributes with your ip and user agent info.

Hope this helps!

1 Like

Thanks for informative reply, it really helped, so I’m sending custom event from frontend and created a hook

  async function hook() {
      if (event.type === 'setUserIp') {
        bp.logger.error(JSON.stringify(event.payload.payload.payload));
        bp.logger.error(event.target);
        bp.users.updateAttributes('web', event.target, event.payload.payload.payload);
      }
    }

  return hook()

This part doesn’t work

bp.users.updateAttributes

it is not saving data, but user with given ID exists

any thoughts? maybe I’m calling it wrong way?

Thanks!

I’m sorry, I might have misled you a little bit. During the event flow (within hooks or actions) using the botpress sdk to update user attributes will not work but will in a http call outside the event flow.

You can simply mutate event.state.session.user with your informations and it will be persisted properly.

async function hook() {
      if (event.type === 'setUserIp') {
        bp.logger.error(JSON.stringify(event.payload.payload.payload));
        bp.logger.error(event.target);
       //this 
       Object.assign(event.state.session.user, event.payload.payload.payload)
       // instead of 
       // bp.users.updateAttributes('web', event.target, event.payload.payload.payload);
      }
    }

  return hook()

@EFF

Thank you! This is final code:

async function hook() {
      if (event.type === 'setUserIp') {
        Object.assign(event.state.user, event.payload.payload.payload);
      }
    }

  return hook()

instead of

event.state.session.user

I used

event.state.user

Thank you!

P.S. it is really not obvious that setting event.state.user will save user attributes behind the scene

You are totally right. I’ll share your feedback with the team !

1 Like