Пример #1
0
    def handle_team_member_added(self, request):
        data = request.data
        channel_data = data["channelData"]
        # only care if our bot is the new member added
        matches = filter(lambda x: x["id"] == data["recipient"]["id"], data["membersAdded"])
        if not matches:
            return self.respond(status=204)

        team = channel_data["team"]

        # need to keep track of the service url since we won't get it later
        signed_data = {
            "team_id": team["id"],
            "team_name": team["name"],
            "service_url": data["serviceUrl"],
        }

        # sign the params so this can't be forged
        signed_params = sign(**signed_data)

        # send welcome message to the team
        client = get_preinstall_client(data["serviceUrl"])
        card = build_welcome_card(signed_params)
        client.send_card(team["id"], card)
        return self.respond(status=201)
Пример #2
0
    def handle_personal_message(self, request: Request):
        data = request.data
        command_text = data.get("text", "").strip()
        lowercase_command = command_text.lower()
        conversation_id = data["conversation"]["id"]
        teams_user_id = data["from"]["id"]

        # only supporting unlink for now
        if "unlink" in lowercase_command:
            unlink_url = build_unlinking_url(conversation_id,
                                             data["serviceUrl"], teams_user_id)
            card = build_unlink_identity_card(unlink_url)
        elif "help" in lowercase_command:
            card = build_help_command_card()
        elif "link" == lowercase_command:  # don't to match other types of link commands
            has_linked_identity = Identity.objects.filter(
                external_id=teams_user_id).exists()
            if has_linked_identity:
                card = build_already_linked_identity_command_card()
            else:
                card = build_link_identity_command_card()
        else:
            card = build_unrecognized_command_card(command_text)

        client = get_preinstall_client(data["serviceUrl"])
        client.send_card(conversation_id, card)
        return self.respond(status=204)
Пример #3
0
    def handle_personal_member_add(self, request):
        data = request.data
        # only care if our bot is the new member added
        matches = filter(lambda x: x["id"] == data["recipient"]["id"], data["membersAdded"])
        if not matches:
            return self.respond(status=204)

        client = get_preinstall_client(data["serviceUrl"])

        user_conversation_id = data["conversation"]["id"]
        card = build_personal_installation_message()
        client.send_card(user_conversation_id, card)
        return self.respond(status=204)
Пример #4
0
    def handle_personal_message(self, request):
        data = request.data
        command_text = data.get("text", "").strip()
        conversation_id = data["conversation"]["id"]

        # only supporting unlink for now
        if "unlink" in command_text:
            unlink_url = build_unlinking_url(conversation_id,
                                             data["serviceUrl"],
                                             data["from"]["id"])
            card = build_unlink_identity_card(unlink_url)
        else:
            card = build_unrecognized_command_card(command_text)

        client = get_preinstall_client(data["serviceUrl"])
        client.send_card(conversation_id, card)
        return self.respond(status=204)
Пример #5
0
    def handle_channel_message(self, request):
        data = request.data

        # check to see if we are mentioned
        recipient_id = data.get("recipient", {}).get("id")
        if recipient_id:
            # check the ids of the mentions in the entities
            mentioned = (len(
                filter(
                    lambda x: x.get("mentioned", {}).get("id") == recipient_id,
                    data.get("entities", []),
                )) > 0)
            if mentioned:
                client = get_preinstall_client(data["serviceUrl"])
                card = build_mentioned_card()
                conversation_id = data["conversation"]["id"]
                client.send_card(conversation_id, card)

        return self.respond(status=204)
