CreatingBots - 18F/charlie GitHub Wiki

Charlie developer documentation > Creating bots

Initialization

Charlie's entrypoint is src/main.js. When it starts, it goes through the following steps:

  1. Merge any Cloud Foundary-provided environment variables with variables provided directly to the environment. In the event of a conflict, the directly-provided values win.

  2. Create and initialize a Bolt App object with the Slack credentials to connect to Slack and to sign and verify messages to and from Slack.

  3. Initializes a brain for persistent storage. This will create a database table if it does not already exist. It will then read the contents of the key/value-pair database into memory. The brain is then attached to the app object to make it easy for bots and scripts to access.

  4. Starts the Bolt app. This starts the HTTP listener that Bolt uses to receive messages from Slack.

  5. Sets the default client that will be used by the Slack utility.

  6. Loads all of the Javascript files in src/main.js (excluding those that end with .test.js), and require()s them so they are loaded. If the export of the loaded module is a function, that function is called with the Bolt app as its only argument.

Creating a new Charlie bot

For a new bot called awesomobot, this is the general flow:

  1. Create a file at src/scripts/awesomebot.js. This module should export a function that takes a Bolt app object as its sole argument. This exported function will be called by Charlie during initialization.

    • The Bolt app object that your bot receives has been augmented with a brain property. You can use this brain for persistent storage of key/value pairs.
  2. Write the core bot functionality inside the exported function. This is where you can subscribe to Bolt events and create event handlers.

    • There are a host of utilities that are provided. You can import them into your bot with:

      const utils = require("../utils");
      

      You can also reduce how much you import by destructuring to just the utilities that your bot needs:

      const {
        slack: { postMessage },
      } = require("../utils");
      
  3. Write accompanying tests in src/scripts/awesomebot.test.js. Use the testing utilities to help with mocking.