Build a Slack Chatbot in Python

Yeonho Park
BotHub.Studio
Published in
4 min readSep 11, 2018

Creating a Slack Bot

1. Create a Slack App for your bot

To create a new Bot, click the ‘Create New App’ button

1) Enter an ‘App Name’ and select a ‘Development Slack Team’.

2)Click the ‘Create App’ button.

2. Create a Slack Bot User

Adding a Bot User to created Slack App

1) Select ‘Bot Users’ tab.

2) Click the ‘Add a Bot User’

3) Enable ‘Always Show My Bot as Online’

4) Click the ‘Add a Bot User’ to complete.

3. Submit Slack credentials to Bothub

# Gather credentials

1) Select ‘Basic Information’ tab.

2) The ‘Client ID’, ‘Client Secret’ and ‘Signing Secret’ are required for configuration of your Slack Bot.

# Setup Hosting for Bot and submit credentials

First, create an account on BotHub.Studio, a chatbot hosting service for developers, and install CLI tool using the command below:

$ pip install bothub-cli

Run configure next:

$ bothub configure

Create a project directory:

A template code will be generated after init finished.

$ mkdir MyBot
$ cd MyBot
$ bothub init

Now we connect with ‘Slack’ channel:

The ‘URL’ s that is result of command will be used ‘Slack’ setting page:

1) Enter command below command at the prompt.

bothub channel add slack --client-id=<client id> \
--client-secret=<client secret> \
--signing-secret=<signing secret>

2) Paste your Slack credentials from the Slack ‘Basic Information’ webpage.

3) Printed URLs are required for Slack setting.

  • Event Subscriptions URL
  • Interactivity URL
  • Redirect URL

4. Slack Setup

# OAuth setup

1) Select the ‘OAuth & Permissions’ tab.

2) Click ‘Add New Redirect URL’

3) Copy the ‘Redirect URL’ from ‘Bothub’ command output and paste to ‘Redirect URLs’

4) Click ‘Add’ and ‘Save URLs’

# Event Subscriptions setup

1) Select the ‘Event Subscriptions’ tab.

2) Click ‘Enable Events’ to On.

3) Copy the ‘Event subscriptions URL’ from ‘Bothub’ command output and paste to ‘Request URL’

4) Click ‘Add Bot User Event’ under the ‘Subscribe to Bot Events’

  • message.im
  • message.groups
  • message.channels
  • im_created

5) Click the ‘Save Changes’

# Interactive Components setup

1) Select the ‘Interactive Components’ tab and enable ‘Interactivity’

2) Copy the ‘Interactivity URL’ from ‘Bothub’ command output and paste to ‘Request URL’

3) Click the ‘Save Changes’

5. Add Slack Bot to a Team

1) Select ‘Manage Distribution’ tab.

2) Click the ‘Add to Slack’ button.

3) Click ‘Authorize’ button then Slack App will be installed in team.

6. Slack Bot Test

You can find installed Slack Bot at the your Workspace and you can communicate with Bot by inviting Bot to channel or selecting a Bot.

(If you want to install Bot at other Workspace, you should activate public distribution through ‘Share Your App with Other Workspaces’)

Should you have any questions, please feel free to contact bothub@bothub.studio anytime.

--

--