Who doesn't like playing games? Do you want to create your own game? Get started with a simple game project using Slack bot API to interact with the workspace members and get to know them better.
Who doesn't like playing games? Do you want to create your own game? Get started with a simple game project using Slack bot API to interact with the workspace members and get to know them better.
Slack was created in August of 2013 by Stewart Butterfield, Eric Costello, Cal Henderson, and Serguei Mourachov and stands for “Searchable Log of All Conversation and Knowledge.” Slack began as a way for the team of Glitch (then a massive multi-player online game) to communicate between each other. Even as the team realized that Glitch was an Adobe flash based game at a time the market had moved towards mobile app-based games, they found that the internal software they developed could be used to help teams collaborate.
Thus, they launched it as a set of cloud based tools to make communication at work better. Currently Slack has 11+ million daily users. So, to make the workflow easy and make some automation, there are many bots available for Slack.
We will be creating a bot for our Slack workspace that's named "Two truths and a lie". This bot will help you to play a game when someone who joins your workspace to get to know each other.
Our bot will support the following functionalities:
The project consists of the following stages:
Who doesn't like playing games? Do you want to create your own game? Get started with a simple game project using Slack bot API to interact with the workspace members and get to know them better.
Slack was created in August of 2013 by Stewart Butterfield, Eric Costello, Cal Henderson, and Serguei Mourachov and stands for “Searchable Log of All Conversation and Knowledge.” Slack began as a way for the team of Glitch (then a massive multi-player online game) to communicate between each other. Even as the team realized that Glitch was an Adobe flash based game at a time the market had moved towards mobile app-based games, they found that the internal software they developed could be used to help teams collaborate.
Thus, they launched it as a set of cloud based tools to make communication at work better. Currently Slack has 11+ million daily users. So, to make the workflow easy and make some automation, there are many bots available for Slack.
We will be creating a bot for our Slack workspace that's named "Two truths and a lie". This bot will help you to play a game when someone who joins your workspace to get to know each other.
Our bot will support the following functionalities:
The project consists of the following stages:
Let's get started with a few of the components involved in the project.
This milestone will help you to:
You will need to have a Glitch account. So create one if you don’t have it already
Create a project by going to the New Project
After setting up the project, install the dependencies for the project
The directory structure of the project looks like this
Create a Slack new app using Slack API
Expected Outcome by the end of this milestone, you should be able to create a project on Glitch and access the Slack API dashboard to create an app and manage access.
Security for the workspace is important as the security of your gallery. So, we need token (security key) and permission (to be able to access required parts of API), which we are going to get it from the Slack API dashboard.
Add a bot user, which will communicate with the user
Approve the app for the workspace
Get Bot User OAuth Access Token
from slack API dashboard and set it in .env file as SLACK_TOKEN
Get Signing Secret
from the basic information in slack API dashboard and set it in the .env
file as SLACK_SIGNING_TOKEN
At the end of this milestone you would have setup a token for the Slack bot. .env
file should look like this:
To listen to the events such as new users joining the channel, users using options in slack, etc., and performing the actions based on the events, we are going to create API endpoints.
Create instances of slack web client, event API and interactive messages using Slack tokens and following functions:
Setup routes to listen to events and actions. Routes path:
/events
/actions
Set permission in the Slack API dashboard to listen to the user events
By the end of this milestone, you'll have set only required permissions for the Slack API and created the endpoints to listen to those events such as a new member joining the channel, users using options in slack and much more.
Every time when user joins a channel we want to notify him about channel joining and want to ask him if he wants to play truth and lie game with his teammates to know them better and also as an ice breaker.
We'll be using express.js and slack events package to create endpoints to listen events. You'll be writing a script to listen to events from Slack workspace and the event id when someone joins the channel. We are going to send notification to other users about new user joining the channel. When a new user joins the channel, we will surface the option to play this game to the user.
Listen to channel_join
event using the endpoint.
if (evt.subtype === 'channel_join')
Create Game class with web client, user id and channel id as properties.
Create function to open conversation with user to tell him about the channel joining and ask his choice to play with the user of joined channel.
Create action handler for declining and accepting the choice to play the game.
By the end of this milestone, you'll be able to send appropriate message to the user on particular action like joining the channel, accepting to play or decline to play
Your message should be see to user like this:
When the user accepts the option to play the game with the channel members. We are going to open a dialog box which will take the input of two truths and a lie form the user.
If the user declines the request then we are going to show him the proper message for the rejection.
By the end of this milestone, you'll be able to create a dialog box to take the input from the user for truths and the lie.
Your dialog box should look something like this:
We are going to notify the channel members of joining of user with a poll to predict which one of three is a lie. When a channel member clicks on the poll a private message would be displayed to him that if he is right or wrong about the choice.
After few minutes (may be after 12 hours) we are going to reveal truths and lie to all the channel members.
By the end of this milestone, you'll be able to notify the channel members for the new joining member and ask them to guess his lie. As soon as they guess, they will get a private message in the channel about correctness of their choice. In addition to this, you'll be reveling the answer to the channel members after sometime.
Your notification message should look something like this:
Now, we are going to define scopes and interactive components of the bot using Slack API dashboard. After setting up the permission, we are going to install or reinstall bot in our Slack workspace
Install Application to your workspace
Click on the green button as shown in the image
Verify your identity to the Slack API by opening the console of Glitch app (navigate to tools to find console) and running the following command
./node_modules/.bin/slack-verify --secret=$SLACK_SIGNING_SECRET --port=3000 --path=/events
You will see the following message.
Click show live. Your app will open in new tab
Copy the URL of your project from this tab and return to the Slack API dashboard. Navigate to event subscription and paste the link in the Request URL box
Subscribe to workspace events by navigating to the Event Subscription section
Turn on Interactive Components
Add twp scopes for our bot
bot
chat:write:bot
Now reinstall your app and approve it for the workspace.
Hooray! we are ready to play the game with our new members
You should be able to deploy the application and play the game with the new channel members.