简体   繁体   中英

How with twilio to send sms at defined time?

I just started using twilio for a start-up. We need to send to our customers sms 15 min before appointments. The appointments can be 1-5 per day. I have the times and dates of the appointments, with the name of the corresponding customer and more info, saved in a csv-file.

How can I make twilio send the sms at the desired time from the csv?

Note that I am coding exclusively in python (my level is basic to mid). I have found this example:

from twilio.rest import Client

customer_num = '+15558675310'
account_sid = 'AC56382b8b1ac86598d9a775851c9652dc'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages \
                .create(
                     body="Join Earth's mightiest heroes. Like Kevin Bacon.",
                     from_='+15017122661',
                     to=customer_num)
                 )

print(message.sid)

It would be great if I could include something like a function written by me and if there would be a parameter of client.message.create for when to send the message, something like:

def send_time_func(phone_num, other_parameters):
        '''Some function I write on my own that goes through the csv
        and sends the sms to the customer at the defined time in the csv'''
        return(send_time)

message = client.messages.create(
                         ....
                         send_time=send_time_func(customer_num, other_parameters))

Is there a simple solution for that with python? If not, is there an alternative to twilio that does this? What other suggestions can you give? Tnx

Twilio currently doesn't have a job scheduler, so you need to use an external scheduler to perform some of these tasks, possibly the below will be helpful.

4 ways to schedule Node.js code

  • Yes Node but I imagine you can derive the steps for Python

How to use email and SMS notifications together

Another simple way would be to create a google apps script. You can utilize the clock triggers to run a function to fetch a URL to call a Twilio API to send a sms here is a basic function to send a twilio sms in a google apps script.

function sendSms(){  
var messagesUrl = "https://api.twilio.com/2010-04-01/Accounts/YOUR_TWILIO_ACCOUNT_SID/Messages.json";
    
  var payload = {
    "To": "DESTINATION_NUMBER",
    "From" : "YOUR_TWILIO_NUMBER",
    "Body" : 'This is a reminder that you have an appointment',
  };
  
  var options = {
    "method" : "post",
    "payload" : payload
  };
  
  options.headers = {    
    "Authorization" : "Basic " + Utilities.base64Encode('YOUR_ACCOUNT_SID:YOUR_AUTH_TOKEN')
  };
  UrlFetchApp.fetch(messagesUrl, options);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM