How to create a Twitter / X bot posting data driven alerts

In this post I am going to show you how easy is to build a data workflow capable of sending alerts on Twitter, all integrated in one solution.

This is something we have done to help measuring air pollution in Puglia (Italy), but you can apply this concept to anything. For instance you might want to Tweet financial news when data meets a certain threshold, or tweet an update on your company page when a new feature is released.

Step by step, here is described how we built and power this Twitter Omniscope data alerts bot – @OmniscopeBot.

An example tweet:

1. Setup your Twitter developer account and create your app

To build your bot the first thing to do is to apply for a Twitter developer account on developer.twitter.com .

You can build your profile for personal use or for your organisation, either ways Twitter needs to know what’s the reason for the developer account, so you have to specify the details to get approved, making sure to be thourough in the explaination.

Once your account is verified you can create your Twitter app, which it will be in practice used by your bot to tweet.

The main things to obtain once you have created your Twitter app are: the API keys and access tokens. They allow a script (e.g. a Python script) to communicate with your Twitter app.
On the Twitter app page, go to the App details to obtain the 4 keys and tokens. N.B. anyone with these keys will be able to access your account, keep them safe!

 

2. Setup the data workflow

In our case the workflow is obtaining data from a remote service, so we are pulling data in Omniscope, cleaning, transforming, and then we have a path to validate the data: any measurement above limit will be gathered, ready to be used to compose the text to tweet.

This is the resulted workflow:


The bottom right part is the one which aggregates the data to compare the measurements against the limits. We also generate a screenshot of the report to be added to the post on X (Twitter).
If some rows are produced by the record filter block (the data over the limits, signifying anomalies) the information is sorted and passed to the Python – Twitter block, where the tweeting happens.

 

3. Setup the Python to post updates / alerts

To tweet we use a very common Python library https://pypi.org/project/tweepy/ , which allows you to authenticate to your account through the API keys and tokens, post updates to your account and more.

This is the content of the Custom Python block in Omniscope which gets the input data, builds a message and tweets on the bot account.

import tweepy, sys, pandas
from omniscope.api import OmniscopeApi
omniscope_api = OmniscopeApi()

# read the records associated to the first block input
input_data = omniscope_api.read_input_records(input_number=0)

if len(input_data) <= 0: 
    omniscope_api.close() 
    sys.exit(2)

#Omniscope Bot 
from requests_oauthlib import OAuth1Session 
import os 
import json 

api_key = "xxxx" 
api_secret = "xxxx" 
access_token="xxxx" 
access_token_secret="xxxx" 

msg="" 

for index, row in input_data.iterrows(): 
    msg+= row['Messaggio'] + " https://bit.ly/BenzeneTamburi" 


def get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret) -> tweepy.API:
    """Get twitter conn 1.1"""

    auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret)
    return tweepy.API(auth)

def get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret) -> tweepy.Client:
    """Get twitter conn 2.0"""

    client = tweepy.Client(
        consumer_key=api_key,
        consumer_secret=api_secret,
        access_token=access_token,
        access_token_secret=access_token_secret,
    )

    return client

client_v1 = get_twitter_conn_v1(api_key, api_secret, access_token, access_token_secret)
client_v2 = get_twitter_conn_v2(api_key, api_secret, access_token, access_token_secret)

media_path = "/home/ubuntu/omniscope-server/files/internal/Pollution/Pics/screenshot_0.png"
media = client_v1.media_upload(filename=media_path)
media_id = media.media_id

client_v2.create_tweet(text=message, media_ids=[media_id])
  
output_data = pandas.DataFrame([msg])

#write the output records in the first output
if output_data is not None:
    omniscope_api.write_output_records(output_data, output_number=0)
    omniscope_api.write_output_records(input_data, output_number=1)
omniscope_api.close()

Note that the consumer_key, consumer_secret, access_token_key, access_token_secret have been obscured, and those are the only bits you have to customise to allow the script communicating with your Twitter app.

The create_tweet method is the one which posts a tweet on your page. In this case we are posting a message with the excess of benzene level measured, and a screenshot of the report showing the graph with the measurements above a certain limit.

 

4. Automate the workflow

The last step obviously is to automate the workflow, to run at certain time / interval. This is done by setting up a Omniscope Scheduler task which re-execute the whole workflow to get new data, validate the information and tweet in case of anomalies.

Enjoy!

 

 

 

No Comments

Leave a Reply

Discover more from Visokio

Subscribe now to keep reading and get access to the full archive.

Continue reading