简体   繁体   English

在 Twilio 和 Sendgrid api 中,计划消息以 5 小时 30 分钟的时间差发送

[英]Scheduled messages are sent at difference of 5 hours 30 minutes in Twilio and Sendgrid api

I am working on a Django application where I have used twilio to send sms and whatsapp messages and the sendgrid api for sending the emails.我正在开发一个 Django 应用程序,我使用 twilio 发送短信和 whatsapp 消息,使用 sendgrid api 发送电子邮件。 The problem occurs in scheduled messages in all three.该问题出现在所有三个中的预定消息中。 For example, if I have schedule an email to be sent at 06:24 PM(scheduled time is 06:24 PM), then I am receiving the email at 11:54 PM, hence the difference is of 5 hours and 30 minutes.例如,如果我安排在下午 06:24 发送 email(预定时间是下午 6:24),那么我在晚上 11:54 收到 email,因此时差为 5 小时 30 分钟。 The same problem also arises in sms and whatsapp.同样的问题也出现在短信和whatsapp中。

I think the problem might be due to the timezone.我认为问题可能是由于时区。 I am scheduling the messages from India and twilio account is in USA.我正在安排来自印度的消息,并且 twilio 帐户在美国。 And, the time difference of 5 hours 30 mins is due to the UTC format as India is 5 hours 30 mins ahead of UTC.而且,5 小时 30 分钟的时差是由于 UTC 格式,因为印度比 UTC 早 5 小时 30 分钟。 But I am not getting any solution for this.但我没有得到任何解决方案。

Also the time is specified as the UNIX time.时间也被指定为 UNIX 时间。 I am getting a datetime object whose value is like: "2022-09-13 11:44:00".我得到一个日期时间 object,其值类似于:“2022-09-13 11:44:00”。 I am converting it to unix time with the "time module" of python.我正在使用 python 的“时间模块”将其转换为 unix 时间。 Hence the code looks like:因此代码如下所示:

message = MAIL(...)
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime

In the above code, scheduleTime is the datetime object.在上面的代码中,scheduleTime 是日期时间 object。

So, is there any specific way to solve this problem?那么,有没有什么具体的方法可以解决这个问题呢?

As per the answer of philnash , the sendgrid api and twilio is scheduling the messages in UTC and we are generating it in IST without timezone attached.根据 philnash 的回答, sendgrid api 和 twilio 正在以 UTC 调度消息,我们在没有附加时区的 IST 中生成它。 Hence first of all we need to attach the timezone and then convert it in the UNIX Timestamp.因此,首先我们需要附加时区,然后将其转换为 UNIX 时间戳。 Refer to the following code:参考以下代码:

scheduleTime = scheduleTime.astimezone()
finalScheduleTime = int(time.mktime(scheduleTime.timetupple()))
message.send_at = finalScheduleTime 

Hence, this will solve the problem.因此,这将解决问题。

The issue is that Twilio and SendGrid are scheduling the messages in UTC and you are creating your time in IST without a time zone attached, so the time is being interpreted as UTC and thus sent 5 hours 30 minutes out.问题是 Twilio 和 SendGrid 正在安排 UTC 中的消息,并且您在没有附加时区的 IST 中创建时间,因此时间被解释为 UTC,因此发送了 5 小时 30 分钟。

I'm not a python expert, so I may be wrong here.我不是 python 专家,所以我在这里可能错了。 But, I think using mktime is incorrect here and you should be using gmtime .但是,我认为在这里使用mktime是不正确的,您应该使用gmtime

message = MAIL(...)
finalScheduleTime = int(time.gmtime(scheduleTime.timetuple()))
message.send_at = finalScheduleTime

Alternatively, you don't have to convert the datetime to a time first.或者,您不必先将日期时间转换为时间。 You can change the datetime's timezone from your local one to UTC:您可以将日期时间的时区从本地时间更改为 UTC:

import pytz

utcdatetime = scheduleTime.astimezone(pytz.UTC)

Then turn that into a timestamp:然后将其转换为时间戳:

message.send_at = int(utcdatetime.timestamp())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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