In the present day’s tutorial will train you learn how to create a Twitter bot for crypto utilizing Python, Moralis, and Twitter’s developer portal. Because of Moralis’ Streams API, you’ll be able to take heed to on-chain occasions in real-time, which you should utilize to set off your Twitter bot and tweet the small print associated to the focused on-chain occasions. For the sake of this tutorial, we’ll deal with utilizing a Twitter bot to publish whale alerts.
Now, to create this bot, you have to acquire particular particulars from the developer portal after which use these particulars to authenticate Twitter in your script. However basically, you’ll be able to create the bot with these three traces of code:
def send_tweet(tweet):
api.update_status(tweet)
print(“Tweeted: {}”.format(tweet))
To show your bot right into a Twitter bot for crypto, the facility of Moralis’ Streams API and Flask will do the trick. Moreover, the next filter will allow you to deal with transfers of 1 million Tether (USDT) or extra:
[
{
“topic0”: “Transfer(address,adress,uint256)”,
“filter”: {
“gt”: [
“value”,
1000000000000
]
}
}
]
You probably have some expertise with Python and have labored with Moralis’ Streams API earlier than, you in all probability already know learn how to implement the above snippets of code. Nevertheless, in case you want some assist, make sure that to comply with our lead. In case you’re new round right here, don’t forget to create your free Moralis account!
Overview
We’ll begin this text by diving straight into the tutorial on learn how to create a Twitter bot for crypto. That is the place we’ll cowl all of the steps it is advisable full from begin to end. Moreover, we’ll present you learn how to get began with Twitter’s developer portal, what settings it is advisable tweak, and learn how to acquire the mandatory particulars. Then, we’ll information you thru the method of making a Twitter bot utilizing the above-presented traces of code. In fact, we may also present you learn how to set your key and token in place. That is additionally the place you’ll discover ways to use the Tweepy Python library to entry the Twitter API. At that time, you’ll know learn how to create a Twitter bot.
Our goal will probably be for our bot to deal with blockchain occasions. Now, to create a Twitter bot for crypto, you’ll need to use Moralis’ Streams API to start out listening to the specified on-chain occasions. Lastly, we’ll present you learn how to tie all of it along with a easy Python script.
Beneath the tutorial, you’ll additionally discover sections protecting theoretical points of in the present day’s subject. That is the place you’ll be able to be taught what a Twitter bot is, the gist of Moralis, and what makes Moralis the very best crypto instrument for Web3 devs.

Tutorial: Create a Twitter Bot for Crypto
As defined above, our tutorial will undergo the next 4 steps:
Establishing your Twitter developer portal account, creating a brand new app, and tweaking the suitable settings. That is the place you’ll generate your Twitter API key and token. Utilizing Python and the Tweepy library to create your Twitter bot.Creating a brand new stream to fetch real-time, on-chain knowledge concerning massive transfers of USDT utilizing Moralis’ admin UI.Utilizing Flask and the knowledge your stream supplies you with to create a Twitter bot for crypto whale alerts.
Step 1 – Twitter Developer Portal Setup
Begin by signing up for a Twitter developer account. Go to “developer.twitter.com“ and click on on the “Enroll” button on the “Getting began” web page:

When you efficiently join, you’ll have the ability to entry your developer portal panel:

Because you need your bot to have the ability to publish tweets, you additionally have to allow elevated entry. To do that, choose the “Twitter API v2” possibility below “Merchandise” and go to the “Elevated” tab to enter the mandatory particulars:

As you’ll be able to see within the above two screenshots, now we have already created the “Moralis Bots” mission. You’ll be able to comply with our lead naming your mission the identical method or use every other identify. As soon as in your mission web page, scroll down and hit the “Add App” button:

