如何在给定日期后使用 python 在 Twilio API 中检索短信

How to retrieve SMS message after a given date with python in Twilio API

我正在尝试从给定日期之前的 twilio 消息列表中检索 SMS 消息。当我用等号询问给定日期时它起作用(它 returns 2019 年 2 月 2 日的所有短信:

 timestamp = datetime.datetime(2019, 2, 15, 0, 0,0)
 client = Client(account_sid, auth_token)

 messages = client.messages.list(
                           date_sent=timestamp
                       )

但如果我尝试使用:

 date_sent<=timestamp

 date_sent>=timestamp

我收到一个错误。

global name 'date_sent' is not defined

文档似乎建议您可以使用 >= 或 <= 运算符,但实际上这种方式行不通。任何想法如何让它吐出正确的数据?

这里是 Twilio 开发人员布道者!很好的问题,这没有很好的记录。我会和团队一起解决这个问题。

python 库在日期之前有一个不同的过滤器参数,所以你需要的是 date_sent_before 而不是 date_sent:

import os
import datetime

from twilio.rest import Client

account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN']

timestamp = datetime(2019, 2, 15, 0, 0,0)
client = Client(account_sid, auth_token)

# retrieve all messages before a given date
messages = client.messages.list(date_sent_before=timestamp)

print(len(messages1))
print(len(messages2))

如果您有任何其他问题,请告诉我:)