简体   繁体   中英

How to format time in ISO 8601 in Twilio API

I'm trying to use Twilio's messaging services to schedule a text, but I can't figure out how to format the time properly. I've pretty much copied the instructions to a T from Twilio's website, but I keep getting an invalid syntax error. Here's the line of code for the send_at variable:

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

How do I format this properly so it'll run? Thanks in advance for the help.

How is this timestamp being generated? Are you hard-coding it? The syntax error you're seeing is Python being unable to understand what's in between the parentheses, it doesn't seem syntactically correct.

You could simply use the isoformat() method from Python's datetime library instead to convert a regular datetime object to ISO afterwards, example:

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

Twilio's own docs directly suggest this pattern (see the "Send Scheduled SMS in Python" section):

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

Looks like the only additional detail is appending that 'Z' at the end, which is the only difference between my first snippet and your original example. If there are different docs that you followed that you can share a link to, happy to give more specific advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM