Reputation: 3691
I have a webhook that posts a message to a Slack channel. Is there a way to ensure that times mentioned in the message are shown in the user's local timezone?
Upvotes: 10
Views: 19137
Reputation: 812
Slack's API allows you to output time in the user's timezone using a special placeholder. See Formatting Dates in the API documentation.
Upvotes: 13
Reputation: 22884
As described in https://api.slack.com/reference/surfaces/formatting#date-formatting you can use special date notation <!date^timestamp^token_string^optional_link|fallback_text>
to have time displayed using viewers local TZ.
A snippet for GO that I use to format time for slack :
func SlackFormat(t time.Time) string {
return `<!date^` + strconv.FormatInt(t.Unix(), 10) + `^{date_num} {time}|` + t.Format(time.RFC3339) + `>`
}
Upvotes: 1
Reputation: 2156
As per the slack API documentation, the chat.postMessage
method returns a JSON with a response like this one here
{
"ok": true,
"ts": "1405895017.000506",
"channel": "C024BE91L",
"message": {
...
}
}
So, i guess you can parse and get the timestamp and format it according to your need . Here is the link to slack api postMessage API documentation. Just to mention, ts
is timestamp in above JSON response.
Upvotes: 1
Reputation: 3691
Looks like it is impossible to display dates in the user's timezone in a shared channel. Even though the Slack clients for Linux and Android both seem to be using an embedded browser as a platform for their UI, there is no way that we can run JS on the client, which means that we can't do anything dynamic, like looking up the local TZ.
If the message were a direct message to a single user, then codehat's answer would work, but if we don't want timezones to differ between channel messages and direct messages.
Upvotes: 0