首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Twilio在Python中发送短信并等待回复?

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

Stack Overflow用户
提问于 2021-03-18 03:14:59
回答 2查看 138关注 0票数 1

我正在尝试用Python创建疫苗预约计划程序。我目前正在从Excel表格中读取数据,该表格包含时间段和电话号码,并将文本发送给他们:

代码语言:javascript
运行
复制
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取消。我该如何做到这一点?任何文档或代码片段都会很有帮助。

EN

回答 2

Stack Overflow用户

发布于 2021-03-18 20:19:50

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

基本流程是:

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

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

票数 1
EN

Stack Overflow用户

发布于 2021-08-14 18:46:41

我也有同样的问题,现在我想出了如何完全从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()。下面是一个示例:

代码语言:javascript
运行
复制
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(),以根据发送者编号、接收者编号和发送日期进行过滤:

代码语言:javascript
运行
复制
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。)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66679782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档