/slacktrivia

A Slack Bot through which you create and host a Quiz in a Slack Channel.

Primary LanguagePython

Quiz App for Slack

Welcome to Quiz App for Slack! We wanted to make our Slack Channels more interactive and fun with an inhouse Quiz Bot. Through this, any Team Member can create a custom quiz/or fetch pre-existing questions category wise and release it in the channel for other members to attempt. We're hoping this leads to more interaction among Team Members.

We also hope that you enjoy playing around with this App just as much as we enjoyed building it.

Building For Slack

Docs : https://api.slack.com/#read_the_docs

Slack’s APIs allow anyone to build full featured integrations that extend and expand the capabilities of your Slack workspace. These APIs allow you to build applications that interact with Slack just like the people on your team – they can post messages, respond to events that happen – as well as build complex UIs for getting work done.

There are two main Slack APIs we'll be using to connect with Slack.

  1. Events API ( https://api.slack.com/events-api )

    There are multiple type of events, and in order to trigger something on Slack based on an event, we'll need to use Events API to listen to those events.

  2. Web API ( https://api.slack.com/web )

    We're using Web API to push messages or update previously pushed messages. To push messages we're using chat.postMessage(). To update previously pushed messages we're using chat.update().

We're also be using Slack Developer Kit for Python ( https://slack.dev/python-slackclient ), which can be installed by:

pip install slackclient

There are two components to managing a Slack App.

  1. App Dashboard
  2. Backend Server

App Dashboard is where you have to

  • Generate Signing Secret ( Used for authorization )
  • Update Request URL ( Any interactions with shortcuts, modals, or interactive components such as buttons, select menus, and datepickers will be sent to a URL you specify. )
  • Define Slash Commands
  • Generate OAuth Tokens ( These tokens were automatically generated when you installed the app to your team. You can use these to authenticate your app. )
  • Suscribe to Bot Events ( Apps can subscribe to receive events the bot user has access to eg : liking new messages in a channel. )

Backend Server is where you define methods to handle HTTP Requests or respond to events generated by Slack.

-Example Scenario-

Suppose we have pushed a message to Slack using chat.postMessage() of WebAPI and we want the message to be updated with additional info every time an User reacts with an emoji to it. In that case, we can use Events API's 'add_reaction' method to listen to such events, and then we can use chat.update() method of Web API, which takes a previously pushed message as a parameter and updates that specific message.

Sending Messages to Slack

Messages can be plain text or interactive. If we want to send interactive messages( in other words, messages that have buttons etc in them which Users can respond to ), we'll need to use Block Kits which is a UI Framework of Slack. Block Kits are in JSON Format.

Whatever messages we'll be sending to Slack through chat.postMessage() are basically a pre defined JSON format specific to UI that we want the message to have.

Slack makes this process of building those JSON files much easier by having an interactive UI Building Interface called Block Kit Builder ( https://app.slack.com/block-kit-builder ) using which you can drag and drop UI elements and Slack will prepare the JSON format for you.

Let's talk about Entry points

There can be multiple entry points by which User can start interacting with the App. One such entry point is going to "App Home". When User opens App Home, an event called "app_home_opened" gets triggered. Using SlackEventAdapter from slackeventsapi we can listen to such events and define a method that will respond to that event with a message. In our case, we want to greet the User when User opens App Home.

Another entry point can be through sending messages or slash commands through message dialog box. In our case, we want below slash commands to act as entry points. ( https://api.slack.com/interactivity/slash-commands )

/quiz start : Starts Quiz Creation Prompt

/quiz help : List quiz commands

/quiz create : Starts Custom Quiz Creation Prompt

Every time one of above commands are entered by User in Message Composer Box, an HTTP POST Request is sent to the Request URL specified in the App Dashboard. On the backend, we attach a method to handle such URLs/Routes.

Currently, we're only using 2 entry points.

  1. When User opens App Home
  2. When User enters a slash command.

Below is a representation of how Code is structured.

N|Solid