【问题标题】:How to Send a Sms and wait for reply in Python using Twilio?如何使用 Twilio 在 Python 中发送短信并等待回复?
【发布时间】:2021-03-17 19:14:59
【问题描述】:

我正在尝试在 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 取消。我该如何做到这一点?任何文档或代码 sn-ps 都会有所帮助。

【问题讨论】:

    标签: python twilio twilio-api twilio-twiml twilio-programmable-chat


    【解决方案1】:

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

    基本流程是:

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

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

    【讨论】:

      【解决方案2】:

      我也有同样的问题,我现在完全从 Twilio REST Python 库中弄清楚了如何做到这一点。

      TLDR:如果您想使用 REST 帮助程序库,请使用 client.messages.list(from_='phone number here')


      The documentation has examples using Flask and TwiML,这似乎是执行此操作的“正确”方式,但这里是使用 Python 库执行此操作的方法。

      图书馆文档在这里:https://www.twilio.com/docs/libraries/reference/twilio-python/index.html

      如何查看所有发送和接收的消息

      如果您只想查看所有发送和接收的消息,您可以使用twilio.rest.Client.messages.list()。这是一个例子:

      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)
      

      这里,client.messages.list() 返回twilio.rest.api.v2010.account.message.MessageInstance 类型的列表。

      这里,message.to 是返回电话号码的字符串,message.body 是消息的内容。也可以使用message.direction查看是入站消息还是出站消息,

      您也可以使用client.messages.stream() 获取生成器,其工作方式类似。

      如何查看从特定号码收到的所有消息

      根据this page in the documentation,我们实际上可以将参数传递给list(),以按发送者编号、接收者编号和发送日期进行过滤:

      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)
      

      (注意这是参数名称from_,而不是python关键字from。)

      【讨论】:

        猜你喜欢
        • 2021-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多