Skip to content

Commit c4ab439

Browse files
authored
Feature/scheduled messages (#37)
Added support for scheduling templated messages. You can add a template, and then pass it as an argument to management command. Then system cron run this command at specified intervals.
1 parent 76ce937 commit c4ab439

File tree

8 files changed

+97
-2
lines changed

8 files changed

+97
-2
lines changed

deploy/playbooks/01_setup.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,3 @@
9292
- name: Display the public key
9393
debug:
9494
msg: "For private repositories, make sure to put this key as deploy key on github: {{ deploy_key.content | b64decode }}"
95-

deploy/playbooks/04_cron.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@
1515
minute: "5"
1616
hour: "6"
1717
job: "make prod/cron/pretix"
18+
19+
- name: "Schedule standup message on Monday morning"
20+
ansible.builtin.cron:
21+
name: "Send a standup message"
22+
minute: "5"
23+
hour: "9"
24+
weekday: "1"
25+
job: "make prod/cron/standup"

deploy/templates/app/Makefile.app.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ prod/migrate:
77
$(MAKE_APP) in-container/migrate
88

99
prod/shell:
10-
$(MAKE_APP) in-container/shell
10+
$(MAKE_APP) in-container/manage ARG="shell_plus"
1111

1212
prod/db_shell:
1313
$(MAKE_APP) in-container/db_shell
@@ -22,5 +22,8 @@ prod/cron/pretalx:
2222
prod/cron/pretix:
2323
$(MAKE_APP) in-container/manage ARG="download_pretix_data --event=ep2025"
2424

25+
prod/cron/standup:
26+
$(MAKE_APP) in-container/manage ARG="send_scheduled_message --template=standup"
27+
2528
logs:
2629
docker compose logs -f

intbot/core/bot/channel_router.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class Channels:
7777
channel_name=settings.DISCORD_GRANTS_CHANNEL_NAME,
7878
)
7979

80+
# scheduled messages
81+
standup_channel = DiscordChannel(
82+
channel_id=settings.DISCORD_STANDUP_CHANNEL_ID,
83+
channel_name=settings.DISCORD_STANDUP_CHANNEL_NAME,
84+
)
85+
8086

8187
def discord_channel_router(wh: Webhook) -> DiscordChannel:
8288
if wh.source == "github":

intbot/core/bot/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
Configuration for all things discord related
3+
"""
4+
from django.conf import settings
5+
6+
class Roles:
7+
# We keep this statically defined, because we want to use it in templates
8+
# for scheduled messages, and we want to make the scheduling available
9+
# withotu access to the discord server.
10+
board_member_role_id = settings.DISCORD_BOARD_MEMBER_ROLE_ID

intbot/core/bot/scheduled_messages.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
Factory functions for scheduled messages
3+
"""
4+
5+
from typing import Dict, Callable
6+
7+
from core.models import DiscordMessage
8+
from core.bot.channel_router import Channels
9+
from core.bot.config import Roles
10+
11+
12+
def standup_message_factory() -> DiscordMessage:
13+
"""Factory for weekly standup message."""
14+
content = (
15+
f"## Happy Monday <@&{Roles.board_member_role_id}>!\n\n"
16+
f"Let's keep everyone in the loop :)\n\n"
17+
f"(1) What you worked on last week\n"
18+
f"(2) What are you planning to work on this week\n"
19+
f"(3) Are there any blockers or where could you use some help?"
20+
)
21+
22+
# Using the test channel for now - replace with appropriate channel later
23+
channel = Channels.standup_channel
24+
25+
return DiscordMessage(
26+
channel_id=channel.channel_id,
27+
channel_name=channel.channel_name,
28+
content=content,
29+
sent_at=None
30+
)
31+
32+
33+
# Registry of message factories
34+
MESSAGE_FACTORIES: Dict[str, Callable[[], DiscordMessage]] = {
35+
"standup": standup_message_factory,
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from core.bot.scheduled_messages import MESSAGE_FACTORIES
2+
from django.core.management.base import BaseCommand
3+
4+
5+
class Command(BaseCommand):
6+
help = "Sends a scheduled message to Discord"
7+
8+
def add_arguments(self, parser):
9+
parser.add_argument(
10+
"--template",
11+
required=True,
12+
choices=MESSAGE_FACTORIES.keys(),
13+
help="Message template to send",
14+
)
15+
16+
def handle(self, *args, **options):
17+
message_template = options["template"]
18+
19+
factory = MESSAGE_FACTORIES[message_template]
20+
message = factory()
21+
message.save()
22+
23+
self.stdout.write(
24+
self.style.SUCCESS(
25+
f"Scheduled '{message_template}' message for channel {message.channel_name}"
26+
)
27+
)

intbot/intbot/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ def get(name) -> str:
178178
DISCORD_GRANTS_CHANNEL_ID = get("DISCORD_GRANTS_CHANNEL_ID")
179179
DISCORD_GRANTS_CHANNEL_NAME = get("DISCORD_GRANTS_CHANNEL_NAME")
180180

181+
DISCORD_STANDUP_CHANNEL_ID = get("DISCORD_STANDUP_CHANNEL_ID")
182+
DISCORD_STANDUP_CHANNEL_NAME = get("DISCORD_STANDUP_CHANNEL_NAME")
183+
184+
# Discord Roles
185+
DISCORD_BOARD_MEMBER_ROLE_ID = get("DISCORD_BOARD_MEMBER_ROLE_ID")
186+
181187
# Github
182188
GITHUB_API_TOKEN = get("GITHUB_API_TOKEN")
183189
GITHUB_WEBHOOK_SECRET_TOKEN = get("GITHUB_WEBHOOK_SECRET_TOKEN")

0 commit comments

Comments
 (0)