Setting Up your Twilio Account

  1. Go to Twilio and Sign in after which you will find fue important Informations.
  2. 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:

SMS you will receive in your phone

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,

Output in Google Cloud Service

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).

Visualizing the data

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.

Similar Reads

Setting up your Google Cloud Project

Step 1: Sign in with your Google Account in Google Cloud (Type Google Cloud and then search for Console, NOT the topmost link)Or you can skip the account creation as It requires Card information and tax details some of you might not have them...

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....

Conclusion

...

Contact Us