Skip to content

Latest commit

 

History

History
70 lines (57 loc) · 3.45 KB

storage.md

File metadata and controls

70 lines (57 loc) · 3.45 KB

Storing Information

Botkit has a built in storage system used to keep data on behalf of users and teams between sessions. Botkit uses this system automatically when storing information for Slack Button applications (see below).

By default, Botkit will use json-file-store to keep data in JSON files in the filesystem of the computer where the bot is executed. (Note this will not work on Heroku or other hosting systems that do not let node applications write to the file system.) Initialize this system when you create the bot:

var controller = Botkit.slackbot({
  json_file_store: 'path_to_json_database'
});

This system supports freeform storage on a team-by-team, user-by-user, and channel-by-channel basis. Basically controller.storage is a key value store. All access to this system is through the following twelve functions. Example usage:

controller.storage.users.save({id: message.user, foo:'bar'}, function(err) { ... });
controller.storage.users.get(id, function(err, user_data) {...});
controller.storage.users.delete(id, function(err) {...});
controller.storage.users.all(function(err, all_user_data) {...});

controller.storage.channels.save({id: message.channel, foo:'bar'}, function(err) { ... });
controller.storage.channels.get(id, function(err, channel_data) {...});
controller.storage.channels.delete(id, function(err) {...});
controller.storage.channels.all(function(err, all_channel_data) {...});

controller.storage.teams.save({id: message.team, foo:'bar'}, function(err) { ... });
controller.storage.teams.get(id, function(err, team_data) {...});
controller.storage.teams.delete(id, function(err) {...});
controller.storage.teams.all(function(err, all_team_data) {...});

Note that save must be passed an object with an id. It is recommended to use the team/user/channel id for this purpose. [user/channel/team]_data will always be an object while all_[user/channel/team]_data will always be a list of objects.

Writing your own storage module

If you want to use a database or do something else with your data, you can write your own storage module and pass it in.

Make sure your module returns an object with all the methods. See simple_storage.js for an example of how it is done! Make sure your module passes the test in storage_test.js.

Then, use it when you create your bot:

var controller = Botkit.slackbot({
  storage: my_storage_provider
})

Botkit Documentation Index