Crio Projects - Two truths and a lie game slack bot | Crio.Do | Project-Based Learning Platform for Developers

Objective

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.

Project Context

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:

  • Notify other users when a new user joins a channel
  • Asks the new user for two truths and a lie about them
  • Notify channel about new joining member
  • Ask existing channel members about what the new user lied about himself/herself
  • Notifies whether the predictions of the existing member is right or not

Project Stages

The project consists of the following stages:

Flow Diagram

High-Level Approach

  • Notify user when he/she joins a workspace
  • Ask to fill two truths and a lie about them
  • Notify channel with a poll to predict their lie
  • Tell the predicting member that he/she is right about new joining member

Objective

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.

Project Context

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:

  • Notify other users when a new user joins a channel
  • Asks the new user for two truths and a lie about them
  • Notify channel about new joining member
  • Ask existing channel members about what the new user lied about himself/herself
  • Notifies whether the predictions of the existing member is right or not

Project Stages

The project consists of the following stages:

Flow Diagram

High-Level Approach

  • Notify user when he/she joins a workspace
  • Ask to fill two truths and a lie about them
  • Notify channel with a poll to predict their lie
  • Tell the predicting member that he/she is right about new joining member

Getting Started

Let's get started with a few of the components involved in the project.

This milestone will help you to:

  1. Get more clarity around the unknowns like Glitch, Slack packages - events api, interactive-messages and client.
  2. Get a better understanding of the stages involved in the project like creating an app with Glitch, connecting with Slack app an workspace, providing permission and having fun.

Requirements

  • 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

    package.json

  • The directory structure of the project looks like this

    directory structure

  • Create a Slack new app using Slack API

    Create app

Expected Outcome

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.

Glitch Dashboard

Tokens and Permissions

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.

Requirements

  • Add a bot user, which will communicate with the user

    Bot user page

  • 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

    SLACK_TOKEN

  • Get Signing Secret from the basic information in slack API dashboard and set it in the .env file as SLACK_SIGNING_TOKEN

    SLACK_SIGNING_TOKEN

Expected Outcome

At the end of this milestone you would have setup a token for the Slack bot. .env file should look like this:

.env file

Creating endpoints to handle event of new user joining

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.

Requirements

  • Create instances of slack web client, event API and interactive messages using Slack tokens and following functions:

    Instance function

  • 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

    Event permissions

Expected Outcome

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.

Notify user and ask for his choice

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.

Requirements

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

Expected outcome

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:

Private message to user

Asking user for truths and lie

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.

Requirements

  • Set up function to open a dialog box to ask user for two truths and a lie
  • Create a function to validate the choices entered by the user and store the choices.
  • Create a function to acknowledge him for declining the option to play the game.

Expected Outcome

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:

Choice Dialog

Notify channel members as user submits the 2TL

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.

Requirements

  • Create a broadcast function to notify channel members with following details:
    • A introduction message of the new channel joining member
    • Three options entered by him as truths and a lie
    • Option to answer the question to choose lie statement
  • On choosing the option, show user an ephemeral message about the choice, whether he/she is correct or not.
  • Reveal the answer after sometime

Expected Outcome

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:

Channel Notification

Connection and deployment

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

Requirements

  • Install Application to your workspace

    Install Application

  • Click on the green button as shown in the image

    approving bot for workspace

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

    Slack verify 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

    Event subscription

  • Subscribe to workspace events by navigating to the Event Subscription section

    Subscribe event workspace

  • Turn on Interactive Components

    Interactive Component

  • Add twp scopes for our bot

    • bot
    • chat:write:bot

    App Scope

  • Now reinstall your app and approve it for the workspace.

  • Hooray! we are ready to play the game with our new members

Expected Outcome

You should be able to deploy the application and play the game with the new channel members.