twilio / twilio-csharp

Twilio C#/.NET Helper Library for .NET6+.
MIT License
675 stars 302 forks source link

Incorrect timezone when sending SMS messages. #558

Closed DaveBagler closed 3 years ago

DaveBagler commented 3 years ago

Issue Summary

When I send an SMS message from my application the Created timestamp is in my local timezone (EST) however the documentation says it should be UTC. When I receive an SMS message through the webhook the Created timestamp is correctly in UTC. This creates a sorting issue in my database log of SMS messages.

Steps to Reproduce

  1. Send an SMS message using the API (Snippet below: SendMessage.)
  2. Log the message to the database.
  3. Check the Created timestamp in the database.
  4. Set up a webhook for receiving SMS messages for the number used to send the SMS message.
  5. Reply to the SMS message sent in step one.
  6. The reply will hit the webhook URL set up in step 4.
  7. When the webhook is hit use the information to fetch the SMS message (Snippet below: RetrieveMessageDetails.)
  8. Check the Created timestamp in the database for the reply. The Created timestamp for the message sent in step 1 is not UTC, the timestamp for the message sent in step 2.
Screen Shot 2021-02-15 at 9 01 27 PM

Code Snippet

        public async Task<SmsMessage> SendMessage(string from, string to, string body, string vendorId, string vendorToken)
        {
            CreateClient(vendorId, vendorToken);
            PhoneNumber toNumber = new PhoneNumber(to);
            PhoneNumber fromNumber = new PhoneNumber(from);

            CreateMessageOptions messageOptions = new CreateMessageOptions(toNumber) {
                From = fromNumber,
                Body = body,
            };

            MessageResource message = await MessageResource.CreateAsync(messageOptions, _client);

            return MapMessage(message);
        }

        private async Task<SmsMessage> RetrieveMessageDetails(string messageId, string vendorId, string vendorToken)
        {
            CreateClient(vendorId, vendorToken);

            FetchMessageOptions messageOptions = new FetchMessageOptions(messageId);
            MessageResource message = await MessageResource.FetchAsync(messageOptions, _client);

            return MapMessage(message);
        }

Exception/Log

N/A

Technical details:

DaveBagler commented 3 years ago

The issue is just on the initial send. I added a status callback webhook. If the status is sent, failed, delivered or undelivered, I've added a fetch, and I update the message in the database. The fetch provides the timestamps in the correct timezone.

shwetha-manvinkurke commented 3 years ago

Hi @DaveBagler the timestamps on the messages are set at the API layer in UTC when you make a call and this is not a helper library issue. That said, have you set a timezone in your Twilio project? I would also recommend reaching out to support if you need more help.

DaveBagler commented 3 years ago

Thanks, I'll reach out to Twilio for support. It does seem like it's not setting UTC on the initial call but then updating it to UTC on fetch.