1

I am trying to create a vaccine appointment scheduler in Python. I am reading data currently from an Excel Sheet which has the time slots and the phone numbers and I am sending them the text as:

import csv              
from twilio.rest import Client
account_sid = ''
auth_token = ''
client = Client(account_sid, auth_token)
name='Test'
f = open(name+'.csv')
csv_f = csv.reader(f)   
data = []

for row in csv_f: 
    data.append(row)

f.close()
for row in data:
    if (data):
        firstname = row[0]
        
        phone_number = row[3]
        print(phone_number)
        time = row[5]
        confirmation = row[6]
        print(confirmation)
        nl = '\n'
        message = client.messages \
                        .create(
                            body=f"{firstname},\n\nCOVID Vaccine appointment confirmation on March 17th, {time} at .\n\nYour confirmation number is {confirmation}. Please show this message at your arrival.\n\nWe have LIMITED parking, please show up on time.\n\nYou MUST register using the form below before your appointment.\n\n\n ",
                            from_='+1',
                            to=f'+1{phone_number}'
                        )

    print(message.sid)
    #print (firstname,lastname,phone_number,time)

Now, I want to have a feature where I can ask the user to send 1 to confirm and 2 to cancel. How do I achieve that? Any documentation or code snippets would be helpful.

John
  • 23
  • 4

2 Answers2

1

Check out the Twilio docs on How to Receive and Reply to SMS and MMS Messages in Python.

The basic flow being:

  • Send out the SMS (your code) and store to whom you've send it and their appointment details in some kind of database/cache.
  • Receive SMS via your new endpoint, this does a look up in the database/cache and marks them confirmed or cancelled.

For this to work you would need to connect the webhook of your from number in Twilio to the new endpoint for receiving SMS which you need to create.

yvesonline
  • 4,609
  • 2
  • 21
  • 32
0

I had the same question, and I just now figured out how to do this entirely from the Twilio REST Python library.

TLDR: If you want to use the REST helper library, use client.messages.list(from_='phone number here')


The documentation has examples using Flask and TwiML, which seems like the "right" way to do this, but here is how to do it with the Python library.

Library documentation is here: https://www.twilio.com/docs/libraries/reference/twilio-python/index.html

How to see all sent and received messages

If you just want to see all messages, sent and received, you can use twilio.rest.Client.messages.list(). Here is an example:

from twilio.rest import Client
account_sid = ''
auth_token  = ''
service_SID = ''
client = Client(account_sid, auth_token)

# See all messages, both sent and received
for message in client.messages.list():
    print(message.to, message.body)

Here, client.messages.list() returns a list of type twilio.rest.api.v2010.account.message.MessageInstance.

Here, message.to is a string that returns the phone number, and message.body is the content of the message. You can also use message.direction to see if it's an inbound or outbound message,

You can also use client.messages.stream() to get a generator, which works in a similar manner.

How to see all messages received from a specific number

Per this page in the documentation, we can actually pass arguments to list() to filter by sender number, receiver number, and date sent:

from twilio.rest import Client
account_sid = ''
auth_token  = ''
service_SID = ''
client = Client(account_sid, auth_token)

# See all messages, both sent and received
for message in client.messages.list(from_=''):
    print(message.body)

(Note this is a parameter name from_ and not the python keyword from.)

lynn
  • 536
  • 2
  • 5
  • 16