Skip to content

Commit

Permalink
better attachment support (#135)
Browse files Browse the repository at this point in the history
Having read a lot of Slack documentation and operated v2.0.0.3 in
production for a bit, I now understand that the Slack message attachment
field is poorly standardized. None of the fields are required and there
are different versions of the attachments api over the years that are
all supported in this one field.

To make apps that look at attachments, chance are, you're going to need
to look at the raw json value. So I've edited slack-web to try to decode
the attachment based on documented schema (and ported from Slack's own
open source javascript sdks) but then also pass the raw json value to
clients.

Before submitting your PR, check that you've:

- [x] Documented new APIs with [Haddock
markup](https://www.haskell.org/haddock/doc/html/index.html)
- [x] Added [`@since`
declarations](http://haskell-haddock.readthedocs.io/en/latest/markup.html#since)
to the Haddock

After submitting your PR:

- [x] Update the Changelog.md file with a link to your PR
- [x] Bumped the version number if there isn't an `(unreleased)` on the
Changelog
- [x] Check that CI passes (or if it fails, for reasons unrelated to
your change, like CI timeouts)
  • Loading branch information
ldub authored Nov 1, 2024
1 parent 4dc4e3c commit 649186a
Show file tree
Hide file tree
Showing 16 changed files with 1,619 additions and 163 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.0.0.4 (2024-09-15)
* [#135](https://github.com/MercuryTechnologies/slack-web/pull/135)
Improves attachement support by providing clients wih the raw JSON value
in case of a parse failure.

# 2.0.0.3 (2024-08-30)
* [#133](https://github.com/MercuryTechnologies/slack-web/pull/133)
Adds attachment support for message event subscriptions.
Expand Down
2 changes: 1 addition & 1 deletion slack-web.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: slack-web
version: 2.0.0.3
version: 2.0.0.4

build-type: Simple

Expand Down
59 changes: 57 additions & 2 deletions src/Web/Slack/Experimental/Events/Types.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

-- FIXME(jadel): Use NoFieldSelectors when we drop everything before 9.2.
Expand All @@ -8,6 +11,8 @@ module Web.Slack.Experimental.Events.Types where

import Data.Aeson
import Data.Aeson.TH
import Data.Aeson.Types (Parser)
import Data.Scientific qualified as Sci
import Data.Time.Clock.System (SystemTime)
import Web.Slack.AesonUtils
import Web.Slack.Experimental.Blocks (SlackBlock)
Expand Down Expand Up @@ -58,7 +63,7 @@ $(deriveFromJSON snakeCaseOptions ''AttachmentMessageBlock)
-- Ported from https://github.com/slackapi/node-slack-sdk/blob/fc87d51/packages/types/src/message-attachments.ts
--
-- @since 2.0.0.3
data MessageAttachment = MessageAttachment
data DecodedMessageAttachment = DecodedMessageAttachment
{ fallback :: Maybe Text
, color :: Maybe Text
, pretext :: Maybe Text
Expand Down Expand Up @@ -87,7 +92,57 @@ data MessageAttachment = MessageAttachment
}
deriving stock (Show)

$(deriveFromJSON snakeCaseOptions ''MessageAttachment)
instance FromJSON DecodedMessageAttachment where
parseJSON = withObject "DecodedMessageAttachment" $ \v -> do
fallback <- v .:? "fallback"
color <- v .:? "color"
pretext <- v .:? "pretext"
authorName <- v .:? "author_name"
authorLink <- v .:? "author_link"
authorIcon <- v .:? "author_icon"
title <- v .:? "title"
titleLink <- v .:? "title_link"
text <- v .:? "text"
fields <- v .:? "fields"
imageUrl <- v .:? "image_url"
thumbUrl <- v .:? "thumb_url"
footer <- v .:? "footer"
footerIcon <- v .:? "footer_icon"
ts <- v .:? "ts" >>= maybe (return Nothing) parseTs
isMsgUnfurl <- v .:? "is_msg_unfurl"
messageBlocks <- v .:? "message_blocks"
authorId <- v .:? "author_id"
channelId <- v .:? "channel_id"
channelTeam <- v .:? "channel_team"
isAppUnfurl <- v .:? "is_app_unfurl"
appUnfurlUrl <- v .:? "app_unfurl_url"
fromUrl <- v .:? "from_url"
pure DecodedMessageAttachment {..}
where
parseTs :: Value -> Parser (Maybe Text)
parseTs (String s) = pure $ Just s
parseTs (Number n) =
let s = Sci.formatScientific Sci.Fixed Nothing n
formatted = if '.' `elem` s then s else s ++ ".000000"
in pure $ Just (pack formatted)
parseTs _ = pure Nothing

data MessageAttachment = MessageAttachment
{ decoded :: Maybe DecodedMessageAttachment
-- ^ If the attachment can be decoded, this will be populated
, raw :: Value
-- ^ Slack does not document the attachment schema/spec very well and we can't
-- decode many attachments. In these cases clients can work with the raw JSON.
}
deriving stock (Show)

instance FromJSON MessageAttachment where
parseJSON = withObject "MessageAttachment" $ \v -> do
let ov = Object v
-- Attempt to parse the entire object as DecodedMessageAttachment
decodedContent <- optional $ parseJSON ov
-- Return the structured data with raw JSON preserved
pure MessageAttachment {decoded = decodedContent, raw = ov}

-- | <https://api.slack.com/events/message>
-- and
Expand Down
4 changes: 4 additions & 0 deletions tests/Web/Slack/Experimental/Events/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ spec = describe "Types for Slack events" do
"email_message"
, "message_subtype_bot_message"
, "forwarded_message"
, "github_notification"
, "github_notification_ts_string"
, "github_with_link"
, "non_spec_attachment"
]
254 changes: 201 additions & 53 deletions tests/golden/SlackWebhookEvent/forwarded_message.golden

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/golden/SlackWebhookEvent/forwarded_message.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"token": "d90zMGO2eDLrkLHJYcKevn2b",
"token": "aaaa",
"team_id": "T043DB835ML",
"context_team_id": "T043DB835ML",
"context_enterprise_id": null,
Expand Down
197 changes: 197 additions & 0 deletions tests/golden/SlackWebhookEvent/github_notification.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
EventEventCallback
( EventCallback
{ eventId = EventId
{ unEventId = "Ev07MH6R7FDE" }
, teamId = TeamId
{ unTeamId = "T043DB835ML" }
, eventTime = MkSystemTime
{ systemSeconds = 1725998548
, systemNanoseconds = 0
}
, event = EventMessage
( MessageEvent
{ blocks = Nothing
, channel = ConversationId
{ unConversationId = "C07LRFB3C8M" }
, text = ""
, channelType = Channel
, files = Nothing
, user = UserId
{ unUserId = "U07M725KAD7" }
, ts = "1725998548.581159"
, threadTs = Nothing
, appId = Just "A01BP7R4KNY"
, botId = Just "B07LDR01Z63"
, attachments = Just
[ MessageAttachment
{ decoded = Just
( DecodedMessageAttachment
{ fallback = Just "[myorg/myrepo] Issue opened by ldub"
, color = Just "36a64f"
, pretext = Just "Issue created by <https://github.com/ldub|ldub>"
, authorName = Nothing
, authorLink = Nothing
, authorIcon = Nothing
, title = Just "<https://github.com/myorg/myrepo/issues/2|#2 test issue 2>"
, titleLink = Nothing
, text = Just "another oneeeee"
, fields = Nothing
, imageUrl = Nothing
, thumbUrl = Nothing
, footer = Just "<https://github.com/myorg/myrepo|myorg/myrepo>"
, footerIcon = Just "https://slack.github.com/static/img/favicon-neutral.png"
, ts = Just "1725998545.0"
, isMsgUnfurl = Nothing
, messageBlocks = Nothing
, authorId = Nothing
, channelId = Nothing
, channelTeam = Nothing
, isAppUnfurl = Nothing
, appUnfurlUrl = Nothing
, fromUrl = Nothing
}
)
, raw = Object
( fromList
[
( "actions"
, Array
[ Object
( fromList
[
( "id"
, String "1"
)
,
( "name"
, String "comment"
)
,
( "style"
, String ""
)
,
( "text"
, String "Comment"
)
,
( "type"
, String "button"
)
,
( "value"
, String "{&amp;amp;quot;selectedOrg&amp;amp;quot;:&amp;amp;quot;myorg&amp;amp;quot;,&amp;amp;quot;selectedOrgId&amp;amp;quot;:140364112,&amp;amp;quot;selectedRepo&amp;amp;quot;:&amp;amp;quot;myrepo&amp;amp;quot;,&amp;amp;quot;selectedRepoId&amp;amp;quot;:809927288,&amp;amp;quot;issueNumber&amp;amp;quot;:2,&amp;amp;quot;issueHtmlUrl&amp;amp;quot;:&amp;amp;quot;https://github.com/myorg/myrepo/issues/2&amp;amp;quot;,&amp;amp;quot;issueTitle&amp;amp;quot;:&amp;amp;quot;test issue 2&amp;amp;quot;}"
)
]
)
, Object
( fromList
[
( "id"
, String "2"
)
,
( "name"
, String "edit"
)
,
( "style"
, String ""
)
,
( "text"
, String "Edit"
)
,
( "type"
, String "button"
)
,
( "value"
, String "{&amp;amp;quot;selectedOrg&amp;amp;quot;:&amp;amp;quot;myorg&amp;amp;quot;,&amp;amp;quot;selectedOrgId&amp;amp;quot;:140364112,&amp;amp;quot;selectedRepo&amp;amp;quot;:&amp;amp;quot;myrepo&amp;amp;quot;,&amp;amp;quot;selectedRepoId&amp;amp;quot;:809927288,&amp;amp;quot;issueNumber&amp;amp;quot;:2,&amp;amp;quot;issueHtmlUrl&amp;amp;quot;:&amp;amp;quot;https://github.com/myorg/myrepo/issues/2&amp;amp;quot;,&amp;amp;quot;issueTitle&amp;amp;quot;:&amp;amp;quot;test issue 2&amp;amp;quot;}"
)
]
)
, Object
( fromList
[
( "id"
, String "3"
)
,
( "name"
, String "close"
)
,
( "style"
, String "danger"
)
,
( "text"
, String "Close"
)
,
( "type"
, String "button"
)
,
( "value"
, String "{&amp;amp;quot;selectedOrg&amp;amp;quot;:&amp;amp;quot;myorg&amp;amp;quot;,&amp;amp;quot;selectedOrgId&amp;amp;quot;:140364112,&amp;amp;quot;selectedRepo&amp;amp;quot;:&amp;amp;quot;myrepo&amp;amp;quot;,&amp;amp;quot;selectedRepoId&amp;amp;quot;:809927288,&amp;amp;quot;issueNumber&amp;amp;quot;:2,&amp;amp;quot;issueHtmlUrl&amp;amp;quot;:&amp;amp;quot;https://github.com/myorg/myrepo/issues/2&amp;amp;quot;,&amp;amp;quot;issueTitle&amp;amp;quot;:&amp;amp;quot;test issue 2&amp;amp;quot;}"
)
]
)
]
)
,
( "callback_id"
, String "issue-opened-interaction"
)
,
( "color"
, String "36a64f"
)
,
( "fallback"
, String "[myorg/myrepo] Issue opened by ldub"
)
,
( "footer"
, String "<https://github.com/myorg/myrepo|myorg/myrepo>"
)
,
( "footer_icon"
, String "https://slack.github.com/static/img/favicon-neutral.png"
)
,
( "id"
, Number 1.0
)
,
( "mrkdwn_in"
, Array
[ String "text" ]
)
,
( "pretext"
, String "Issue created by <https://github.com/ldub|ldub>"
)
,
( "text"
, String "another oneeeee"
)
,
( "title"
, String "<https://github.com/myorg/myrepo/issues/2|#2 test issue 2>"
)
,
( "ts"
, Number 1.725998545e9
)
]
)
}
]
}
)
}
)
Loading

0 comments on commit 649186a

Please sign in to comment.