/
slack_api.py
70 lines (53 loc) · 1.87 KB
/
slack_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
from sirbot import SirBot
from slack import ROOT_URL
from slack.exceptions import SlackAPIError
from pybot.endpoints.api.utils import (
_slack_info_from_email,
handle_slack_invite_error,
production_only,
)
from pybot.plugins import APIPlugin
from pybot.plugins.api.request import SlackApiRequest
logger = logging.getLogger(__name__)
def create_endpoints(plugin: APIPlugin):
plugin.on_get("verify", verify, wait=True)
plugin.on_get("invite", invite, wait=True)
async def verify(request: SlackApiRequest, app: SirBot) -> dict:
"""
Verifies whether a user exists in the configured slack group with
the given email
:return: The user's slack id and displayName if they exist
"""
slack = app.plugins["slack"].api
email = request.query["email"]
user = await _slack_info_from_email(email, slack)
if user:
return {"exists": True, "id": user["id"], "displayName": user["name"]}
return {"exists": False}
@production_only
async def invite(request: SlackApiRequest, app: SirBot):
"""
Pulls an email out of the querystring and sends it an invite
to the slack team
:return: The request response from slack
"""
admin_slack = app.plugins["admin_slack"].api
slack = app.plugins["slack"].api
body = await request.json()
if "email" not in body:
return {"error": "Must contain `email` JSON value"}
email = body["email"]
try:
response = await admin_slack.query(
url=ROOT_URL + "users.admin.invite", data={"email": email}
)
return response
except SlackAPIError as e:
logger.info("Slack invite resulted in SlackAPIError: " + e.error)
await handle_slack_invite_error(email, e, slack)
return e.data
except Exception as e:
logger.exception(e)
await handle_slack_invite_error(email, e, slack)
return e