我正在尝试用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取消。我该如何做到这一点?任何文档或代码片段都会很有帮助。
发布于 2021-03-18 20:19:50
在How to Receive and Reply to SMS and MMS Messages in Python上查看Twilio文档。
基本流程是:
为此,您需要将Twilio中的from
号码的webhook连接到新的端点,以接收您需要创建的短信。
发布于 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()
。下面是一个示例:
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
。)
https://stackoverflow.com/questions/66679782
复制相似问题