Creating a slack bot with Javascript

slack.png Slack can be easily described as a messaging system which allows channel creation for easy real-time communication.
It is a very popular means used by team members in communicating.
Although a messaging system, it is incredibly flexible and can be extended and customized.
With slack, you can create a custom welcome message, emojis, applications, third-party applications and slack bots.
A slack bot functions as an event receiver, which handles the events sent from slack.
There is an excess of events being sent to slack bot and it is necessary to decide how to handle each event and what events should be handled.
To do this, node.js is needed.
Node.js application is a runtime environment for JavaScript.

CREATING A CUSTOM BOT

  • You can create a custom bot from slack.com. You need a slack account and must be logged in.
  • Click the add configuration button on the left.
  • A new page pops requiring you to provide your bot username. Slack has some naming guidelines and you need to follow them.
  • You are redirected to a customize page after creating the bot. You can use the features available to you, to your satisfaction. You need the API Token that appears on this page. This token starts with xoxb-, and you should copy this to somewhere safe, unless you are certain this page will remain open.
  • Next, you have to create or select an existing channel that will interact with your bot.

Also, you need to invite and add your bot for interaction to the channel.
The slack bot has just been configured.

  • Install node.js if you do not have it.
  • Create a new node.js application to run through the npm init process.
    The npm init process launches a utility to assist you configure your new project.
    Using command prompt, Set where the application should be installed.

Run

mkdir slackbot
cd slackbot
npm init

The npm init utility asks for the name, giving you the opportunity to change your application name.
Press ENTER to proceed.

  • The entry point is requested, by default, it is set to index.js. You can change it to apps.js if you want to.
    You'd be asked for test command, keywords, license, author, git repository.
    You can decide to leave these at their default settings or change them.
  • Install Slack Development Kit to ease the slack interaction.
    npm install @slack/ client --save will do this.
  • Create my index.js which uses the API token earlier saved from the custom bot to create an RtmClient.
  • Create an event listener for RTM.AUTHENTICATED which finds the slack channel which the bot is part of.
  • Debug. Create an event listener.
    This listener is for RTM.RTM.CONNECTION.OPENED which will send a Hello! message to your slack channel.
  • Begin the authentication process called RTM Start Session which is handled by your event listeners.

To finish creating the bot, you have to handle the message event which can be a bit complicated.
This will enable the bot to receive messages.
Also you can enhance the message listener to respond to a specific command by;

    1. Splitting the text of a message to an array.
    1. Check if the bot's username matches the first index.

The bot is completed as the variable is set and the previously created message event listener is fleshed out.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?