Open jetersen opened 5 years ago
I see I should use: https://www.npmjs.com/package/botbuilder-storage-mongodb
Actually, it does not support tables features 😢
@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.
@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.
Examples of using the Bot Framework userstate?
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
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) { ... });
Neat, that should at least allow me to migrate quickly and then, later on, remove the usage of this module.
@casz let me know how that goes! :)
@casz did this work out?
I haven't had a chance yet. Hopefully sometime during the weekend :sweat:
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:
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);
}
}
Yes, that works. I am trying to avoid any sort of prescription on using external databases, as every app is different.
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.
I am also getting the same error TypeError: this.storage.read is not a function What is the solution?
@benbrown I would like to know what changes are required for this to continue to work 😓 SlackAdapter does not accept
.storage