If you’re questioning easy methods to arrange automated Web3 notification emails however need assistance determining the place to start out, this text is for you! Even if you happen to’re a whole newbie in blockchain growth, we’ll aid you catch up. In any case, we’ll begin by explaining what Python and Flask are. As soon as we ensure you’re all up to the mark, we’ll tackle an instance undertaking the place you’ll have an opportunity to arrange automated Web3 notification emails with Python and Web3 webhooks.
Moralis is the final word Web3 supplier of blockchain APIs. As such, it’s an indispensable device if you wish to create decentralized purposes (dapps) rapidly and simply. Moreover, Moralis’ cross-chain interoperability allows you to create multi-chain dapps. You solely must tweak a single line of code to focus on one other chain. As well as, Moralis allows you to be a part of the Web3 revolution utilizing the legacy instruments you might be acquainted with. Whereas we’ll deal with utilizing Python on this article, Moralis helps all main legacy dev platforms, frameworks, and languages. Basically, Moralis allows you to construct quicker and smarter with APIs that bridge the event hole between Web2 and Web3.
Except for its Streams API, which we’ll use to arrange automated Web3 notification emails, Moralis gives a full scope of Web3 APIs. This contains the final word NFT API, Web3 Auth API, Token API, and extra. So, create your free Moralis account and observe our lead!
What’s Python and Flask?
Python is a transparent and highly effective object-oriented programming language. In some ways, it’s protected to check Python to a number of different coding languages. These embody Ruby, Scheme, Perl, and Java.
It’s price stating that Python will not be as common and as extensively used as JavaScript. Nevertheless, it’s nonetheless among the many main programming languages, and Python has an extended historical past. In any case, it first appeared on the scene again in 1991, because of its developer Guido van Rossum. The primary objective of Python was to make issues as easy as doable for builders. Primarily based on the opinions of those that know and use this language, Python is fairly straightforward to get began with, be taught, and straightforward to make use of. Nonetheless, as “python.org” places it, Python is a programming language that allows you to work rapidly and combine techniques extra successfully.
Flask is an internet framework, a Python module that allows you to develop net purposes simply. Additional, Flask has a small and easy-to-extend core. In any case, it’s a microframework that doesn’t embody an ORM (object relational supervisor) or options associated to ORM. Nonetheless, Flask has many glorious options. As an example, it contains URL routing and a template engine. As well as, word that Flask is an internet server gateway interface (WSGI) net app framework. Furthermore, this Python framework was developed by Armin Ronacher, who led Poocco – a staff of worldwide Python lovers. Therefore, it’s not shocking that Flask relies on two different Poocco initiatives – the Werkzeg WSGI toolkit and the Jinja2 template engine.
Set Up Automated Web3 Notification Emails with Python and Moralis’ Streams API – Instance Undertaking
Now that you realize what Python and Flask are, it’s time we present you the automated Web3 notification e-mail setup course of. Nevertheless, we consider it’s only truthful if you happen to first see the top results of utilizing Python and Moralis for this goal. Therefore, let’s do a fast demo of our instance backend dapp earlier than illustrating easy methods to arrange automated Web3 notification emails!
Setup of Automated Web3 Notification Emails – Demo
To display the outcomes of automated Web3 notification emails, we’ll use a Gmail account, a blockchain explorer (PolygonScan), and the preferred Web3 pockets – MetaMask.
First, let’s have a look at the next screenshot, which demonstrates that we’re beginning with an empty e-mail inbox:
We open the PolygonScan (for the Mumbai testnet) explorer since Mumbai is the testnet the place our sensible contract lives. As you could know, blockchain explorers additionally allow you to work together with deployed sensible contracts. Therefore, we open the web page for our instance sensible contract’s handle and choose the “Write Contract” choice contained in the “Contract” tab:
Then, as you possibly can see within the above screenshot, we enter “2” (this could possibly be any quantity our steadiness can cowl) within the “newDonation” subject and hit the “Write” button. This button triggers our MetaMask extension, which is related to the Polygon Mumbai testnet. As you possibly can see above, we have to affirm the transaction notification by clicking on the “Affirm” button. After this motion, our terminal notifies us that an e-mail has been despatched:
After we return to our e-mail inbox, we will see the “New Donation” e-mail:
Lastly, let’s additionally test the main points of this instance notification e-mail:
Trying on the above screenshot, you possibly can see that we’ve arrange automated Web3 notification emails that embody donors’ pockets addresses and the quantity they donated in MATIC (native foreign money for the Polygon chain).
In the event you’d wish to arrange this type of e-mail notification automation for on-chain occasions, be certain that to roll up your sleeves and observe our lead all through the sections under.
Set Up Automated Web3 Notification Emails with Python and Moralis’ Streams API – Step-by-Step Tutorial
On this a part of at present’s article, you’ll have an opportunity to observe our lead as we information you thru the next 4 levels:
Set Up FlaskInitial Setup of Moralis and the Streams APIReceive Web3 WebhooksAutomate Emails
We’ll give you screenshots that will help you correctly full every of the above 4 levels. Nevertheless, on the backside of this text, you too can discover a detailed video tutorial on easy methods to arrange automated Web3 notification emails. Observe that we are going to sometimes check with particular timestamps of that video shifting ahead.
Set Up Flask
For readability, we encourage you to observe our instance and use Visible Studio Code (VSC). Subsequent, create the “automateEmails” folder and open it in VSC. Then, you can begin your Python digital setting by getting into the next command:
python3 -m venv venv
The above command will create a “venv” folder contained in the “automateEmails” folder:
Transferring on, you wish to run this command line:
Supply venv/bin/activate
As a affirmation that you simply’ve efficiently activated your digital setting, you need to see “(venv)” in your terminal:
Together with your digital setting operating, proceed by putting in the newest model of “pip” with the next command:
pip set up –upgrade pip
Subsequent, it’s time to put in all dependencies. Therefore, enter this command:
pip set up flask flask_cors
With the dependencies put in, go to your undertaking tree and create a brand new Python file. Be at liberty to name it as you please. Nevertheless, to keep away from any confusion, it could be safer to make use of the identical identify as we – “backend.py”:
Then, import a pattern Flask app utilizing the next traces of code:
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route(‘/streams’, strategies=[“POST”])
def streams():
return jsonify(success=True)
if __name__ == “__main__”:
app.run(host=”127.0.0.1″, port=3000, debug=True)
The above template code contains an endpoint you should use as a webhook URL. As such, you possibly can ship blockchain occasions as webhooks to that endpoint utilizing Moralis’ Streams API. By this level, you realize that the latter is the important thing to setup automated Web3 notification emails the simple manner.
Preliminary Setup of Moralis and the Streams API
Transferring on, you’ll want so as to add correct performance to deal with the Web3 webhook you’ll obtain from Moralis. As such, you’ll be including many of the extra code inside “@app.route” (see above). Nevertheless, first, it’s worthwhile to full the preliminary Moralis setup. Thus, ensure you have your Moralis account up and operating. In case you haven’t created it but, you should use the hyperlink said on the outset or click on on the “Begin for Free” button on Moralis’ homepage:
Together with your Moralis account prepared, you’ll have the ability to entry your admin space. There, you’ll wish to choose the “Streams” tab from the facet menu. As soon as on the “Streams” web page, you wish to click on on the “New Stream” button:
Within the subsequent step, you get to pick one of many stream templates or create one from scratch, which is what we’ll do to arrange automated Web3 notification emails. Therefore, be certain that to click on on the “Create From Scratch” button:
Subsequent, you’ll must enter the main points for the sensible contract in query. In fact, you should use any sensible contract you would like. Nevertheless, for the sake of this instance undertaking, we advocate you deal with the above-presented “donationExample” sensible contract (4:39). You possibly can receive all the main points concerning this sensible contract through the use of the above-seen PolygonScan web page. On the high, it’s worthwhile to paste the contract’s handle:
As you possibly can see above, you additionally want to present your stream an outline. Then, you additionally want to supply your webhook URL, add a tag, and choose networks. With all the main points in place, you’ll have the ability to hit the “Create Stream” button. For extra particulars, use the video under, beginning at 5:26.
Obtain Web3 Webhooks
With the above stream created, you’ll have the ability to begin receiving Web3 webhooks mechanically. To see this in motion, be certain that to observe the video under (9:12) and execute an instance donation. If you wish to do that, you’ll want a MetaMask extension and a few take a look at MATIC, which you will get utilizing the Polygon faucet. Basically, you’ll get to execute the identical steps as offered within the above demonstration.
Because you haven’t arrange automated Web3 notification emails but, you’ll solely view the leads to your terminal. Nevertheless, you’ll see that you simply get all the data you want. Therefore, you solely must set off “e-mail sending” and embody the specified particulars (donor’s handle and donated quantity) in these notification emails.
At 11:57 of the video, you too can learn to pause your streams whereas nonetheless creating your dapp:
Automate Emails: The Setup of Automated Web3 Notification Emails
To finish the ultimate stage of at present’s tutorial, it’s worthwhile to refocus on the “backend.py” script. First, it’s worthwhile to import “EmailMessage“, “ssl“, and “smtlib” on the high. Subsequent, you will need to initialize some key variables. In the end, that is how the highest twelve traces of “backend.py” ought to seem like:
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
from e-mail.message import EmailMessage
import ssl
import smtplib
emailPass = “”
e-mail = “”
emailReceiver = “”
topic=”New Donation”
Trying on the traces of code above, you possibly can see that it’s worthwhile to present your e-mail handle subsequent to “e-mail” and “emailReceiver”, like so:
In fact, you could possibly ship notification emails to any e-mail handle (or checklist of addresses) you need. Nonetheless, be certain that to make use of the video under, beginning at 13:13, and learn to receive your “emailPass” from Gmail.
Lastly, it’s time so as to add the required traces of code inside “@app.route”:
@app.route(‘/streams’, strategies=[“POST”])
def streams():
if (request.json[‘confirmed’]):
return jsonify(success=True)
particulars = request.json[“txs”]
for donation in particulars:
quantity = int(donation[‘value’])/1000000000000000000
em = EmailMessage()
em[‘From’] = e-mail
em[‘To’] = emailReceiver
em[‘Subject’] = topic
em.set_content(donation[‘fromAddress’] + ” has simply despatched you ” + str(quantity) + ” in MATIC!”)
context = ssl.create_default_context()
with smtplib.SMTP_SSL(‘smtp.gmail.com’, 465, context=context) as smtp:
smtp.login(e-mail, emailPass)
smtp.sendmail(e-mail, emailReceiver, em.as_string())
print(“E mail Despatched”)
return jsonify(success=True)
Once more, we advocate you employ the video under for a extra detailed code walkthrough. Furthermore, you possibly can entry the ultimate code on GitHub. Final however not least, right here’s the video tutorial containing all the main points required to efficiently arrange automated Web3 notification emails:
Set Up Automated Web3 Notification Emails with Python – Abstract
We coated fairly a distance in at present’s article. Beginning with the fundamentals, we answered the “what’s Python and Flask?” query. Then, we dove proper into the method of establishing automated Web3 notification emails. As such, you noticed that an e-mail is shipped informing us about that on-chain occasion as quickly as an on-chain transaction is executed. Lastly, we additionally took you thru the 4 foremost levels it’s worthwhile to full to arrange automated Web3 notification emails. Consequently, you now know easy methods to simply take heed to sensible contract occasions and be certain that these occasions set off notification emails.
If , use the Web3 Python SDK documentation, our blockchain-development movies, and our crypto weblog to develop your information of blockchain growth additional. These shops give you numerous tutorials and blockchain growth subjects. As an example, a number of the newest articles cowl Web3 Python growth, blockchain infrastructure firms, blockchain infrastructure as a service, and way more. Plus, the weblog additionally covers newly launched blockchain networks. With newly launched blockchain networks, many are questioning if one other chain will push Ethereum off the leaderboard amongst programmable blockchains. If you’re a type of, try our articles exploring the Palm community and the next-gen L2 blockchain for Ethereum – Optimism. You may as well enroll in Moralis Academy to grow to be blockchain licensed. You can begin with blockchain fundamentals on the academy or deal with extra superior programs, comparable to Ethereum dapp programming.