Subsequent, choose the “Manufacturing” possibility:
Then, identify your app (once more, be happy to make use of the identical identify as ours):
On the third step of your app setup, you’ll have the ability to see your API key and API key secret. Nevertheless, you’ll have to generate new ones after altering some parameters. So, click on on the “App setting” button. On the following web page, scroll down and hit “Arrange” below “Consumer authentication settings”:
On the person authentication settings web page, choose the “Learn and write” possibility:
Subsequent, choose the “Internet App, Automated App or Bot” possibility:
Within the “App data” part, use Twitter’s URL for each of the next choices:
Scroll to the underside of the web page and click on on “Save” adopted by “Sure”:
Notice: We gained’t be utilizing “Shopper ID” and “Shopper Secret” in in the present day’s tutorial; nonetheless, we advocate copying these two particulars someplace secure earlier than clicking on “Achieved”:
Shifting on, choose the “Keys and tokens” tab, the place it is advisable regenerate your Twitter API key and your authentication tokens:
Step 2 – Create Your Twitter Bot with Python and Tweepy
First, create your “.env” file and populate it with the above-obtained key and token particulars:
Notice: At all times maintain your keys and tokens non-public. We confirmed them to you as a result of we’ll delete our bot as soon as we publish this tutorial.
Subsequent, create a “twitter_bot.py” file. Each the above “.env” and “twitter_bot.py” information needs to be inside the identical mission folder (e.g., “twitter-bot-whale-alerts”). On the high of this new Python script, import Tweepy, “dotenv”, “os”, and cargo “dotenv”. Listed here are the traces of code that cowl that:
import tweepy
from dotenv import load_dotenv
import os
load_dotenv()
Additionally, don’t forget to put in the Tweepy library utilizing the next command:
pip set up tweepy
Subsequent, it is advisable add the keys and tokens you saved within the “.env” file:
CONSUMER_KEY = os.getenv(“TWITTER_API_KEY”)
CONSUMER_SECRET = os.getenv(‘TWITTER_API_SECRET_KEY’)
ACCESS_TOKEN = os.getenv(‘ACCESS_TOKEN’)
ACCESS_TOKEN_SECRET = os.getenv(‘ACCESS_TOKEN_SECRET’)
With the above particulars in place, you’ll have the ability to entry the Twitter API with these traces of code:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
Lastly, you might have every little thing prepared to make use of the snippet of code offered within the intro, which can create your Twitter bot:
def send_tweet(tweet):
api.update_status(tweet)
print(“Tweeted: {}”.format(tweet))
Notice: You’ll be able to entry an entire script through the “twitter_bot.py” hyperlink above. Additionally, in case you want to take a look at your code at this stage, use the video on the high of the article, beginning at 5:49.
Step 3 – Acquire Actual-Time, On-Chain Information with Moralis Streams
To make use of Moralis’ Streams API, you want a Moralis account. So, in case you haven’t finished so but, use the “create your free Moralis account” hyperlink within the intro. Together with your account prepared, you’ll have the ability to entry your admin space. There, choose the “Streams” tab. Subsequent, click on on the “Create a brand new stream” button:
Then, paste within the Tether sensible contract tackle (on this case, you’ll be able to merely choose it among the many given examples):
Subsequent, it is advisable present some particulars contained in the “Stream Configuration” part:

You’ll be tying your stream and your Twitter bot collectively within the subsequent part utilizing the “index.py” script. Nevertheless, at this level, it is advisable acquire your webhook URL. As such, run that script (not but in its ultimate type) to create a easy server. These are the traces of code you want at this stage in your “index.py” script:
import json
from flask import Flask, request
import locale
from twitter_bot import send_tweet
locale.setlocale(locale.LC_ALL, ‘en_US.UTF-8’)
app = Flask(__name__)
@app.route(“/webhook”, strategies=[“POST”])
def webhook():
# Course of the request knowledge right here
webhook = request.knowledge.decode(“utf-8”)
json_object = json.masses(webhook)
print(json_object)
return “OK”
if __name__ == “__main__”:
app.run(port=5002)
Use the next command to run the above script:
python index.py
In consequence, you’ll have a easy server working on localhost 5002:
To lastly get your webhook URL, you should utilize ngrok for the above port (5002) with this command line:
ngrok http 5002
The above command will create a tunnel to your localhost and offer you a URL tackle that you should utilize as your webhook URL:
Copy the tackle and paste it into the “Webhook URL” entry discipline (add “/webhook”). Additionally, enter an outline and tag:
Choose the Community and Acquire the Good Contract ABI
Within the third step of your stream setup, you get to pick out networks. Since Ethereum is toggled by default, you don’t need to do something. In spite of everything, Tether is an ERC-20 token based mostly on the Ethereum blockchain. Nevertheless, in case you have been to focus on different chains, you’d have to toggle them on this step.
Subsequent, be sure you solely choose the “Contract Interactions” possibility:
Within the “Superior Choices” part, it is advisable enter your sensible contract’s ABI (the one you need to take heed to):
So, use the Tether tackle and paste it into Etherscan’s search bar:
Then, choose the “Contract” tab:
As soon as on the “Contract” tab, scroll down till you see “Contract ABI”, which incorporates the small print it is advisable copy:
As quickly as you paste the ABI into the entry discipline within the “Superior Choices” part, Moralis supplies you with the accessible subjects. Since we need to deal with USDT transfers, we have to choose the “Switch” operate:
Since we need to deal with massive (whale) transfers, we should additionally implement an acceptable filter. That is the place the second snippet of code offered on the high of the article comes into play. Paste these traces of code and click on on “Replace”:
Lastly, change your stream from demo to manufacturing:
Step 4 – Provide Your Twitter Bot for Crypto with the Info from Your Stream
At this level, you might have your Twitter bot prepared, and you’re getting the details about all USDT transfers bigger than a million. This implies you solely want to produce your bot with the knowledge that the above-created stream fetches. To attain this, you have to tweak your “index.py” script accordingly.
For starters, you do not need to print your complete response however deal with particular data. A “attempt” assertion will do the trick. You additionally need to outline the items of data the stream returns. That is the up to date “webhook” operate:
def webhook():
# Course of the request knowledge right here
webhook = request.knowledge.decode(“utf-8”)
json_object = json.masses(webhook)
attempt:
switch = json_object[“erc20Transfers”][0]
besides IndexError:
return “OK”
sender = switch[“from”]
receiver = switch[“to”]
worth = switch[“value”]
token_name = switch[“tokenName”]
transaction_hash = switch[“transactionHash”]
handle_response_and_tweet(sender, receiver, worth,
token_name, transaction_hash)
return “OK”
To make use of the above parameters in a tweet, we have to add one other operate – “handle_response_and_tweet” – into the “index.py” file:
def handle_response_and_tweet(sender, receiver, worth, token_name, transaction_hash):
sender = sender[:6] + “…” + sender[-3:] + “…”
receiver = receiver[:6] + “…” + receiver[-3:] + “…”
worth = “${:,.6f}”.format(float(worth)/1000000)
transaction_hash=”https://etherscan.io/tx/” + transaction_hash
tweet = f”New Whale Alert! {sender} despatched {worth} {token_name} to {receiver}! in transaction {transaction_hash}”
send_tweet(tweet)
The above “sender” and “receiver” traces guarantee we solely tweet the primary six and the final three characters of a sender’s and receiver’s addresses. In spite of everything, tweets have a restricted quantity of characters. Relating to “worth“, the above code provides the USD image and codecs the quantity in line with the truth that USDT makes use of six decimals. So far as “transaction_hash” goes, it merely follows Etherscan’s format. Furthermore, “tweet” constructs the message that we would like our Twitter bot for crypto to tweet:

Notice: You’ll be able to entry the entire “index.py” script on GitHub.
What’s a Twitter Bot?
A Twitter bot is a particular form of app that has the facility to manage a Twitter account. It does so via the Twitter API, which devs can entry through their Twitter developer portal accounts (as demonstrated above). Moreover, Twitter bots can carry out varied actions, together with posting tweets, re-tweeting, following, unfollowing, liking, and even direct-messaging different accounts. Out of those, the above tutorial confirmed you learn how to use a Twitter bot for tweeting.
Finest Crypto Device for Builders
Relating to Web3 improvement and creating dapps (decentralized functions), there are lots of helpful instruments. Nevertheless, due to its simplicity, cross-chain functionalities, and cross-platform interoperability, Moralis stands out. It serves as a instrument that bridges the event hole between Web2 and Web3. It allows devs to make use of legacy programming languages, frameworks, and platforms to affix the Web3 revolution. In the present day’s tutorial is a superb instance of that – enabling Python-proficient devs to arrange a Twitter bot for crypto alerts.
Within the above tutorial, you had an opportunity to expertise the facility of Moralis’ Streams API. Nevertheless, that is simply one of many three core merchandise this enterprise-grade Web3 API supplier gives. Here’s a neat structure of Moralis’ API fleet:
You’ve already realized and even skilled how the Streams API allows you to take heed to on-chain occasions. The Web3 Information API lets you fetch any on-chain data and get it parsed. Amongst many different options, Moralis can also be an ENS resolver. Additionally, Moralis allows you to add blockchain-based knowledge storage. As for the Web3 Auth API, it enables you to equip your dapps with the preferred Web3 log-in strategies.
As Web3 expertise evolves, so does Moralis. It’s continuously enhancing and perfecting its services and products. Therefore, Moralis’ sources additionally repeatedly add increasingly more worth. As an illustration, now you can use Moralis’ Pure Taps web page to entry user-friendly and hustle-free taps, together with the very best Ethereum faucet and the Web3 market’s main Polygon Mumbai faucet. So, take advantage of what Moralis has to supply; join in the present day!
Create a Twitter Bot for Crypto – Abstract
The core of in the present day’s article was our tutorial exploring learn how to create a Twitter bot for crypto. The tutorial took you thru the next 4 steps:
Twitter developer portal setupCreate your Twitter bot with Python and TweepyObtain real-time, on-chain knowledge with Moralis StreamsSupply your Twitter bot for crypto with the knowledge out of your stream
As a part of the third step, you additionally realized learn how to choose a community or extra of them and acquire a sensible contract ABI as a part of establishing a brand new stream. Other than the tutorial, you additionally realized what a Twitter bot is and what makes Moralis the very best crypto instrument for builders.
If you happen to loved making a Twitter bot for crypto, we encourage you to additionally discover our NodeJS Telegram bot tutorial. You may additionally need to cowl our Web3.py vs Web3.js comparability. On the Moralis weblog, you too can be taught the gist of sensible contract programming, what danksharding is, discover out which the last word NFT commonplace is by diving into the ERC721 vs ERC1155 comparability, and way more.
Other than our crypto weblog, you additionally don’t need to miss out on sensible video tutorials that await you on the Moralis YouTube channel. Lastly, in case you want to change into blockchain-certified, Moralis Academy is the place to be. There, you’ll discover numerous blockchain improvement programs; nonetheless, we encourage you to first get your blockchain and Bitcoin fundamentals straight.