How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
Key Takeaways
This video demonstrates how to send SMS text messages using Python and the Twilio API, covering setup, implementation, and environment variable configuration. It utilizes the Twilio Python helper library and pip for installation.
Full Transcript
hey everyone welcome to this tutorial where i'm going to be sharing with you how you can send a text or an sms message to your phone from a python script using the twilio api the ability to send text or sms messages to your phone can be a really useful feature to have in your app for instance you can use it to verify customer identities or to alert you when something important has happened and we're going to be doing this by making an api request to a service called twilio which also conveniently provides us with a python library now twilio does normally charge per text message that you send something like a couple of cents per message depending on the country that you're in but it does come with quite a generous free trial that we're going to use for this tutorial so with that out of the way let's get started with the code before we get started writing any python code we're first going to need to have a twilio account so head on over to twilio.com and then just click on this sign up and start building button here that should then take you to a sign up page where you have to fill in a couple of details and then verify your email once your account is verified and you sign into your twilio dashboard you should see something like this so in your console there'll be five steps you can take to use twilio or to sort of on board with it and the very first step is to get a twilio phone number i've actually already gone ahead and done this step so i can't do it again apparently but there should be a button here that you can click and it will just let you acquire a phone number for free and once you have that number that's going to be the number that appears when you use this to send messages to yourself or to other people in the next step it gives us a bash command or a terminal command to actually send a text message to our phone so we're gonna try that you need to select what type of terminal you want to use so you can use windows powershell or just windows curl if you are using windows or if you're using ubuntu linux or mac os then you will need to use this unix option so i'm actually on ubuntu right now so this is what i'm going to use and this is the command we're going to need to put into our terminal and this will test that the twilio api works and can send us a text message so this auth token would be needed to be replaced with your actual off token which you can find by scrolling down here and then clicking reveal to show this off token it's hidden by default but if we go back up here and uh just click show off token then it's going to reveal it and we can just copy that normally we shouldn't reveal it but i'm just testing it and i will delete this token after so i'm comfortable with doing this for now so let's go ahead and do that and once we have that just take it to your terminal of choice and then paste the entire message and then you should be able to just hit enter and then you should get a response like this and if you've input your uh to destination as your actual phone number with the international dialing code then you should see a text message appear on your phone [Music] so now that we've shown how this can work with the curl api in the terminal we want to actually put this into python so that we can use it as part part of a script so what does that look like so if we go to the next step there's an app demo that has a no code demo but we're not interested in this so we're just going to skip this step and go to step 4 which is a tutorial and this is how we can learn line by line how to send and receive messages so if i click that that should take me to a documentation page and here we can choose what language we want to use it in so we're going to use python we're going to click the python library and here is going to be our python specific instructions for using this so the first step is we have to use twilio's python helper library and if we open that then it's just a matter of pip installing twilio pip install twilio so for here it's not going to do anything it's already satisfied but if you run that and you don't have it you should see it download and install twilio so going back to this it pretty much just gives us the entire code we can use um as a sample i'm just going to go ahead and copy that here we go to our python ide and i'll just create a new file and i'll call it semtex dot py and then i'll paste the code i copied from there so if we look at it um we import os we import twilio and then we set the account ids and the off token uh so here we're actually getting this from the environment variables so we have to set that first otherwise otherwise this script will fail we create a twilio client and then we send a message pretty much just using this function here so this is the text of the message and this is the source phone number that we want to use and the destination now if you do want to use this functionality as part of an app it probably makes sense to wrap this whole thing in a function that you can specify the destination phone number and the message so i'm just going to go ahead and do that first i'm going to create a function here called send text message and it's going to have an argument called destination which is going to be the phone number i want to send it to and it's also going to have a message so this function will wrap this entire thing and i'm just going to indent that so it's under this scope and now i need to replace this here in this api call okay so now i have a function that wraps their example i might also want to actually use this function so let me create a main function which will send the text message and i'm going to put a number here and then i'm gonna put a message okay so now i'm using this function that wraps this example code that we have uh and we should be able to use this and then i'll just have to make sure that i call the main function so i could just go ahead and do it like this but if i want to do this properly then i need to do the if name equals main thing as well like this so this will only be called if we run the script directly which we're actually going to do so when i run the script it will enter this condition which will run this function which will send our message using our wrapper here and this will use the example code we saw from twilio earlier the reason i did it this way rather than running the example right away is because most likely when you build a more complex app you're probably going to want to wrap this stuff in some kind of functionality so i just want to show you how it would look if you were to do that now if i go ahead and run this i should expect it to fail because these things should be missing so let's try that and confirm that it actually does fail okay so as expected we haven't assigned this environment variable twilio account id so it's giving us this error so we can either do that by uh getting our account id and our auth token and replacing it like hard coding it here but that's bad because then we have our configuration in code so let's actually just go ahead and add it to our environment variable first if we go back to the tutorial page and scroll down there's actually a section here that says it's okay to hard code your credentials when getting started but you should use environment variables to keep them secret before deploying to production so here's a guide on how to set environment variables so let's go and have a look at that and there's a whole uh and there's a whole article here so you could follow this if you want and i'm using linux so i'll be doing one of these things but if you're using windows then just follow one of these instructions above so just click on that and it'll take you to this page for here i just have to export the environment variable and then i just have to replace it with this value first we have to identify what these values are going to be so to do that we go back to our console and we can go to account and click api keys and token but if you're already on this console page you can also find it in this account info here as well so your account id is just this one here so let's get this into our environment variable first so the first one we have to set is this twilio account sid that's the key so to do that we'll go to our terminal write export twilio account sid and then just type in whatever value you had in your console and copy and paste it so i'll set that one and then we'll do the same for our off token which i've already copied to my clipboard so i'll just run it here as well and by the way the key for that one is twilio off token so let's just go ahead and do that okay and now my environment variables should be set so if i echo these values now in my terminal i should actually see those numbers appear so now that's done i can run the script again and hopefully this time it will work okay so i actually have a bug here because in my app i didn't change the default phone number from the one that was provided in the example so the one that was provided in the example this is not the phone number that i created when i use the console so i have to go back to this step one where i got the phone number and then go down here where it says my twilio phone number and copy that and then replace it in my script so let's go ahead and do that so my from number is going to be replaced and now let me go back to this terminal and send it again okay so it has a text message id being returned and if i wait for this message on my phone it's going to show up [Music] so that brings us to the end of the tutorial for how you can send text or sms messages to your phone from a python script this is definitely a worthwhile skill or tool to have under your belt because it's easy to implement as you've seen but also at the same time still very useful and if you have any ideas for how to use a feature like this in your project or if you have any questions about the api then please feel free to share that in the comments and i'll try to answer everything i can anyways that's it for today i hope you find it useful and thank you for watching
Original Description
In this video, I will show you how to use the Twilio API in Python to send simple SMS text messages to your phone.
👉 https://twilio.com/
👉 Photo by @jonasleupe on Unsplash
00:00 Introduction
00:46 Setting up your Twilio account
04:01 Implement the Python script
07:10 Setting the API key environment variables
10:17 Wrapping up
#python #twillio
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from pixegami · pixegami · 11 of 60
1
2
3
4
5
6
7
8
9
10
▶
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How to Build an AWS Lambda Function in Python in Just 7 Minutes!
pixegami
AWS CDK Tutorial: Deploy a Python Lambda Function using AWS
pixegami
I used GPT-3 to Write Poetry • Is AI the Future of Creative Writing?
pixegami
Create NFT Generative Art with Python! (Full Tutorial)
pixegami
Build an AI-driven SaaS Application: FULLSTACK Tutorial with Python, React, and AWS
pixegami
NextJS and TailwindCSS: How to Build a Portfolio Site from Scratch
pixegami
Python Web Scraping Tutorial • Step by Step Beginner's Guide
pixegami
Build Wordle in Python • Word Game Python Project for Beginners
pixegami
How to create 1000+ unique NFT-style images (like Cryptopunk) | Python Tutorial
pixegami
Top 10 Python Modules 2022
pixegami
How to Send SMS Text Messages with Python & Twilio - Quick and Simple!
pixegami
How To Write Unit Tests in Python • Pytest Tutorial
pixegami
How to Style Your React Landing Page with Tailwind CSS
pixegami
FastAPI Python Tutorial - Learn How to Build a REST API
pixegami
How to Deploy FastAPI on AWS EC2: Quick and Easy Steps!
pixegami
PyScript • How to run Python in a browser
pixegami
My Custom Ubuntu Linux Terminal with Themes and Plug-ins 💻
pixegami
Deploy FastAPI on AWS Lambda ⚡ Serverless hosting!
pixegami
NextJS Firebase Auth Tutorial • How to Authenticate Users for Your App
pixegami
AWS Lambda Python functions with a database (DynamoDB)
pixegami
How To Build a CRUD (TO-DO) App on AWS using FastAPI and Python
pixegami
How to Make a Discord Bot with Python
pixegami
How To Use GitHub Copilot (with Python Examples)
pixegami
PyTest • REST API Integration Testing with Python
pixegami
Python Beginner Project: Build a Caesar Cipher Encryption App
pixegami
Decorators in Python: How to Write Your Own Custom Decorators
pixegami
NextJS 13 Tutorial: Create a Static Blog from Markdown Files
pixegami
Exploring ChatGPT for Coding and Business ✨ 8 Real Examples!
pixegami
How I Would Learn Python (if I had to start over) • A Roadmap for 2023
pixegami
Build an AI Pokemon Generator with Python and Midjourney
pixegami
Why You Should Learn Python in 2023 (as your first programming language)
pixegami
ChatGPI API in Python ✨ How to Build a Custom AI Chat App
pixegami
Learn Python • #1 Installation and Setup • Get Started With Python!
pixegami
Learn Python • #2 Variables and Data Types • Python's Building Blocks
pixegami
Learn Python • #3 Operators • Add, Subtract and More...
pixegami
Learn Python • #4 Conditions • If / Else Statements
pixegami
Learn Python • #5 Lists • Storing Collections of Data
pixegami
Learn Python • #6 Loops • How to Repeat Code Execution
pixegami
Learn Python • #7 Dictionaries • The Most Useful Data Structure?
pixegami
Learn Python • #8 Tuples and Sets • More Ways To Store Data!
pixegami
Learn Python • #9 Functions • Python's Most Important Concept?
pixegami
Learn Python • #10 User Input • 4 Ways To Get Input From Your User
pixegami
Learn Python • #11 Classes • Create and Use Classes in Python
pixegami
Learn Python • #12 Final Project • Build an Expense Tracking App!
pixegami
Stripe & Firebase Tutorial • Add Payments To Your NextJS App
pixegami
How To Use GitHub Actions • Automate Your AWS Deployments
pixegami
How to Run a Python Docker Image on AWS Lambda
pixegami
My MacOS Terminal Setup for HIGH Productivity
pixegami
Host a Python Discord Bot on AWS Lambda (Free and Easy)
pixegami
Python FastAPI Tutorial: Build a REST API in 15 Minutes
pixegami
Pydantic Tutorial • Solving Python's Biggest Problem
pixegami
How to Get Started with AWS • Crash Course
pixegami
Python Requests Tutorial: HTTP Requests and Web Scraping
pixegami
Amazon Bedrock Tutorial: Generative AI on AWS
pixegami
How to Publish a Python Package to PyPI (pip)
pixegami
Langchain: The BEST Library For Building AI Apps In Python?
pixegami
RAG + Langchain Python Project: Easy AI/Chat For Your Docs
pixegami
Python Dataclasses: Here's 7 Ways It Will Improve Your Code
pixegami
Build a Custom AI RPG Game with OpenAI GPTs
pixegami
Create a Custom AI Assistant + API in 10 Mins
pixegami
More on: AI Pair Programming
View skill →Related AI Lessons
Chapters (5)
Introduction
0:46
Setting up your Twilio account
4:01
Implement the Python script
7:10
Setting the API key environment variables
10:17
Wrapping up
🎓
Tutor Explanation
DeepCamp AI