Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

updated example working with botkit slack adapter? #42

Open
jetersen opened this issue May 6, 2019 · 16 comments
Open

updated example working with botkit slack adapter? #42

jetersen opened this issue May 6, 2019 · 16 comments
Labels

Comments

@jetersen
Copy link

jetersen commented May 6, 2019

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

@jetersen
Copy link
Author

jetersen commented May 6, 2019

@jetersen jetersen closed this as completed May 6, 2019
@jetersen
Copy link
Author

jetersen commented May 6, 2019

Actually, it does not support tables features 😢

@jetersen jetersen reopened this May 6, 2019
@benbrown
Copy link
Contributor

benbrown commented May 6, 2019

@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
Copy link
Author

jetersen commented May 6, 2019

@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
Copy link
Contributor

benbrown commented May 6, 2019

Examples of using the Bot Framework userstate?

@jetersen
Copy link
Author

jetersen commented May 6, 2019

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
Copy link
Contributor

benbrown commented May 6, 2019

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
Copy link
Author

jetersen commented May 6, 2019

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

@benbrown
Copy link
Contributor

benbrown commented May 6, 2019

@Casz let me know how that goes! :)

@benbrown
Copy link
Contributor

benbrown commented May 7, 2019

@Casz did this work out?

@jetersen
Copy link
Author

jetersen commented May 7, 2019

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

@jetersen
Copy link
Author

Migrating is not easy. Lots of things that have to be rewritten and readjusted 😢
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 😰

@jetersen
Copy link
Author

jetersen commented May 12, 2019

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

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
Copy link
Contributor

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

@mhjb
Copy link

mhjb commented Jul 19, 2019

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants