【问题标题】:How to retrieve SMS message after a given date with python in Twilio API如何在 Twilio API 中使用 python 在给定日期后检索 SMS 消息
【发布时间】:2019-02-15 08:49:16
【问题描述】:

我正在尝试在给定日期之前从 twilio 消息列表中检索 SMS 消息。当我用等号询问给定日期时它会起作用(它会在 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

文档似乎建议您可以使用 >= 或

【问题讨论】:

    标签: python twilio twilio-api


    【解决方案1】:

    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))
    

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

    【讨论】:

    • 这有帮助!谢谢!而 date_sent_after 则相反!
    猜你喜欢
    • 1970-01-01
    • 2022-07-06
    • 2018-12-27
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2020-08-25
    相关资源
    最近更新 更多