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

Updated Slack Connector for new Events API #6986

Merged
merged 11 commits into from
Oct 15, 2020
1 change: 1 addition & 0 deletions changelog/6986.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Updated Slack Connector for new Slack Events API
20 changes: 17 additions & 3 deletions rasa/core/channels/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _is_user_message(slack_event: Dict) -> bool:
)

@staticmethod
def _sanitize_user_message(text, uids_to_remove) -> Text:
def _sanitize_user_message(text: Text, uids_to_remove: List[Text]) -> Text:
"""Remove superfluous/wrong/problematic tokens from a message.

Probably a good starting point for pre-formatting of user-provided text
Expand Down Expand Up @@ -367,10 +367,19 @@ def get_metadata(self, request: Request) -> Dict[Text, Any]:
event = slack_event.get("event", {})
thread_id = event.get("thread_ts", event.get("ts"))

users = []
if "authed_users" in slack_event:
users = slack_event.get("authed_users")
elif (
"authorizations" in slack_event
and len(slack_event.get("authorizations")) > 0
):
users.append(slack_event.get("authorizations")[0].get("user_id"))

return {
"out_channel": event.get("channel"),
"thread_id": thread_id,
"users": slack_event.get("authed_users"),
"users": users,
}

if content_type == "application/x-www-form-urlencoded":
Expand All @@ -379,10 +388,15 @@ def get_metadata(self, request: Request) -> Dict[Text, Any]:
payload = json.loads(output["payload"][0])
message = payload.get("message", {})
thread_id = message.get("thread_ts", message.get("ts"))

users = []
if payload.get("user", {}).get("id"):
users.append(payload.get("user", {}).get("id"))

return {
"out_channel": payload.get("channel", {}).get("id"),
"thread_id": thread_id,
"users": payload.get("user", {}).get("id"),
"users": users,
}

return {}
Expand Down
6 changes: 3 additions & 3 deletions tests/core/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def test_slack_form_metadata():
r.headers = header
metadata = input_channel.get_metadata(request=r)
assert metadata["out_channel"] == channel
assert metadata["users"] == authed_user
assert metadata["users"][0] == authed_user
assert metadata["thread_id"] == ts


Expand Down Expand Up @@ -631,7 +631,7 @@ def test_slack_metadata_missing_keys():
r.json = direct_message_event
r.headers = header
metadata = input_channel.get_metadata(request=r)
assert metadata["users"] is None
assert metadata["users"] == []
assert metadata["out_channel"] == channel
assert metadata["thread_id"] == ts

Expand Down Expand Up @@ -675,7 +675,7 @@ def test_slack_form_metadata_missing_keys():
r.form = form_event
r.headers = header
metadata = input_channel.get_metadata(request=r)
assert metadata["users"] is None
assert metadata["users"] == []
assert metadata["out_channel"] == channel
assert metadata["thread_id"] == ts

Expand Down