简体   繁体   English

如何使用 Twilio 在 Python 中发送短信并等待回复?

[英]How to Send a Sms and wait for reply in Python using Twilio?

I am trying to create a vaccine appointment scheduler in Python.我正在尝试在 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:我目前正在从具有时隙和电话号码的 Excel 表中读取数据,我将文本发送为:

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.现在,我想要一个功能,我可以要求用户发送 1 确认,发送 2 取消。 How do I achieve that?我该如何做到这一点? Any documentation or code snippets would be helpful.任何文档或代码片段都会有所帮助。

Check out the Twilio docs on How to Receive and Reply to SMS and MMS Messages in Python .在 Python 中查看有关如何接收和回复 SMS 和 MMS 消息的 Twilio 文档

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.发送 SMS(您的代码)并将您发送的对象及其约会详细信息存储在某种数据库/缓存中。
  • 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.为此,您需要将 Twilio 中from号码的 webhook 连接到新端点以接收您需要创建的 SMS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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