generated from opensafely-core/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slack support for pulling tech support messages
- Loading branch information
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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
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 |
This file contains 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
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)), | ||
) |
This file contains 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
This file contains 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