25 Oct How to build a Twitter bot posting data 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:
Omniscope data alerts bot on Twitter
Air pollution alert – https://t.co/5TcJZ0Vhv8 – daily average over limit: * Brindisi – Via dei Mille, PM10: 82.47 µg/m³;
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:
Arpa Puglia Monitor
The bottom right part (highlighted in yellow) is the one which aggregates the data to evaluate a sum, to then compare the measurements against the limits.
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/python-twitter/ , 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 twitter, sys
if len(input_data) <= 0:
api = twitter.Api(consumer_key="xxx",
message="Air pollution alert - https://bit.ly/ArpaPuglia24h - daily average over limit: "
for index, row in input_data.iterrows():
message+= " \n• "+ row['Centralina'] + ", " + row['Inquinante'] + ": " + str(row['Media giornaliera']) + " "+ row['Unita Misura']+";"
status = api.PostUpdates(message)
print("Your message could not be encoded. Perhaps it contains non-ASCII characters? ")
print("Try explicitly specifying the encoding with the --encoding flag")
output_data = pandas.DataFrame([message])
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 PostUpdates method is the one which posts a tweet on your page. In this case we are concatenating rows from the input data to build a message showing all measurements which are 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.