|
| 1 | +import logging |
| 2 | +from sirbot import SirBot |
| 3 | +from slack import ROOT_URL |
| 4 | +from slack.exceptions import SlackAPIError |
| 5 | +from aiohttp.web_request import Request |
| 6 | + |
| 7 | +from pybot.endpoints.api.utils import _slack_info_from_email |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def create_endpoints(plugin): |
| 13 | + plugin.on_get("verify", verify, wait=True) |
| 14 | + plugin.on_get("invite", invite, wait=True) |
| 15 | + |
| 16 | + |
| 17 | +async def verify(request: Request, app: SirBot) -> any: |
| 18 | + """ |
| 19 | + Verifies whether a user exists in the configured slack group with |
| 20 | + the given email |
| 21 | +
|
| 22 | + :return: The user's slack id and displayName if they exist |
| 23 | + """ |
| 24 | + slack = app.plugins["slack"].api |
| 25 | + email = request.query["email"] |
| 26 | + |
| 27 | + user = await _slack_info_from_email(email, slack) |
| 28 | + if user: |
| 29 | + return {"exists": True, "id": user["id"], "displayName": user["name"]} |
| 30 | + return {"exists": False} |
| 31 | + |
| 32 | + |
| 33 | +async def invite(request: Request, app: SirBot): |
| 34 | + """ |
| 35 | + Pulls an email out of the querystring and sends it an invite |
| 36 | + to the slack team |
| 37 | +
|
| 38 | + :return: The request response from slack |
| 39 | + """ |
| 40 | + try: |
| 41 | + slack = app.plugins["admin_slack"].api |
| 42 | + email = request.query["email"] |
| 43 | + |
| 44 | + response = await slack.query( |
| 45 | + url=ROOT_URL + "users.admin.invite", data={"email": email} |
| 46 | + ) |
| 47 | + # logger.info("Response from slack: ", response) |
| 48 | + return response |
| 49 | + |
| 50 | + except SlackAPIError as e: |
| 51 | + logger.info(e) |
| 52 | + return e.data |
0 commit comments