Skip to content

Commit

Permalink
Add slack support for pulling tech support messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ghickman committed Nov 3, 2023
1 parent 4708350 commit fcd43aa
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions metrics/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .github.cli import github
from .logs import setup_logging
from .slack.cli import slack


@click.group()
Expand All @@ -16,3 +17,4 @@ def cli(ctx, debug):


cli.add_command(github)
cli.add_command(slack)
Empty file added metrics/slack/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions metrics/slack/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from datetime import datetime, time, timedelta

from slack_bolt import App


bennet_bot_id = "B03UJ58MALV"


def get_app(signing_secret, token):
return App(token=token, signing_secret=signing_secret)


def iter_messages(app, channel_id, date=None):
start = end = 0
if date:
start = datetime.combine(date, time()).timestamp()
end = (datetime.combine(date, time()) + timedelta(days=1)).timestamp()

for page in app.client.conversations_history(
channel=channel_id,
include_all_metadata=True,
latest=end,
oldest=start,
):
for message in page["messages"]:
if "bot_id" in message and message["bot_id"] == bennet_bot_id:
yield message
48 changes: 48 additions & 0 deletions metrics/slack/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import itertools
from datetime import datetime

import click

from .. import influxdb
from .api import get_app, iter_messages


writer = influxdb.write


@click.group()
@click.option("--signing-secret", required=True, envvar="SLACK_SIGNING_SECRET")
@click.option("--token", required=True, envvar="SLACK_TOKEN")
@click.pass_context
def slack(ctx, signing_secret, token):
ctx.ensure_object(dict)

ctx.obj["SLACK_SIGNING_SECRET"] = signing_secret
ctx.obj["SLACK_TOKEN"] = token


@slack.command()
@click.argument("date", type=click.DateTime(), required=False)
@click.option(
"--tech-support-channel-id", required=True, envvar="SLACK_TECH_SUPPORT_CHANNEL_ID"
)
@click.option("--backfill", is_flag=True)
@click.pass_context
def tech_support(ctx, date, tech_support_channel_id, backfill):
if backfill and date:
raise click.BadParameter("--backfill cannot be used with a date")

day = None if backfill else date.date()

app = get_app(ctx.obj["SLACK_SIGNING_SECRET"], ctx.obj["SLACK_TOKEN"])

messages = iter_messages(app, tech_support_channel_id, date=day)

for date, messages in itertools.groupby(
messages, lambda m: datetime.fromtimestamp(float(m["ts"])).date()
):
writer(
"slack_tech_support_requests",
date,
len(list(messages)),
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies = [
"influxdb-client",
"requests",
"psycopg[binary]",
"slack-bolt",
"structlog",
]
dynamic = ["version"]
Expand Down
8 changes: 8 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ six==1.16.0 \
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926 \
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254
# via python-dateutil
slack-bolt==1.18.0 \
--hash=sha256:43b121acf78440303ce5129e53be36bdfe5d926a193daef7daf2860688e65dd3 \
--hash=sha256:63089a401ae3900c37698890249acd008a4651d06e86194edc7b72a00819bbac
# via metrics (pyproject.toml)
slack-sdk==3.23.0 \
--hash=sha256:2a8513505cced20ceee22b5b49c11d9545caa6234b56bf0ad47133ea5b357d10 \
--hash=sha256:9d6ebc4ff74e7983e1b27dbdb0f2bb6fc3c2a2451694686eaa2be23bbb085a73
# via slack-bolt
sqlite-fts4==1.0.3 \
--hash=sha256:0359edd8dea6fd73c848989e1e2b1f31a50fe5f9d7272299ff0e8dbaa62d035f \
--hash=sha256:78b05eeaf6680e9dbed8986bde011e9c086a06cb0c931b3cf7da94c214e8930c
Expand Down

0 comments on commit fcd43aa

Please sign in to comment.