11import logging
2+
23from sirbot import SirBot
34from slack import ROOT_URL
45from slack .exceptions import SlackAPIError
5- from aiohttp .web_request import Request
66
7- from pybot .endpoints .api .utils import _slack_info_from_email
7+ from pybot .endpoints .api .utils import _slack_info_from_email , handle_slack_invite_error
8+ from pybot .plugins import APIPlugin
9+ from pybot .plugins .api .request import SlackApiRequest
810
911logger = logging .getLogger (__name__ )
1012
1113
12- def create_endpoints (plugin ):
14+ def create_endpoints (plugin : APIPlugin ):
1315 plugin .on_get ("verify" , verify , wait = True )
1416 plugin .on_get ("invite" , invite , wait = True )
1517
1618
17- async def verify (request : Request , app : SirBot ) -> any :
19+ async def verify (request : SlackApiRequest , app : SirBot ) -> dict :
1820 """
1921 Verifies whether a user exists in the configured slack group with
2022 the given email
@@ -30,23 +32,34 @@ async def verify(request: Request, app: SirBot) -> any:
3032 return {"exists" : False }
3133
3234
33- async def invite (request : Request , app : SirBot ):
35+ async def invite (request : SlackApiRequest , app : SirBot ):
3436 """
3537 Pulls an email out of the querystring and sends it an invite
3638 to the slack team
3739
3840 :return: The request response from slack
3941 """
40- try :
41- slack = app .plugins ["admin_slack" ].api
42- email = request .query ["email" ]
42+ slack = app .plugins ["admin_slack" ].api
43+ body = await request .json ()
44+
45+ if "email" not in body :
46+ return {"error" : "Must contain `email` JSON value" }
47+ email = body ["email" ]
4348
49+ try :
4450 response = await slack .query (
4551 url = ROOT_URL + "users.admin.invite" , data = {"email" : email }
4652 )
47- # logger.info("Response from slack: ", response)
4853 return response
4954
5055 except SlackAPIError as e :
51- logger .info (e )
52- return e .data
56+ logger .info ("Slack invite resulted in SlackAPIError: " + e .error )
57+ if e .data ["error" ] == "already_invited" :
58+ return e .data
59+ else :
60+ await handle_slack_invite_error (email , e , slack )
61+
62+ except Exception as e :
63+ logger .exception (e )
64+ await handle_slack_invite_error (email , e , slack )
65+ return e
0 commit comments