[PYTHON] A script that transfers tweets containing specific Twitter keywords to Slack in real time

Learn how to monitor Twitter and forward tweets containing specific keywords to Slack in real time. The script is python3, and the Twitter API statuses / filter.json is used to get tweets in real time.

System schematic

Imagine this system configuration. This article provides an example script for twitter_monitoring.py at the bottom left of this figure.

システム構成図03.png

The behavior of the script is to get tweets containing specific keywords with the Twitter API statuses / filter.json and have Slackbot post the tweets to any channel via the Slack API.

Advance preparation

Get ready to use the Twitter API and Slack API.

Create a Twitter API account

Token to be acquired

It's OK if you create a Twitter API account and get the following 4 tokens.

How to make

For how to create an account, the following site was helpful.

Creating a Slackbot

From the Slack API page, create a bot that will be the user for posting tweets.

Token to be acquired

After creating Slackbot, it is OK if you can get the following tokens from the ʻOAuth & Permissions` page of the Slack API management site.

How to make

For how to create Slackbot, the following site was helpful.

Scope (authority)

For the scope (permission) setting of Slackbot, it is enough to allow only chat: write for this purpose.

Python script

I will explain about the twitter_monitoring.py created this time.

Whole code

Below is a coding example of a Python script (45 lines in total).

twitter_monitoring.py


# coding: utf-8
import json
import logging
from time import sleep

import requests_oauthlib
import slack

# Parameters
keyword = 'keyword' #Specify any search keyword

# Twitter parameters
consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx' # Twitter API key
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Twitter API secret key
access_token = '9999999999999999999-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Twitter Access token
access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' # Twitter Access token secret

# Slack parameters
bot_token = "xoxb-999999999999-9999999999999-xxxxxxxxxxxxxxxxxxxxxxxx" # Slack Bot User OAuth Access Token
channel_id = 'xxxxxxxxxxx' # Slack channel ID

# Logging
logging.basicConfig(filename='twitter_monitoring.log', format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.WARNING)

#processing
uri = 'https://stream.twitter.com/1.1/statuses/filter.json'
twitter_session = requests_oauthlib.OAuth1Session(consumer_key, consumer_secret, access_token, access_token_secret)
slack_client = slack.WebClient(token = bot_token)

while True:
    try:
        logging.info("Request to Twitter API.")
        twitter_response = twitter_session.post(uri, data=dict(track=keyword), stream=True)
        if twitter_response.status_code != 200: #If you get caught in Twitter rate limit, etc.
            logging.warning("status_code:%s, reason:%s", twitter_response.status_code, twitter_response.reason)
            sleep(900) #Wait 15 minutes
            continue
        for line in twitter_response.iter_lines():
            if not line: #No corresponding tweet
                logging.info("No any tweets.")
                continue
            tweet_dict = json.loads(line.decode("utf-8"))
            tweet_link = 'https://twitter.com/i/web/status/' + tweet_dict['id_str']
            slack_response = slack_client.chat_postMessage(channel = channel_id , text = tweet_link) #Forward tweet links to Slack
    except Exception as e:
         logging.error(f'{e}')

Setting parameters

Since the code itself is short, this example embeds the configuration parameters in the script. Specify the values of various tokens and parameters in the variables below.

Classification Target Variable name Description
token Twitter consumer_key Twitter API key
token Twitter consumer_secret Twitter API secret key
token Twitter access_token Twitter Access token
token Twitter access_token_secret Twitter Access token secret
token Slack bot_token Slack Bot User OAuth Access Token
Parameters Twitter keyword Twitter search keywords
Parameters Slack channel_id Post to Slack Channel ID[^1]

[^ 1]: The end of the link URL for your Slack channel.

Running this script with common keywords for keywords can flood the destination Slack channel with a large number of tweets, so it's a good idea to try it first with a test Slack channel.

Logging settings

I've included the logging setting for debugging, but you can delete it if you don't need it. If you execute it as it is, the following operation will be performed.

--The log file of twitter_monitoring.log is output on the same directory at the time of execution. --To output the log defined in logging.info, change the level parameter of logging.basicConfig in the code to logging.INFO in advance.

Script execution method

Execute the Python script by the following method.

Python library

Install the required Python library in advance.

sudo pip install --upgrade pip
sudo pip install slackclient
sudo pip install requests_oauthlib
sudo pip install logging

Execution command

Execute the python script with the following command.

nohup python twitter_monitoring.py &

By running it with nohup, the script will continue to run even if you log out of the Python executor.

that's all. After execution, make sure that the link of the tweet containing the character string specified in keyword will continue to be transferred in real time to the destination Slack channel.

Remarks

-Twitter API Terms of Service

Recommended Posts

A script that transfers tweets containing specific Twitter keywords to Slack in real time
Save tweets containing specific keywords in CSV on Twitter
A Python program that collects tweets containing specific keywords daily and saves them in csv
Continue to retrieve tweets containing specific keywords using the Streaming API in Python
I made a program to collect images in tweets that I liked on twitter with Python
Use Heroku in python to notify Slack when a specific word is muttered on Twitter
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
How to stop a program in python until a specific date and time
I refactored "I tried to make a script that saves posted images at once by going back to the tweets of a specific user on Twitter".
The story of creating Botonyan that returns the contents of Google Docs in response to a specific keyword on Slack
Extract lines containing a specific "string" in Pandas
How to count numbers in a specific range
Get a row containing a specific element in np.where
A Python script that crawls RSS in Azure Status and posts it to Hipchat
How to unit test a function containing the current time using freezegun in python
A python script that wants to use Mac startup / end time for attendance management
A solution to the problem that files containing [and] are not listed in glob.glob ()
I made a script to put a snippet in README.md
A memorandum to run a python script in a bat file
What's in that variable (when running a Python script)
Try to delete tweets in bulk using Twitter API
Try face detection in real time using a webcam
[Note] A shell script that checks the CPU usage of a specific process in a while loop.