diff --git a/changelog/6986.bugfix.md b/changelog/6986.bugfix.md new file mode 100644 index 000000000000..dbea6c7417ba --- /dev/null +++ b/changelog/6986.bugfix.md @@ -0,0 +1 @@ +- Updated Slack Connector for new Slack Events API diff --git a/rasa/core/channels/slack.py b/rasa/core/channels/slack.py index a04cc44f52bc..ff413cb1aa41 100644 --- a/rasa/core/channels/slack.py +++ b/rasa/core/channels/slack.py @@ -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 @@ -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": @@ -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 {} diff --git a/tests/core/test_channels.py b/tests/core/test_channels.py index bf374ba9f1e8..901d38ee83ab 100644 --- a/tests/core/test_channels.py +++ b/tests/core/test_channels.py @@ -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 @@ -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 @@ -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