Communication in Python

Twilio is a very commonly used service for messages and calls. Here, as of now, we will understand implementation with Twilio and SMTP (to send emails using Gmail)

What is Twilio?

Twilio is an API that software developers can use to add communications such as

  • Mobile messaging

  • Two-factor authentication into their Python applications

  • Check the validity of a mobile number

  • Whatsapp messaging

  • Phone calling

  • Video

Communications using Twilio, How to do it?

First, you need to create an account on Twilio. The procedure is clearly given in Twilio documentation. For more details check the implementation. The simple procedure includes:

  • Create an account

  • Open the console after logging in to the account

  • Note "Account SID" and "Auth Token", this is required for using Twilio with python

Going step by step through the 4 sample use cases should provide you the overall view of Twilio usage.

  • Free up to 200 messages per user are allowed using Twilio.

  • Twilio gives a 15.5 dollar sign in balance, which can be used to buy a Twilio number (which is required for sending SMS or for calls)

  • Hence, we get sufficient balance as a free user for practice and learning purposes on Twilio

  • The trial account can not send messages to unverified accounts, verify number before sending SMS

Communication using SMTP: Send Email

The procedure to send an email using python is very well explained in this article. However, if you want to directly use the code without going deep into it, use the below sample code (you will have to pip install smtplib)

import smtplib

gmail_user = "<gmail login id>"
gmail_password = "<gmail password>"

sent_from = gmail_user
to = ["<test email id 1>", '<test email id 2>']
subject = 'OMG Super Important Message'
body = "Hey, what's up?\n\n- You"

email_text = """\
From: %s
To: %s
Subject: %s

%s
""" % (sent_from, ", ".join(to), subject, body)

try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    server.sendmail(sent_from, to, email_text)
    server.close()

    print('Email sent!')
except:
    print('Something went wrong...')

Implementation

1. Messaging for a daily update at a particular time every day (share price updates)

The message includes both mobile phone SMS and email. Functionalities creating in the app

  • Accumulate price, volume, and change of each share from the "money control" website

  • Send SMS using Twilio, and email using SMTP (email should be properly formatted in tabular form)

  • Send WhatsApp SMS as well using Twilio

  • Use airflow to schedule the SMS every day at a particular time (11:00 am and 5:00 pm)

Github repo to be updated....

2. Messaging for a Flask app error alerts

For sample code, go through the git repository: "to be updated" and branch "to be updated"

3. Two-factor authentication for the login

to be updated....

4. Check mobile number validity

to be updated....

Use Cases

Twilio provides a lot of functionalities, which we have not covered here; but are well documented along with good descriptive use cases and step by step implementation guide. Go through the different links listed in following link:

Last updated

Was this helpful?