From ca893a34c40dc3b3ba7f5144a653a142e6ebaaca Mon Sep 17 00:00:00 2001 From: Phil Christensen Date: Sun, 20 Oct 2024 14:31:34 -0400 Subject: [PATCH] feat: move optional chat features into dedicated apps --- .../app_home}/__init__.py | 0 django_slackbot/app_home/apps.py | 6 +++ django_slackbot/app_home/chat.py | 37 +++++++++++++++++++ django_slackbot/chat.py | 29 --------------- django_slackbot/frinkiac/__init__.py | 0 .../frinkiac/apps.py | 2 +- .../frinkiac/chat.py | 0 examples/settings.py | 3 +- 8 files changed, 46 insertions(+), 31 deletions(-) rename {examples/frinkiac => django_slackbot/app_home}/__init__.py (100%) create mode 100644 django_slackbot/app_home/apps.py create mode 100644 django_slackbot/app_home/chat.py create mode 100644 django_slackbot/frinkiac/__init__.py rename {examples => django_slackbot}/frinkiac/apps.py (70%) rename {examples => django_slackbot}/frinkiac/chat.py (100%) diff --git a/examples/frinkiac/__init__.py b/django_slackbot/app_home/__init__.py similarity index 100% rename from examples/frinkiac/__init__.py rename to django_slackbot/app_home/__init__.py diff --git a/django_slackbot/app_home/apps.py b/django_slackbot/app_home/apps.py new file mode 100644 index 0000000..003340c --- /dev/null +++ b/django_slackbot/app_home/apps.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from django.apps import AppConfig + + +class AppHomeConfig(AppConfig): + name = 'django_slackbot.app_home' diff --git a/django_slackbot/app_home/chat.py b/django_slackbot/app_home/chat.py new file mode 100644 index 0000000..8fc58ca --- /dev/null +++ b/django_slackbot/app_home/chat.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +import logging +from urllib.parse import urlencode, parse_qs, urlparse + +from slackblocks import SectionBlock, HeaderBlock, DividerBlock +from slackblocks.messages import BaseMessage + +from django_slackbot.chat import app + +log = logging.getLogger(__name__) + +class ViewMessage(BaseMessage): + def _resolve(self): + result = {**super()._resolve()} + result.pop("mrkdwn", None) + result.pop("text", None) + return result + +@app.event("app_home_opened") +def app_home_opened(event, ack, client): + ack() + details = client.auth_test() + channels = client.users_conversations(types="public_channel,private_channel") + channellist = '' + for channel in channels['channels']: + channellist += f"* <#{channel['id']}>\n" + view = ViewMessage(blocks=[ + HeaderBlock(f":wave: Hello, I'm {details['user']}!"), + DividerBlock(), + SectionBlock("\n*Active in Channels:*"), + DividerBlock(), + SectionBlock(f"{channellist}"), + ]) + client.views_publish( + user_id = event['user'], + view = dict(type="home", **view) + ) diff --git a/django_slackbot/chat.py b/django_slackbot/chat.py index 67dfdd8..6a64b3b 100644 --- a/django_slackbot/chat.py +++ b/django_slackbot/chat.py @@ -2,8 +2,6 @@ import os from slack_bolt import App -from slackblocks import SectionBlock, HeaderBlock, DividerBlock -from slackblocks.messages import BaseMessage from crontab import CronTab app = App(token=os.environ.get("SLACK_BOT_TOKEN")) @@ -18,33 +16,6 @@ def _schedule(f): return _schedule app.schedule = app_schedule -class ViewMessage(BaseMessage): - def _resolve(self): - result = {**super()._resolve()} - result.pop("mrkdwn", None) - result.pop("text", None) - return result - -@app.event("app_home_opened") -def app_home_opened(event, ack, client): - ack() - details = client.auth_test() - channels = client.users_conversations(types="public_channel,private_channel") - channellist = '' - for channel in channels['channels']: - channellist += f"* <#{channel['id']}>\n" - view = ViewMessage(blocks=[ - HeaderBlock(f":wave: Hello, I'm {details['user']}!"), - DividerBlock(), - SectionBlock("\n*Active in Channels:*"), - DividerBlock(), - SectionBlock(f"{channellist}"), - ]) - client.views_publish( - user_id = event['user'], - view = dict(type="home", **view) - ) - def check_access(client, group_id, user_id): response = client.usergroups_users_list(usergroup=group_id) return user_id in response['users'] diff --git a/django_slackbot/frinkiac/__init__.py b/django_slackbot/frinkiac/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/frinkiac/apps.py b/django_slackbot/frinkiac/apps.py similarity index 70% rename from examples/frinkiac/apps.py rename to django_slackbot/frinkiac/apps.py index ea099c3..b24f9f1 100644 --- a/examples/frinkiac/apps.py +++ b/django_slackbot/frinkiac/apps.py @@ -3,4 +3,4 @@ class FrinkiacConfig(AppConfig): - name = 'examples.frinkiac' + name = 'django_slackbot.frinkiac' diff --git a/examples/frinkiac/chat.py b/django_slackbot/frinkiac/chat.py similarity index 100% rename from examples/frinkiac/chat.py rename to django_slackbot/frinkiac/chat.py diff --git a/examples/settings.py b/examples/settings.py index 39d9310..bc91543 100644 --- a/examples/settings.py +++ b/examples/settings.py @@ -39,7 +39,8 @@ 'django.contrib.messages', 'django.contrib.staticfiles', 'django_slackbot', - 'examples.frinkiac' + 'django_slackbot.frinkiac', + 'django_slackbot.app_home', ] MIDDLEWARE = [