shivanggupta / Wit-Facebook-Py

Facebook Messenger Bot with realtime weather updates (Python)
9 stars 9 forks source link

Need context for each FB user. #4

Open hunkim opened 8 years ago

hunkim commented 8 years ago

You need to create a separate context for each FB user. Otherwise, bot reveals some important information to other users. :-)

You need something like this:

const findOrCreateSession = (fbid) => {
  let sessionId;
  // Let's see if we already have a session for the user fbid
  Object.keys(sessions).forEach(k => {
    if (sessions[k].fbid === fbid) {
      // Yep, got it!
      sessionId = k;
    }
  });
  if (!sessionId) {
    // No session found for user fbid, let's create a new one
    sessionId = new Date().toISOString();
    sessions[sessionId] = {
      fbid: fbid,
      context: {
        _fbid_: fbid
      }
    }; // set context, _fid_
  }
  return sessionId;
};

See https://github.com/hunkim/Wit-Facebook/blob/master/index.js.

shivanggupta commented 8 years ago

I am currently passing the messenger's FB User ID as the session ID which is being passed to client.run_actions. The wit.ai bot maintains a session for each user using the converse function.

recipient_id = x['sender']['id']
client.run_actions(recipient_id, message, {})
hunkim commented 8 years ago

recipient_id is not used, right?

     `       client.run_actions(recipient_id, message, {})

`

What is the last '{}'?

hunkim commented 8 years ago

Perhaps, you need something like this:

sessions[sessionId].context

shivanggupta commented 8 years ago

Recipient_id is used as the session ID: Below is the prototype for .run_actions function for PyWit. The '{}' is to pass an empty python dictionary for the context to be held in.

def run_actions(self, session_id, message, context={},
                    max_steps=DEFAULT_MAX_STEPS):
hunkim commented 8 years ago

I don't understand. Currently, each user has their own context?

Sung

On Tue, Apr 26, 2016 at 8:32 PM, shivanggupta notifications@github.com wrote:

Recipient_id is used as the session ID: Below is the prototype for .run_actions function for PyWit. The '{}' is to pass an empty python dictionary for the context to be held in.

def run_actions(self, session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS):

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/shivanggupta/Wit-Facebook-Py/issues/4#issuecomment-214724219

shivanggupta commented 8 years ago

Yes, which is being maintained by the Wit.ai API, the session-id is the user's FB id.

The code below from PyWit may help:

 def converse(self, session_id, message, context={}):
        params = {'session_id': session_id}
        if message:
            params['q'] = message
        return req(self.access_token, 'POST', '/converse', params, json=context)

def req(access_token, meth, path, params, **kwargs):
    rsp = requests.request(
        meth,
        WIT_API_HOST + path,
        headers={
            'authorization': 'Bearer ' + access_token,
            'accept': 'application/vnd.wit.20160330+json'
        },
        params=params,
        **kwargs
    )
    if rsp.status_code > 200:
        raise WitError('Wit responded with status: ' + str(rsp.status_code) +
                       ' (' + rsp.reason + ')')
    json = rsp.json()
    if 'error' in json:
        raise WitError('Wit responded with an error: ' + json['error'])
    return json

Converse is called by the run_actions function we discussed before.

hunkim commented 8 years ago

Since you pass {}, you create new context per each talk, right? So you loose context for each user? I think you need to create a context for each user and reuse it.

shivanggupta commented 8 years ago

I believe the context for each user is loaded into the empty dictionary through the converse method. The session side of things to ensure that context doesn't change every time is handled by the Wit.ai API. I'm not 100% sure about this though, so I intend to add some actions to the bot that require context to be loaded multiple times and then test it.

EDIT: I plan to add a follow up question to ask if the user wants more information such as temperature. I can then try outputting temperature (new context) based on location (loc in old context)

hunkim commented 8 years ago

Whenever you get a new POST (a msg), you create a new context, I guess.

That's why Node has the logic to search/create a context for each session.

On Tue, Apr 26, 2016 at 10:17 PM, shivanggupta notifications@github.com wrote:

I believe the context for each user is loaded into the empty dictionary through the converse method. The session side of things to ensure that context doesn't change every time is handled by the Wit.ai API. I'm not 100% sure about this though, so I intend to add some actions to the bot that require context to be loaded multiple times and then test it.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/shivanggupta/Wit-Facebook-Py/issues/4#issuecomment-214760019

shivanggupta commented 8 years ago

Flask and python both have their own version of sessions which I can use to create sessions. I chose to not do that as I thought that the Wit API handled the session so I will check using my test strategy above and then implement sessions if needed.

hunkim commented 8 years ago

Sure. Please do test (with multi users/context items), and see it works.

On Tue, Apr 26, 2016 at 10:23 PM, shivanggupta notifications@github.com wrote:

Flask and python both have their own version of sessions which I can use to create sessions. I chose to not do that as I thought that the Wit API handled the session so I will check using my test strategy above and then implement sessions if needed.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/shivanggupta/Wit-Facebook-Py/issues/4#issuecomment-214762062