Skip to content

Commit ff63e5b

Browse files
committed
Fix for properly processing Slack payloads
1. Added SlackText class to represent a text object of Slack 2. Added SlackAction class to represent an action block object of Slack 3. Changed SlackPayload class to use SlackAction for actions 4. Changed SlackEvent class to use SlackAction for actions 5. The user ID of a received event from Slack is in event.user and not in event.user_id 6. Populated the timestamp property of even objects from event.event_ts by converting to UTC datetime 7. Typo and type mismatch fixes
1 parent 293916f commit ff63e5b

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from botbuilder.adapters.slack.slack_text import SlackText
2+
from typing import Optional
3+
4+
5+
# Slack action block (https://api.slack.com/reference/block-kit/block-elements)
6+
class SlackAction:
7+
def __init__(self, **kwargs):
8+
self.action_id: str = kwargs.get("action_id")
9+
self.block_id: str = kwargs.get("block_id")
10+
self.value: str = kwargs.get("value")
11+
self.type: str = kwargs.get("type")
12+
self.action_ts: str = kwargs.get("action_ts")
13+
self.text: Optional[SlackText] = (
14+
None if "text" not in kwargs else SlackText(**(kwargs.get("text")))
15+
)

libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_event.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
from typing import List
4+
from typing import Optional, List
5+
from botbuilder.adapters.slack.slack_action import SlackAction
56
from botbuilder.adapters.slack.slack_message import SlackMessage
67

78

@@ -25,10 +26,13 @@ def __init__(self, **kwargs):
2526
self.user = kwargs.get("user")
2627
self.user_id = kwargs.get("user_id")
2728
self.bot_id = kwargs.get("bot_id")
28-
self.actions: List[str] = kwargs.get("actions")
29+
self.actions: Optional[List[SlackAction]] = None
2930
self.item = kwargs.get("item")
3031
self.item_channel = kwargs.get("item_channel")
3132
self.files: [] = kwargs.get("files")
3233
self.message = (
3334
None if "message" not in kwargs else SlackMessage(**kwargs.get("message"))
3435
)
36+
37+
if "actions" in kwargs:
38+
self.actions = [SlackAction(**action) for action in kwargs.get("actions")]

libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_helper.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import json
55
import urllib.parse
6+
from datetime import datetime, timezone
67

78
from aiohttp.web_request import Request
89
from aiohttp.web_response import Response
@@ -124,9 +125,9 @@ def payload_to_activity(payload: SlackPayload) -> Activity:
124125

125126
activity = Activity(
126127
channel_id="slack",
127-
conversation=ConversationAccount(id=payload.channel.id, properties={}),
128+
conversation=ConversationAccount(id=payload.channel.get("id"), properties={}),
128129
from_property=ChannelAccount(
129-
id=payload.message.bot_id if payload.message.bot_id else payload.user.id
130+
id=payload.message.bot_id if payload.message.bot_id else payload.user.get("id")
130131
),
131132
recipient=ChannelAccount(),
132133
channel_data=payload,
@@ -141,7 +142,7 @@ def payload_to_activity(payload: SlackPayload) -> Activity:
141142
payload.type == "block_actions" or payload.type == "interactive_message"
142143
):
143144
activity.type = ActivityTypes.message
144-
activity.text = payload.actions.value
145+
activity.text = payload.actions[0].value
145146

146147
return activity
147148

@@ -168,12 +169,13 @@ async def event_to_activity(event: SlackEvent, client: SlackClient) -> Activity:
168169
id=event.channel if event.channel else event.channel_id, properties={}
169170
),
170171
from_property=ChannelAccount(
171-
id=event.bot_id if event.bot_id else event.user_id
172+
id=event.bot_id if event.bot_id else event.user_id if event.user_id else event.user
172173
),
173174
recipient=ChannelAccount(id=None),
174175
channel_data=event,
175176
text=event.text,
176177
type=ActivityTypes.event,
178+
timestamp=datetime.fromtimestamp(float(event.event_ts), tz=timezone.utc)
177179
)
178180

179181
if event.thread_ts:
@@ -254,7 +256,7 @@ def query_string_to_dictionary(query: str) -> {}:
254256
for pair in pairs:
255257
key_value = pair.split("=")
256258
key = key_value[0]
257-
value = urllib.parse.unquote(key_value[1])
259+
value = json.loads(urllib.parse.unquote(key_value[1]))
258260

259261
values[key] = value
260262

@@ -287,7 +289,7 @@ def deserialize_body(content_type: str, request_body: str) -> SlackRequestBody:
287289
return SlackRequestBody(**request_dict)
288290

289291
if "payload=" in request_body:
290-
payload = SlackPayload(**request_dict)
292+
payload = SlackPayload(**(request_dict.get("payload")))
291293
return SlackRequestBody(payload=payload, token=payload.token)
292294

293295
return SlackRequestBody(**request_dict)

libraries/botbuilder-adapters-slack/botbuilder/adapters/slack/slack_payload.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
# Licensed under the MIT License.
33

44
from typing import Optional, List
5-
from slack.web.classes.actions import Action
5+
from botbuilder.adapters.slack.slack_action import SlackAction
66
from botbuilder.adapters.slack.slack_message import SlackMessage
77

88

99
class SlackPayload:
1010
def __init__(self, **kwargs):
11-
self.type: List[str] = kwargs.get("type")
11+
self.type: [str] = kwargs.get("type")
1212
self.token: str = kwargs.get("token")
1313
self.channel: str = kwargs.get("channel")
1414
self.thread_ts: str = kwargs.get("thread_ts")
1515
self.team: str = kwargs.get("team")
1616
self.user: str = kwargs.get("user")
17-
self.actions: Optional[List[Action]] = None
17+
self.actions: Optional[List[SlackAction]] = None
1818
self.trigger_id: str = kwargs.get("trigger_id")
1919
self.action_ts: str = kwargs.get("action_ts")
2020
self.submission: str = kwargs.get("submission")
@@ -26,6 +26,9 @@ def __init__(self, **kwargs):
2626
message = kwargs.get("message")
2727
self.message = (
2828
message
29-
if isinstance(message) is SlackMessage
29+
if isinstance(message, SlackMessage)
3030
else SlackMessage(**message)
3131
)
32+
33+
if "actions" in kwargs:
34+
self.actions = [SlackAction(**action) for action in kwargs.get("actions")]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Slack text object (https://api.slack.com/reference/block-kit/composition-objects#text)
2+
class SlackText:
3+
def __init__(self, **kwargs):
4+
self.text: str = kwargs.get("text")
5+
self.type: str = kwargs.get("type")
6+
self.emoji: bool = kwargs.get("emoji")
7+
self.verbatim: bool = kwargs.get("varbatim")

0 commit comments

Comments
 (0)