Пример #6
0
    def handle_action_submitted(self, request):
        # pull out parameters
        data = request.data
        channel_data = data["channelData"]
        tenant_id = channel_data["tenant"]["id"]
        payload = data["value"]["payload"]
        group_id = payload["groupId"]
        integration_id = payload["integrationId"]
        user_id = data["from"]["id"]
        activity_id = data["replyToId"]
        conversation = data["conversation"]
        if conversation["conversationType"] == "personal":
            conversation_id = conversation["id"]
        else:
            conversation_id = channel_data["channel"]["id"]

        try:
            integration = Integration.objects.get(id=integration_id)
        except Integration.DoesNotExist:
            logger.info(
                "msteams.action.missing-integration", extra={"integration_id": integration_id}
            )
            return self.respond(status=404)

        team_id = integration.external_id
        client = MsTeamsClient(integration)

        try:
            group = Group.objects.select_related("project__organization").get(
                project__in=Project.objects.filter(
                    organization__in=integration.organizations.all()
                ),
                id=group_id,
            )
        except Group.DoesNotExist:
            logger.info(
                "msteams.action.invalid-issue",
                extra={"team_id": team_id, "integration_id": integration.id},
            )
            return self.respond(status=404)

        try:
            idp = IdentityProvider.objects.get(type="msteams", external_id=team_id)
        except IdentityProvider.DoesNotExist:
            logger.info(
                "msteams.action.invalid-team-id",
                extra={
                    "team_id": team_id,
                    "integration_id": integration.id,
                    "organization_id": group.organization.id,
                },
            )
            return self.respond(status=404)

        try:
            identity = Identity.objects.get(idp=idp, external_id=user_id)
        except Identity.DoesNotExist:
            associate_url = build_linking_url(
                integration, group.organization, user_id, team_id, tenant_id
            )

            card = build_linking_card(associate_url)
            user_conversation_id = client.get_user_conversation_id(user_id, tenant_id)
            client.send_card(user_conversation_id, card)
            return self.respond(status=201)

        # update the state of the issue
        issue_change_response = self.issue_state_change(group, identity, data["value"])

        # get the rules from the payload
        rules = Rule.objects.filter(id__in=payload["rules"])

        # pull the event based off our payload
        event = eventstore.get_event_by_id(group.project_id, payload["eventId"])
        if event is None:
            logger.info(
                "msteams.action.event-missing",
                extra={
                    "team_id": team_id,
                    "integration_id": integration.id,
                    "organization_id": group.organization.id,
                    "event_id": payload["eventId"],
                    "project_id": group.project_id,
                },
            )
            return self.respond(status=404)

        # refresh issue and update card
        group.refresh_from_db()
        card = build_group_card(group, event, rules, integration)
        client.update_card(conversation_id, activity_id, card)

        return issue_change_response
Пример #7
0
    def handle_action_submitted(self, request):
        data = request.data
        channel_data = data["channelData"]
        team_id = channel_data["team"]["id"]
        tenant_id = channel_data["tenant"]["id"]
        group_id = data["value"]["groupId"]
        user_id = data["from"]["id"]

        try:
            integration = Integration.objects.get(provider=self.provider,
                                                  external_id=team_id)
        except Integration.DoesNotExist:
            logger.info("msteams.action.missing-integration",
                        extra={"team_id": team_id})
            return self.respond(status=404)

        try:
            group = Group.objects.select_related("project__organization").get(
                project__in=Project.objects.filter(
                    organization__in=integration.organizations.all()),
                id=group_id,
            )
        except Group.DoesNotExist:
            logger.info(
                "msteams.action.invalid-issue",
                extra={
                    "team_id": team_id,
                    "integration_id": integration.id
                },
            )
            return self.respond(status=404)

        try:
            idp = IdentityProvider.objects.get(type="msteams",
                                               external_id=team_id)
        except IdentityProvider.DoesNotExist:
            logger.info(
                "msteams.action.invalid-team-id",
                extra={
                    "team_id": team_id,
                    "integration_id": integration.id,
                    "organization_id": group.organization.id,
                },
            )
            return self.respond(status=404)

        try:
            identity = Identity.objects.get(idp=idp, external_id=user_id)
        except Identity.DoesNotExist:
            associate_url = build_linking_url(integration, group.organization,
                                              user_id, team_id, tenant_id)

            client = MsTeamsClient(integration)

            card = build_linking_card(associate_url)
            user_conversation_id = client.get_user_conversation_id(
                user_id, tenant_id)
            client.send_card(user_conversation_id, card)
            return self.respond(status=201)

        return self.issue_state_change(group, identity, data["value"])