如何在 Twilio 中格式化 ISO 8601 中的时间 API

How to format time in ISO 8601 in Twilio API

我正在尝试使用 Twilio 的消息服务来安排文本,但我不知道如何正确设置时间格式。我几乎已将 Twilio 网站上的说明复制到 T,但我一直收到无效语法错误。这是 send_at 变量的代码行:

send_at=datetime(2022-2-8'T'17:50:00'Z'),

如何正确格式化它才能运行?在此先感谢您的帮助。

这个时间戳是如何生成的?你是hard-coding吗?您看到的语法错误是 Python 无法理解括号之间的内容,它在语法上似乎不正确。

之后您可以简单地使用 isoformat() method from Python's datetime library 将常规日期时间对象转换为 ISO,示例:

>>> import datetime
>>> x = datetime.datetime(2022, 2, 8, 17, 50)
>>> x.isoformat()
'2022-02-08T17:50:00'

Twilio's own docs 直接建议此模式(请参阅“在 Python 中发送预定短信”部分):

message = client.messages.create(
from_=messaging_service_sid,
to='+1xxxxxxxxxx',  # ← your phone number here
body='Friendly reminder that you have an appointment with us next week.',
schedule_type='fixed',
send_at=send_when.isoformat() + 'Z',
)

看起来唯一的额外细节是在末尾附加 'Z',这是我的第一个代码片段与您的原始示例之间的唯一区别。如果您遵循了不同的文档,您可以分享 link,很乐意提供更具体的建议。