微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试用 Python 创建一个疫苗预约调度程序。我目前正在从 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)

现在,我想要一个功能,我可以要求用户发送 1 确认和 2 取消。我如何做到这一点?任何文档或代码片段都会有所帮助。

解决方法

查看 How to Receive and Reply to SMS and MMS Messages in Python 上的 Twilio 文档。

基本流程是:

  • 发送短信(您的代码)并将您发送给的人及其约会详细信息存储在某种数据库/缓存中。
  • 通过您的新端点接收短信,这会在数据库/缓存中查找并将它们标记为已确认或已取消。

为此,您需要将您在 Twilio 中的 from 号码的 webhook 连接到您需要创建的用于接收 SMS 的新端点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。