Jared Douville
2 min readFeb 12, 2024

--

Integrating X and Slack for Instant Notifications Using Python

Side by side: The iconic logos of Slack and X, representing innovation and collaboration in the digital age.

By Jared Douville

Utilizing modern tools such as the Slack platform, familiarizing oneself with the Slack API is a great tool for anyone in the tech industry. However, finding comprehensive tutorials can be challenging. Hence, I’ve crafted this guide to bridge the gap.

The objective is to create a simple yet valuable tool: immediate notifications to your team whenever your company or products are mentioned on X (formerly knows as twitter).

Here’s a walkthrough on connecting the X Streaming API and Slack Incoming Webhooks using Python. I won’t delve deeply into the mechanics of either API; you can consult their documentation for that. Instead, this tutorial aims to demonstrate a straightforward method of integration.

Setup:

Before proceeding, note that this tutorial employs Python 2. While Python 3 is preferable, Python 2 suffices for simplicity’s sake in this context.

Step 1: Set Up a X App & Obtain Your X Keys

Navigate to developer.twitter.com/en. After logging in, locate and click “Manage Your Apps.” Create a new app and fill in the required details. Once done, click on the “Keys And Access Tokens” tab. Generate your access token by clicking “Create My Access Token.” Keep this tab open; we’ll return to it shortly.

Step 2: Configure Your Slack App

Assuming you already have a Slack team set up, go to Slack.com/apps and select “Custom Integrations.” Choose “Incoming Webhooks” and add it if not already available. Select the channel for notifications and click “Add Incoming Webhooks Integration.” Optionally, customize integration settings like name, description, or icon. Step 3: Implementation

Now, let’s start sending tweets to Slack.

# Imports
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import requests

# Retrieve Twitter App Credentials
client_key = "YourConsumerKeyGoesHere"
client_secret = "YourConsumerSecretGoesHere"
token = "YourAccessTokenGoesHere"
token_secret = "YourAccessTokenSecretGoesHere"

# Send Notification to Slack
def notify_slack(text):
url = "URLToYourSlackChannel"
payload = {"text": text}
r = requests.post(url, data=json.dumps(payload))
print(r.text)

# Stream Listener Class
class TweetListener(StreamListener):

def on_data(self, data):
tweet = json.loads(data)
# Send Tweet To Slack Notification
notify_slack(tweet['text'])
return True

def on_error(self, status):
print("Error: %s" % status)

listener = TweetListener()
auth = OAuthHandler(client_key, client_secret)
auth.set_access_token(token, token_secret)
stream = Stream(auth, listener)
stream.filter(track=['festivus'])

That’s it. Run the script, and in a few moments, tweets will start appearing in your Slack channel.

What’s Next:

Enhance the script to include more information, such as usernames or tweet links. Modify the script to monitor specific keywords or users. Based on interest.

Useful Links:

Slack API: https://api.slack.com

X Streaming API: API v2 — X Developers

--

--

Jared Douville

32 year old Cyber Security Specialist and freelancer writer from Calgary , Canada. I own and operate a cyber security start up called Alberta Cyber Security.