Setting Up your Twilio Account
- Go to Twilio and Sign in after which you will find fue important Informations.
- After Signing in select “Get your number” to get your new Twilio number and also copy your “Account SID” and “Auth Token“and then we will move on to the coding.
Python code for sending messages
Step 1: First we need to install the necessary packages.
Python3
pip install twilio pip install google - cloud - pubsub |
Step 2: Next we need to write the following code. Remember that JSON file that we just downloaded!, now copy and locate your JSON file and then type in the following code along with your other credentials
Python3
from twilio.rest import Client from google.cloud import pubsub_v1 import os # Your JSON file location os.environ[ 'GOOGLE_APPLICATION_CREDENTIALS' ] = '/path/to/your/credentials/filename.json' # Twilio account credentials account_sid = 'YOUR TWILIO ACCOUNT SID' auth_token = 'YOUR TWILIO AUTH TOKEN' # Twilio phone number and target phone number twilio_number = 'YOUR TWILIO NUMBER' # Give full phone number without spacking and also specify your country code target_number = 'YOUR REGISTERED PHONE NUMBER WITH TWILIO' # Create a Twilio client client = Client(account_sid, auth_token) # Create a Pub/Sub publisher client publisher = pubsub_v1.PublisherClient() # Define the Pub/Sub topic name topic_name = 'projects/your-project-id/topics/your-topic-name' def send_sms(sender, recipient, message): # Send the SMS using Twilio client.messages.create( body = message, from_ = sender, to = recipient ) print ( "SMS sent successfully." ) def publish_message(message): # Publish a message to the Pub/Sub topic message_data = message.encode() future = publisher.publish(topic_name, data = message_data) print ( "Message published to Pub/Sub. Message ID:" , future.result()) if __name__ = = "__main__" : # Send the SMS using Twilio send_sms(twilio_number, target_number, 'Thank you for using w3wiki!' ) # Publish a message to the Pub/Sub topic publish_message( 'Thank you for using w3wiki!' ) |
Output:
This Python code will deliver a Message to your Target Phone number and then it will also Publish a message to the Pub/Sub topic, you can always change your message replace it with a link . Now If we look into the Topic in the Google Cloud services we can find this,
Python code which can send SMS which can be monitered using Google cloud and it can pull messages in Google Cloud By going in Pub/Sub, By Creating Subscription( Good to know that all the published message are stored in the topic until a subscriber subscribes to the topic and acknowledges receipt of the message.Or the message will die after 7 days).
Sending SMS from Python with Google Cloud Functions
In this article, we are going to talk about how we are going to send SMS (Short Message Services) with the addition of the Powers of Google Cloud Functions. Combining it with Twilio which is a cloud communications platform that provides APIs for sending and receiving SMS messages, as well as other communication channels like voice calls and video will help us to create a “serverless” solution for sending SMS messages.