Treat these tokens just as you would a password – don’t publish them, don’t check them into source code, don’t share them with others. Our bot, which we will name "StarterBot", requires Python and the Slack API. Go back to the command line where your Flask app is running.

your workspace for changes to take effect. . See the Basic Usage section of this documentation for usage examples. Then test that our ngrok forwarding URL is properly connected to our Flask app by opening your web browser and going to the Forwarding URL.

You can check out our Python Slack Events Adaptor if you want to use this API instead. It’s the programmatic access to retrieve and send messages with the Slack web application programming interface (API) where the power really kicks in. Execute the script from the command line with python

Community. We will grab an API access token and write some Python code to list, retrieve and send data through the API.

If you need new tokens to interact with the Slack API, create a Slack app instead. In this post, we’ll see how to work with Slack via the API and the official SlackClient Python helper library. The OAuth token you use to call the Slack API has access to the data on the workspace where it is installed.

To generate a Slack API token, you'll need to run do the following: Create a new Slack App; Add permissions; Copy the token URL; Create a new Slack App. So we can send messages, but what about if we want to see what users in the #general channel are saying? Unsubscribe any time. It requires some general programming knowledge, and Python basics. flask

The person who set up the bot can regenerate the token using the instructions below: Only the creator or collaborators can reinstall an app to their workspace. All done! Various trademarks held by their respective owners. Note: This article mentions legacy components, an older method of adding integrations to your workspace. There are also other options such as localtunnel and forward. Here is what ngrok looks like in the console when it’s started with the ./ngrok http 5000 command: Take note of your Forwarding URL, in this case, as we’ll need that for Slack to set up our outgoing webhook. Add permissions. We’ll need that test token in just a moment, so keep it handy. chat. Sorry about that! Legacy test tokens. Go to the Slack Outgoing Webhooks page, then click the “outgoing webhook integration” link as shown below: Scroll down to the Integration Settings section. You’ll see output like the following channels list: What is the channel ID that we printed out in parentheses next the the channel name for? © 2012–2020 Real Python ⋅ Newsletter ⋅ Podcast ⋅ YouTube ⋅ Twitter ⋅ Facebook ⋅ Instagram ⋅ Python Tutorials ⋅ Search ⋅ Privacy Policy ⋅ Energy Policy ⋅ Advertise ⋅ Contact❤️ Happy Pythoning! web-dev

Time to give it a try. Dive into your favorite text editor such as Vim, Emacs, or Sublime Text so we can cut some new Python code.

Curated by the Real Python team. Finally, it’s time to test out receiving messages!

We need a localhost tunnel that will give us an externally-accessible domain name while we’re developing our code. If you need new tokens to interact with the Slack API, create a Slack app instead. For example, software developer communities such as DC Python, Dallas-Forth Worth Devs, and Denver Devs set up their own Slack channels. intermediate A successful request to oauth.v2.access will yield a JSON payload with at least one token, a bot token that begins with xoxb. We can go even further into the Slack API now that we know our API calls are working and have the channel ID. We’re authorized to start using the Slack API through our account. Follow the link, and click Create New App. In addition, modify the main function so that when we run this file, main will call our new send_message function: Save the changes and run python intermediate

Add a new function under channel_info named send_message: send_message takes in the ID for a channel, then posts a message from our “Python bot” to that channel. Back on the command line, export the Slack token as an environment variable: We’ll snag the environment variable in our Python script using the os module instead of hardcoding it into the source code. To run our Python code we need: Either Python 2 or 3; pip and virtualenv to handle Python application dependencies Leave a comment below and let us know. If you’re building an application for a single Slack workspace, there’s no need to build out the entire OAuth flow. We're having trouble. Share

Create a new file named and start filling it out with the following imports: Again, the os module will be used to pull the SLACK_TOKEN environment variable we just exported. Scroll to find the workspace and user that you'd like to re-issue a token for. Python 3.5.0 (v3.5.0:374f501f4567, Sep 12 2015, 11:00:19), [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin. Email. Go to your Slack #general channel. We’re set to receive our POST request webhook, except that most development environments do not expose routes beyond localhost. Let’s send a message to the #general channel.

Almost there! Next let’s interact with other users in one of our channels by sending and receiving messages. You can add a token to the environment by starting your app as: For additional information, please see our Safely Storing Credentials page. If you need new tokens to interact with the Slack API. For simplicity, we’re only going to install and use slackclient, but you can also try out libraries like slacker, slack and pyslack once we’re done here. Join us and get access to hundreds of tutorials, hands-on video courses, and a community of expert Pythonistas: Real Python Comment Policy: The most useful comments are those written with the goal of learning from or helping out other readers—after reading the whole article and all the earlier comments. Stuck at home? Now that we know what tools we need to use, let’s begin by creating a new virtualenv to isolate our application dependencies from other Python projects you’re working on: Depending on how your virtualenv and shell are set up, your prompt should now look something like this: Keep the shell open for now as we get our Slack access established via the official slackclient API helper library built by Slack. Note: API tester tokens are quite powerful and it is a best practice to revoke them if they are not being used. In the menu on the left, find OAuth and Permissions.
What’s your #1 takeaway or favorite thing you learned?

Here are several more ideas to try out now that you’ve got the basics down: If you have questions feel free to drop a comment below or contact me via: Get a short & sweet Python Trick delivered to your inbox every couple of days. Connect, simplify, and automate.

You can follow along by writing the code in this post or clone the companion GitHub repository with the finished project. Join us and get access to hundreds of tutorials, hands-on video courses, and a community of expert Pythonistas: Master Real-World Python SkillsWith Unlimited Access to Real Python. We should see the “It works!” message. © 2020 Slack Technologies, Inc. and contributors, '