How to send timed messages using python & Twilio?

 

Twilio is a python library which helps you to send (text/multimedia) messages to your mobile.

First of all you have to register in this twilio API website https://www.twilio.com/try-twilio

Then you have to verify the receivers mobile number in your account.

After registering your twilio account they will give you

1.A TRIAL NUMBER.

2.An ACCOUNT SID.

3.An AUTH TOKEN

Also you have to install twilio in your machine using this command.

pip install twilio

# This is the simple code to send a message to mobile using twilio.

from twilio.rest import Client
# Your Account Sid and Auth Token from twilio.com/console
account_sid = 'AC911bd0d30df0c380d60627a53eb8f52d'
# DANGER! This is insecure. See http://twil.io/secure
auth_token = 'your_auth_token'
#Creates a client for twilio.
client = Client(account_sid, auth_token)
#This is the message part includes from and to addresses:
message = client.messages.create(
   #Actual message   body="Join Earth's mightiest heroes. Like Kevin Bacon.",
                     from_='+15017122661',  #Twilio number
                     to='+15558675310'   # recepants mobile number
                    )
print(message.sid)

To send a timed message you have to import datetime,time modules for current time and sleeping (required) time along with twilio.

from twilio.rest import Client
import time
import datetime

To return present time –>

present_time = time.time()

Also you have to enter the future time

ex: datetime.datetime(year,month,day,hours,min,sec)

Note: The time should always be in future to run your code error free.

#datetime.datetime(year,month,day,hours,min,sec)

time_sleep = datetime.datetime(2020, 7, 11, 17, 30, 20)

#change the time to future in this code

Allocated time to send your message is

# returns the (timer) time in seconds
required_time = (time_sleep.timestamp() - present_time)

function to return the time in seconds to normal time.

def convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60

    return "%d:%02d:%02d" % (hour, minutes, seconds)
required_time = (time_sleep.timestamp() - present_time)

print(convert(required_time))

This is the code used to sleep your program until the timer (required_time) arrives.

time.sleep(time_sleep.timestamp() - present_time)

The actual code format is:

# import packages

from twilio.rest import Client
import time
import datetime

present_time = time.time()
time_sleep = datetime.datetime(2020, 7, 11, 17, 30, 20)

required_time = (time_sleep.timestamp() - present_time)

def convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60

    return "%d:%02d:%02d" % (hour, minutes, seconds)



print(convert(required_time))

# code for sleep
time.sleep(time_sleep.timestamp() - present_time)


# Your Account Sid and Auth Token

account_sid = 'AC911bd0d30df0c380d60627a53eb8f52d'

# DANGER! This is insecure.
auth_token = 'your_auth_token'

#Creates a client for twilio.

client = Client(account_sid, auth_token)

#This is the message part includes from and to addresses:
message = client.messages.create(
    body="Hello cherry,what's going on.", #Actual message
    from_='+15017122661',  #your Twilio number it's dummy
    to='+15558675310'   # it's dummy recepants mobile number
                    )
print(message.sid)

Thank you ….

Create your own website, free on WordPress.com.Create your own website.
Start free