Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlackTaskClient.DeleteMessageAsync takes a datetime but should take a timestamp string #299

Open
mdiluz opened this issue Feb 21, 2022 · 1 comment

Comments

@mdiluz
Copy link

mdiluz commented Feb 21, 2022

DeleteMessageAsync for the task client seems to be written to match the one in the socket client, which understands timestamps as date times, but the API requires the timestamp as a string including microseconds.

Demonstrated in code below:

var response = await SlackTaskClient.PostMessageAsync(recipient, item.Message);
if (!response.ok)
    throw new Exception(response.error);

// response.ts e.g. 1645450156.370359
var baseTimestamp = response.ts.Replace(".",""); // Remove the bullet point
var ts = long.Parse(baseTimestamp) / 1000; // Problem: We have to lose the milliseconds here
var time = DateTimeOffset.FromUnixTimeMilliseconds(ts).DateTime; // Convert to a DateTime

var deletedResponse = await SlackTaskClient.DeleteMessageAsync(response.channel, time); 
if (!deletedResponse.ok)
    throw new Exception(deletedResponse.error); // We fail with message_not_found

Slack needs the full ts string with the unique digits after the ., not a real timestamp, so DeleteMessageAsync can't work here as I don't think you can stick the microseconds back into a DateTime (and I don't think ToProperTimeStamp can convert and add the .).

The workaround for now is simply to make the call manually:

var deletedResponse = await SlackTaskClient.APIRequestWithTokenAsync<DeletedResponse>(new List<Tuple<string, string>>()
{
    new("ts", response.ts),
    new("channel", response.channel)
}.ToArray());

if (!deletedResponse.ok)
    throw new Exception(deletedResponse.error);
@ceciliachan1979
Copy link

@mdiluz This will preserve the milliseconds.

            decimal doubleTimestamp = Decimal.Parse(response.ts);
            long longTimestamp = (long)(doubleTimestamp * 10000000m);
            DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            return unixEpoch.AddTicks(longTimestamp);

There are other APIs (such as GetConversationsHistoryAsync) that return timestamps as DateTime, maybe we can consider changing PostMessageAsync to return DateTime instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants