Git Product home page Git Product logo

apidays-workshop-2020's Introduction

Welcome to Slack App Development!

This repository is the workshop material for Apidays live Singapore. If you're not an attendee of the workshop, don't worry! You can try this sample project just by reading this.

Go to git.io/apidays-slack

This project template is available at git.io/apidays-slack (https://github.com/seratch/apidays-workshop-2020). Use the shortened URL for sharing this material with others!

The App We're Going to Build

In this workshop, we're going to build a helpdesk workflow app from scratch. Building the app helps us learn how to start Slack App development and utilize the latest Slack Platform features.

Here is the quick demo of the complete version of the helpdesk workflow:

When an end-user clicks a shortcut from the text composer (or the search bar), a modal window pops up.

When the user selects a category from the select menu, the modal immediately transforms itself to show input fields for the chosen category. Also, "Back" button in the modal allows the user to go back to the initial step.

The app applies custom input validation rules for the title and due-date inputs.

When the app receives a valid data submission from the user, the app sends notifications to the helpdesk team's channel, the DM with the submitter, and the DM with the approver (only for mobile device requests). In addition, the app updates Home tab for the submitter with the up-to-date list of the person's submissions.

The Slack Platform Features

We'll meet the above requirements by leveraging many of the latest Slack Platform features. We'll learn effective ways to take advantage of Block Kit, Global Shortcuts, and Modals.

  • Block Kit: Block Kit is the Slack's UI framework for building an rich user interface. From a Slack app developer perspective, it is just a kind of JSON data structure to comply with. Slack properly renders your specifically structured JSON for both desktop and mobile.
  • Global Shortcuts: A Global Shortcut is a quick and easy way to start a workflow from anywhere in Slack. Users can access global shortcuts from either the menu in text composer or search bar. Try a built-in one to learn how it works.
  • Modals: A modal is a popover window built with Block Kit components. You use this for collecting user inputs or displaying information in an organized way. A modal can have multiple steps in its flow.

Bolt for JavaScript

We're going to use Bolt for JavaScript, a Slack app framework optimized for taking full advantage of the Platform features. With Bolt, you won't be bothered by Slack-specific non-functional requirements:

  • Verify requests from Slack for security (Your API endpoint is exposed to the internet. That means not only Slack, but any random clients may try to send requests to your endpoint. So, it's highly recommended to distinguish Slack's requests from others.)
  • Parse a variety of payloads (For historical reasons, the data structure of payloads varies among features.)
  • Dispatch requests to their right code path (similarly to well-designed full-stack Web app frameworks)
  • Avoid infinite loop errors by reacting to events triggered by the app itself (particularly when using Events API)
  • Pick up the right access token for an incoming request

As Bolt takes care of these things, your Slack app development becomes even more productive.

All The Steps to Build This App

Get the Project Template

Access git.io/apidays-slack!

git clone [email protected]:seratch/apidays-workshop-2020.git
cd apidays-workshop-2020/
node --version # need to be v10.13 or higher
npm i # install dependencies
code . # Open the project with Visual Studio Code

Create a New Slack App

Visit https://api.slack.com/apps to create a new Slack App configuration.

Signing in to the Slack workspace you're going to use for development in the browser is required.

Add Required Bot Token Scopes

Go to the OAuth & Permissions page and add the following Bot Token Scopes to the app.

Give Your Bot User a Relevant Name

The Slack App's name automatically determines the default bot user name. If you prefer a different name over the name automatically set, go to App Home page and change it. As a bot user name is user-facing, making it nice is crucial for better user experiences.

Install The App Onto Your Development Workspace

As long as you use this app only in its development workspace, you don't need to implement the standard OAuth flow for acquiring OAuth access tokens. You can go with the simplified way offered by the Slack Platform. Go to the Install App page and just click the install button and complete the OAuth flow. You'll use the Bot User OAuth Access Token when booting your Bolt app.

If you're interested in distributing your app to multiple workspaces, refer to the Bolt JS's guide.

Run the Bolt App

All the steps to follow for booting your Bolt app are:

  • Create .env file at the root directory of this project
  • Make sure if you're using Node.js 10.13 or higher (node --version)
  • Run npm install to fetch all required dependencies
  • Run npm run local to start the local app
  • Install ngrok if you don't have yet - https://ngrok.com/
  • Open another terminal and run ngrok http 3000 to establish a public endpoint

Place .env and Save Your Credentails

SLACK_BOT_TOKEN=xoxb-111-111-xxx
SLACK_SIGNING_SECRET=xxx

Run the Local App

The procedure is actually quite simple. Run the following commands.

node --version # should be v10.13.0 or higher
npm install
npm run local # This starts an app at http://localhost:3000/slack/events

Run ngrok for Forwarding Slack Requests To Your Local App

If you don't have ngrok, download it.

# Check if the local app is running
curl -I -XPOST http://localhost:3000/slack/events # You should get HTTP/1.1 401 Unauthorized

# on another terminal
./ngrok http 3000

# if you're a paid user
./ngrok http 3000 --subdomain {whatever-you-want}

# You can verify if it's working by the following on yet another terminal:
curl -I -XPOST https://{your random subdomain here}.ngrok.io/slack/events # You should get HTTP/1.1 401 Unauthorized

Set Request URL and add a Global Shortcut

  • Visit the Slack App configuration page (https://api.slack.comc/apps) and choose your app
  • Go to the Interactivity & Shortcuts page
  • Turn on the Interactivity feature
  • Set https://{your random subdomain here}.ngrok.io/slack/events as the Request URL
  • Create a new Shortcut (Global -> Callback ID: new-helpdesk-request)
  • Click the Save Changes button at the bottom

Implement Listeners in Your Bolt App

Check the ./src/solution.js to know the complete version of the app.

Tip: Block Kit Preview Tool

Block Kit Builder is a preview tool in the browser to see how your blocks look like in Slack. I recommend checking the validity and the appearance of your blocks using this tool before embedding them into your app.

Recap

  • Create a Slack app configuration at https://api.slack.com/apps
    • Signing in with your Slack workspace account is required
  • Configure the Slack app with sufficient permissions
    • Go to the "OAuth & Permissions" page and add bot token scopes
    • Turn the features you use on (Interactivity, Events Subscriptions, Home tab, and so on)
  • Install the app onto its Development Slack Workspace
    • Grab the "Bot User OAuth Access Token" starting with "xoxb-"
  • Create a Bolt app
    • Set env variables - SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET
    • Start the app (app.start())
    • By default, Bolt apps receive requests coming to POST http://localhost:3000/slack/events
  • Have a public endpoint to receive requests from Slack
    • You may use ngrok or similar for it (ngrok http 3000)
  • Add listeners for incoming requests
  • Have fun with Slack app development

License

The MIT License

apidays-workshop-2020's People

Contributors

seratch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.