Skip to content

Commit f532dd4

Browse files
committed
Cleanup slack invite error handling
1 parent c947242 commit f532dd4

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

pybot/endpoints/api/slack_api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ async def invite(request: SlackApiRequest, app: SirBot):
5555

5656
except SlackAPIError as e:
5757
logger.info("Slack invite resulted in SlackAPIError: " + e.error)
58-
if e.error != "already_invited" and e.error != "already_in_team":
59-
await handle_slack_invite_error(email, e, slack)
58+
await handle_slack_invite_error(email, e, slack)
6059
return e.data
6160

6261
except Exception as e:

pybot/endpoints/api/utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313

1414
async def _slack_info_from_email(
15-
email: str, slack: SlackAPI, fallback: Optional[str] = None
16-
):
15+
email: str, slack: SlackAPI, fallback: Optional[dict] = None
16+
) -> dict:
1717
try:
1818
response = await slack.query(
1919
url=ROOT_URL + "users.lookupByEmail", data={"email": email}
@@ -55,7 +55,21 @@ def invite_failure_attachments(email: str, error: str) -> list:
5555

5656

5757
async def handle_slack_invite_error(email, error, slack):
58+
if error.error == "already_invited":
59+
return error.data
60+
5861
attachments = invite_failure_attachments(email, error)
62+
63+
if error.error == "already_in_team":
64+
slack_user = await _slack_info_from_email(email, slack)
65+
attachments[0]["fields"].append(
66+
{
67+
"title": "Slack Username",
68+
"value": f"<@{slack_user['id']}>",
69+
"short": True,
70+
}
71+
)
72+
5973
response = {
6074
"channel": TICKET_CHANNEL,
6175
"attachments": attachments,

0 commit comments

Comments
 (0)