howdyai / botkit-storage-mongo

A MongoDB storage driver for Botkit
MIT License
54 stars 42 forks source link

updated example working with botkit slack adapter? #42

Open jetersen opened 5 years ago

jetersen commented 5 years ago

@benbrown I would like to know what changes are required for this to continue to work 😓 SlackAdapter does not accept .storage

jetersen commented 5 years ago

I see I should use: https://www.npmjs.com/package/botbuilder-storage-mongodb

jetersen commented 5 years ago

Actually, it does not support tables features 😢

benbrown commented 5 years ago

@casz yes, this project is going to be deprecated soon, or at least marked as for legacy Botkit only.

For conversation state persistence, use the botbuilder-storage module. For your own tables, I would use Mongoose directly.

jetersen commented 5 years ago

@benbrown it would be great with more examples of how to use userstate with mongodb storage.

Sadly, I don't have any conversations in my current bot.

benbrown commented 5 years ago

Examples of using the Bot Framework userstate?

jetersen commented 5 years ago

I guess I don't know what I am asking for 😰 I mainly use botkit (no bot builder framework) and I have some need for storing user data in the mongodb for caching / reduced api requests

benbrown commented 5 years ago

Gotcha. Yeah, the approach I am taking with new Botkit is, Botkit isn't in the business of database handling. It is better to manage that yourself.

To be honest, if this module works for you, just use it -- it can be plonked into the new Botkit using the addPluginExtension() method. Something like this:

const mongoStorage = require('botkit-storage-mongo')({mongoUri: '...', tables: ['optional','list', 'of', 'custom','tables', 'to', 'add']});

// register it so it will show up in controller.plugins
controller.addPluginExtension('database', mongoStorage);

// use it
controller.plugins.database.user.get('id', function(err, user) { ... });
jetersen commented 5 years ago

Neat, that should at least allow me to migrate quickly and then, later on, remove the usage of this module.

benbrown commented 5 years ago

@casz let me know how that goes! :)

benbrown commented 5 years ago

@casz did this work out?

jetersen commented 5 years ago

I haven't had a chance yet. Hopefully sometime during the weekend :sweat:

jetersen commented 5 years ago

Migrating is not easy. Lots of things that have to be rewritten and readjusted :cry: The docs for testing could use some love cause that is what I am struggling with not knowing whether my changes work or not is like hit or miss at this point :cold_sweat:

jetersen commented 5 years ago

I wonder why you chose memory example for bot access/user token and why this was not built into the adapter :thinking:

This should do the trick

controller.webserver.get('/install/auth', async (req, res) => {
  try {
    const results = await controller.adapter.validateOauthCode(req.query.code)

    console.log('FULL OAUTH DETAILS', results)

    // Store token by team in bot state.
    storage.write({
      [results.team_id]: {
        bot_access_token: results.bot.bot_access_token,
        bot_user_id: results.bot.bot_user_id,
      },
    })

    res.json('Success! Bot installed.')
  } catch (err) {
    console.error('OAUTH ERROR:', err)
    res.status(401)
    res.send(err.message)
  }
})

async function getTokenForTeam(teamId: string): Promise<string> {
  const team = await storage.read([teamId]);
  if (team && team.bot_access_token) {
      return team.bot_access_token
  } else {
      console.error('Team not found in tokenCache: ', teamId);
  }
}

async function getBotUserByTeam(teamId: string): Promise<string> {
  const team = await storage.read([teamId]);
  if (team && team.bot_user_id) {
      return team.bot_user_id
  } else {
    console.error('Team not found in userCache: ', teamId);
  }
}
benbrown commented 5 years ago

Yes, that works. I am trying to avoid any sort of prescription on using external databases, as every app is different.

mhjb commented 5 years ago

FWIW, and for others Googling, I received the following error after attempting to install botkit-mongo-storage on botkit 4:

TypeError: this.storage.read is not a function

This issue was helpful for understanding what was going on.

Mattw11486 commented 2 years ago

I am also getting the same error TypeError: this.storage.read is not a function What is the solution?