From 691ea0c08042294bc011b0f9781dcd8be3544768 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Mon, 27 Mar 2023 23:37:00 +0530 Subject: [PATCH] feat: slack bot setup (#545) --- .../plane/bgtasks/workspace_invitation_task.py | 17 +++++++++++++++-- apiserver/plane/db/models/user.py | 13 +++++++++++++ apiserver/plane/settings/local.py | 1 + apiserver/plane/settings/production.py | 1 + apiserver/plane/settings/staging.py | 1 + 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/apiserver/plane/bgtasks/workspace_invitation_task.py b/apiserver/plane/bgtasks/workspace_invitation_task.py index 706635588..0ed807171 100644 --- a/apiserver/plane/bgtasks/workspace_invitation_task.py +++ b/apiserver/plane/bgtasks/workspace_invitation_task.py @@ -2,10 +2,13 @@ from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.html import strip_tags +from django.conf import settings # Third party imports from django_rq import job from sentry_sdk import capture_exception +from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError # Module imports from plane.db.models import Workspace, User, WorkspaceMemberInvite @@ -13,9 +16,7 @@ from plane.db.models import Workspace, User, WorkspaceMemberInvite @job("default") def workspace_invitation(email, workspace_id, token, current_site, invitor): - try: - workspace = Workspace.objects.get(pk=workspace_id) workspace_member_invite = WorkspaceMemberInvite.objects.get( token=token, email=email @@ -49,6 +50,18 @@ def workspace_invitation(email, workspace_id, token, current_site, invitor): msg = EmailMultiAlternatives(subject, text_content, from_email_string, [email]) msg.attach_alternative(html_content, "text/html") msg.send() + + # Send message on slack as well + if settings.SLACK_BOT_TOKEN: + client = WebClient(token=settings.SLACK_BOT_TOKEN) + try: + _ = client.chat_postMessage( + channel="#trackers", + text=f"{workspace_member_invite.email} has been invited to {workspace.name} as a {workspace_member_invite.role}", + ) + except SlackApiError as e: + print(f"Got an error: {e.response['error']}") + return except (Workspace.DoesNotExist, WorkspaceMemberInvite.DoesNotExist) as e: return diff --git a/apiserver/plane/db/models/user.py b/apiserver/plane/db/models/user.py index 896681808..8a30981f3 100644 --- a/apiserver/plane/db/models/user.py +++ b/apiserver/plane/db/models/user.py @@ -11,9 +11,12 @@ from django.utils import timezone from django.core.mail import EmailMultiAlternatives from django.template.loader import render_to_string from django.utils.html import strip_tags +from django.conf import settings # Third party imports from sentry_sdk import capture_exception +from slack_sdk import WebClient +from slack_sdk.errors import SlackApiError class User(AbstractBaseUser, PermissionsMixin): @@ -123,6 +126,16 @@ def send_welcome_email(sender, instance, created, **kwargs): msg.attach_alternative(html_content, "text/html") msg.send() + # Send message on slack as well + if settings.SLACK_BOT_TOKEN: + client = WebClient(token=settings.SLACK_BOT_TOKEN) + try: + _ = client.chat_postMessage( + channel="#trackers", + text=f"New user {instance.email} has signed up and begun the onboarding journey.", + ) + except SlackApiError as e: + print(f"Got an error: {e.response['error']}") return except Exception as e: capture_exception(e) diff --git a/apiserver/plane/settings/local.py b/apiserver/plane/settings/local.py index 6cb9ebcc5..21d4ec0ea 100644 --- a/apiserver/plane/settings/local.py +++ b/apiserver/plane/settings/local.py @@ -83,3 +83,4 @@ ANALYTICS_SECRET_KEY = os.environ.get("ANALYTICS_SECRET_KEY", False) ANALYTICS_BASE_API = os.environ.get("ANALYTICS_BASE_API", False) OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", False) GPT_ENGINE = os.environ.get("GPT_ENGINE", "text-davinci-003") +SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN", False) diff --git a/apiserver/plane/settings/production.py b/apiserver/plane/settings/production.py index 87d59c049..ba8268b75 100644 --- a/apiserver/plane/settings/production.py +++ b/apiserver/plane/settings/production.py @@ -231,3 +231,4 @@ ANALYTICS_SECRET_KEY = os.environ.get("ANALYTICS_SECRET_KEY", False) ANALYTICS_BASE_API = os.environ.get("ANALYTICS_BASE_API", False) OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", False) GPT_ENGINE = os.environ.get("GPT_ENGINE", "text-davinci-003") +SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN", False) diff --git a/apiserver/plane/settings/staging.py b/apiserver/plane/settings/staging.py index 48a9702b9..0881d847f 100644 --- a/apiserver/plane/settings/staging.py +++ b/apiserver/plane/settings/staging.py @@ -192,3 +192,4 @@ ANALYTICS_SECRET_KEY = os.environ.get("ANALYTICS_SECRET_KEY", False) ANALYTICS_BASE_API = os.environ.get("ANALYTICS_BASE_API", False) OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", False) GPT_ENGINE = os.environ.get("GPT_ENGINE", "text-davinci-003") +SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN", False)