Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Slack Bots

You're reading from  Building Slack Bots

Product type Book
Published in Jun 2016
Publisher Packt
ISBN-13 9781786460806
Pages 182 pages
Edition 1st Edition
Languages
Concepts

Chapter 4. Using Data

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:

  • Introduction to Redis

  • Connecting to Redis

  • Saving and retrieving data

  • Best practices

  • Error handling

Introduction to Redis


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...

Connecting to Redis


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:

npm install redis

Now, create a new index.js file and paste in the following code:

'use strict';

const redis = require('redis');
const Bot = require('./Bot');

const client = redis.createClient();

const bot = new Bot({
  token: process.env.SLACK_TOKEN,
  autoReconnect: true,
  autoMark: true
});

client.on('error', (err) => {
    console.log('Error ' + err);
});

client.on('connect', () => {
  console.log('Connected to Redis!');
});

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:

client.set('hello', 'Hello World!');

client.get('hello', (err, reply) => {
  if (err) {
    console.log(err);
    return;
  }

  console.log(`Retrieved: ${reply}`);
});

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:

let val = client.get('hello');
console.log('val:', val);

This, perhaps confusingly, results in:

val: false

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...

Best practices


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:

bot.respondTo('roll', (message, channel, user) => {
  // get the members of the channel
  const members = bot.getMembersByChannel(channel);

  // make sure there actually members to interact with. If there...

Simple to-do example


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:

bot.respondTo('todo', (message, channel, user) => {
  let args = getArgs(message.text);

  switch(args[0]) {
    case 'add':
      
      break;

    case 'complete':
      
      break;

    case 'delete':
      
      break;

    case 'help':
      channel.send('Create tasks with \`todo add [TASK]\`, complete them with \`todo complete [TASK_NUMBER]\` and remove them with \`todo delete [TASK_NUMBER]\` or \`todo delete all\`');
      break;

    default:
      showTodos(user.name, channel);
      break;
  }
}, true);

function showTodos(name, channel) {
  client.smembers(name, (err, set...

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Building Slack Bots
Published in: Jun 2016 Publisher: Packt ISBN-13: 9781786460806
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}