-
Notifications
You must be signed in to change notification settings - Fork 21
CreatingBots
Charlie's entrypoint is src/main.js
. When it starts, it goes through the
following steps:
-
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.
-
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.
-
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.
-
Starts the Bolt app. This starts the HTTP listener that Bolt uses to receive messages from Slack.
-
Sets the default client that will be used by the Slack utility.
-
Loads all of the Javascript files in
src/main.js
(excluding those that end with.test.js
), andrequire()
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.
For a new bot called awesomobot
, this is the general flow:
-
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.
- The Bolt app object that your bot receives has been augmented with a
-
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");
-
-
Write accompanying tests in
src/scripts/awesomebot.test.js
. Use the testing utilities to help with mocking.
Charlie developer documentation