Created
June 22, 2022 05:07
-
-
Save bbelderbos/af5555cfb1ff05544b4d0464577b5417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, datetime, time | |
import logging | |
import os | |
from slack_sdk import WebClient | |
from slack_sdk.errors import SlackApiError | |
STOIC_CHANNEL = os.environ["STOIC_CHANNEL"] | |
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"] | |
THIS_YEAR = date.today().year | |
DAILY_STOIC_POST_TIME = {"hour": 6, "minute": 0} # UTC = 8 am CET | |
client = WebClient(token=SLACK_BOT_TOKEN) | |
logger = logging.getLogger(__name__) | |
def _get_date_ordinal(day): | |
"""https://stackoverflow.com/a/5891598""" | |
day = int(day) | |
if 4 <= day <= 20 or 24 <= day <= 30: | |
suffix = "th" | |
else: | |
suffix = ["st", "nd", "rd"][day % 10 - 1] | |
return f"{day}{suffix}" | |
def schedule_post(entry): | |
day_key = entry["key"] | |
title = entry["title"] | |
post_day = datetime.strptime(day_key, "%B_%d").replace(year=THIS_YEAR) | |
scheduled_time = time(**DAILY_STOIC_POST_TIME) | |
schedule_timestamp = datetime.combine(post_day, scheduled_time).strftime("%s") | |
month, day = day_key.split("_") | |
day_str = _get_date_ordinal(day) | |
text = f"{month} {day_str}: {title.capitalize()} 🧵" | |
try: | |
result = client.chat_scheduleMessage( | |
channel=STOIC_CHANNEL, text=text, post_at=schedule_timestamp | |
) | |
print("Schdule post result:", result) | |
logger.info(result) | |
except SlackApiError as e: | |
logger.error(f"Error scheduling message: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment