Esempio n. 1
0
 def __init__(self, message: str, token, team_id, event):
     self._message = message
     self._token = token
     self._team_id = team_id
     self._event = event
     self._course, self._bot_token = get_team_data(team_id)
     self._process()
Esempio n. 2
0
    def message_send():
        d = request.json
        try:
            if "challenge" in d:
                return
            team_id = d["team_id"]
            course, bot_token = get_team_data(team_id)
            event = d["event"]

            if event["type"] == "channel_created":
                print(
                    requests.post(
                        "https://slack.com/api/conversations.join",
                        json={
                            "channel": event["channel"]["id"]
                        },
                        headers={
                            "Authorization": "Bearer {}".format(bot_token)
                        },
                    ).json())
                return

            token = get_user_token(event["user"])
            if token is REJECTED:
                return
            if "edited" in event:
                return
            if "subtype" in event:
                return

            features = CONFIG[course]["features"]

            integrations = []
            if features.get("piazza"):
                integrations.append(PiazzaIntegration)
            if features.get("emojify"):
                integrations.append(EmojiIntegration)
            if features.get("golinks"):
                integrations.append(GoLinkIntegration)

            combined_integration = combine_integrations(integrations)(
                event["text"], token if token is not UNABLE else None, team_id)

            if (combined_integration.message != event["text"]
                    or combined_integration.attachments):
                if token is not UNABLE:
                    resp = requests.post(
                        "https://slack.com/api/chat.update",
                        json={
                            "channel": event["channel"],
                            "ts": event["ts"],
                            "as_user": True,
                            "text": combined_integration.message,
                            "attachments": combined_integration.attachments,
                        },
                        headers={
                            "Authorization": "Bearer {}".format(token)
                        },
                    ).json()
                    if not resp["ok"] and resp["error"] in {
                            "invalid_auth",
                            "token_revoked",
                            "account_inactive",
                            "missing_scope",
                    }:
                        # token available, but no permissions
                        token = UNABLE

                if token is UNABLE or "slack_force" in event["text"]:
                    requests.post(
                        "https://slack.com/api/chat.postEphemeral",
                        json={
                            "blocks":
                            make_promo_block(combined_integration.message),
                            "attachments": [],
                            "channel":
                            event["channel"],
                            "user":
                            event["user"],
                            "username":
                            "******"
                        },
                        headers={
                            "Authorization": "Bearer {}".format(bot_token)
                        },
                    ).json()

        except Exception as e:
            print("".join(
                traceback.TracebackException.from_exception(e).format()))
        finally:
            if "challenge" in d:
                return d["challenge"]
            return ""
Esempio n. 3
0
    def message_send():
        d = request.json
        try:
            if "challenge" in d or request.headers.get("X-Slack-Retry-Reason"):
                return
            team_id = d["team_id"]
            course, bot_token = get_team_data(team_id)
            event = d["event"]

            if event["type"] == "channel_created":
                requests.post(
                    "https://slack.com/api/conversations.join",
                    json={"channel": event["channel"]["id"]},
                    headers={"Authorization": "Bearer {}".format(bot_token)},
                ).json()
                return

            token = get_user_token(event["user"])
            if token is REJECTED:
                return
            if "edited" in event:
                return
            if "subtype" in event:
                return

            with connect_db() as db:
                ret = db(
                    "SELECT service FROM activated_services WHERE course = (%s)",
                    [course],
                )
                active_services = set(x[0] for x in ret)

            integrations = []
            if "piazza" in active_services:
                integrations.append(PiazzaIntegration)
            elif "ed" in active_services:
                integrations.append(EdIntegration)
            if "claps" in active_services:
                integrations.append(ClapIntegration)
            if "emojify" in active_services:
                integrations.append(EmojiIntegration)
            if "golinks" in active_services:
                integrations.append(GoLinkIntegration)
            if "groups" in active_services:
                integrations.append(GroupIntegration)
            if "prlinks" in active_services:
                integrations.append(PRLinkIntegration)
            if "issues" in active_services:
                integrations.append(IssueIntegration)
            if "appsprlinks" in active_services:
                integrations.append(AppsPRLinkIntegration)
            if "build" in active_services:
                integrations.append(BuildIntegration)
            if "lgtm" in active_services:
                integrations.append(LGTMIntegration)

            combined_integration = combine_integrations(integrations)(
                event["text"], token if token is not UNABLE else None, team_id, event
            )

            if combined_integration.responses:
                for response in combined_integration.responses:
                    requests.post(
                        "https://slack.com/api/chat.postMessage",
                        json={
                            "channel": event["channel"],
                            **(
                                {"thread_ts": event["thread_ts"]}
                                if "thread_ts" in event
                                else {}
                            ),
                            "text": response,
                        },
                        headers={"Authorization": "Bearer {}".format(bot_token)},
                    )

            if (
                combined_integration.message != event["text"]
                or combined_integration.attachments
            ):
                if token is not UNABLE:
                    resp = requests.post(
                        "https://slack.com/api/chat.update",
                        json={
                            "channel": event["channel"],
                            "ts": event["ts"],
                            "as_user": True,
                            "text": combined_integration.message,
                            "attachments": combined_integration.attachments,
                        },
                        headers={"Authorization": "Bearer {}".format(token)},
                    ).json()
                    if not resp["ok"] and resp["error"] in {
                        "invalid_auth",
                        "token_revoked",
                        "account_inactive",
                        "missing_scope",
                    }:
                        # token available, but no permissions
                        token = UNABLE

                if token is UNABLE or "slack_force" in event["text"]:
                    requests.post(
                        "https://slack.com/api/chat.postEphemeral",
                        json={
                            "blocks": make_promo_block(combined_integration.message),
                            "attachments": [],
                            "channel": event["channel"],
                            "user": event["user"],
                            **(
                                {"thread_ts": event["thread_ts"]}
                                if "thread_ts" in event
                                else {}
                            ),
                            "username": "******",
                        },
                        headers={"Authorization": "Bearer {}".format(bot_token)},
                    ).json()

        except Exception as e:
            print("".join(traceback.TracebackException.from_exception(e).format()))
        finally:
            if "challenge" in d:
                return d["challenge"]
            return ""