Now that we've seen how to process keywords, commands, and API calls, we will look at the next logical step in bot building: persistent data storage and retrieval. References to data can be kept in JavaScript by assigning said data to a variable; however, its use is limited to when the program is running. If the program is stopped or restarted, we lose the data. Hence, persistent data storage is required for certain tasks.
This allows us to build bots that can, for instance, keep track of a leaderboard or store a to-do list.
In this chapter, we will cover:
In the previous chapter, we discovered how to create a competitive roll bot that allows users to play a "Who can roll the highest" game. Although it worked admirably, the feature sorely missing is a leaderboard of sorts, where each user's wins and losses are stored and an overall winners list is kept.
Such a feature wouldn't be difficult to produce; however, the largest problem comes in storing the data. Any data stored in JavaScript variables would be lost once the program ends or crashes. A better solution would then be to maintain a persistent database, which our bot can write to and read from.
There is a wide variety of database services to choose from; you might already be familiar with MySQL or MongoDB. For the example bots in this chapter, we will pick a service that is easy to set up and simple to use.
The database service we will use is Redis: http://redis.io/.
The Redis website describes the technology as follows:
"Redis is an open source (BSD licensed), in-memory...
To demonstrate how to connect to Redis, we will create a new bot project (including the Bot
class defined in Chapter 3, Adding Complexity). We'll start by installing the Redis Node client, executing the following:
Now, create a new index.js
file and paste in the following code:
This snippet will import the Redis client and connect to the local instance running via the createClient()
method. When not supplied with any arguments, the aforementioned method will assume the service is running locally on the default port of 6379. If you wish to connect to a different host and port combination, then you...
Saving and retrieving data
First, let's look at what the Redis client has to offer us. Add the following lines to index.js
:
In this example, we will set the value "Hello world!" in Redis with the key hello
. In the get
command, we specify the key we wish to use to retrieve a value.
Note
The Node Redis client is entirely asynchronous. This means that you have to supply a callback function with each command if you wish to process data.
A common mistake is to use the Node Redis client in a synchronous way. Here's an example:
This, perhaps confusingly, results in:
This is because the get
function will have returned the Boolean false
before the request to the Redis server has been made.
Run the correct code and you should see the successful retrieval of the Hello world...
Any user should be able to store data in Redis via bot commands; it is however recommended you ensure that the data storage methods cannot be easily abused. Accidental abuse might happen in the form of many different Redis calls in a short amount of time. For more information on Slack channel spam and remedies, revisit Chapter 2, Your First Bot.
By restricting bot traffic, we can ensure that Redis does not receive an inordinate amount of write and retrieve actions. If you ever find that Redis latency is not as good as it should be, visit this webpage to help troubleshoot: http://redis.io/topics/latency.
Let's now look at how we can improve familiar bot behavior with the addition of Redis data storage.
First, here is our roll
command, with the new Redis store code highlighted:
With the basics of Redis covered, we shall now move on to create a simple to-do Slack bot. The aim of this bot is to allow users to create a to-do list, allowing them to add, complete, and delete a task from this list as they go about their day.
This time, we will start with a skeleton of what we want and build each feature step by step. Start by adding this new command to your bot:
In this chapter, the reader has learned the basics of the persistent data storage Redis and how to use it through the Node Redis client. We outlined the reasons why Redis lends itself well for use with bots, particularly when keeping a score list or storing multiple small items.
In the next chapter, we will introduce the concept of natural language processing (NLP) and see how to evaluate and generate natural language for use in a bot.