Skip to content

Commit

Permalink
feat: Support Bluesky (#53)
Browse files Browse the repository at this point in the history
* add bluesky bot

* update actions for bluesky bot secrets

* clean up extra comment tag

* update readme

* support rich text urls
  • Loading branch information
kmackenzieii authored Nov 25, 2024
1 parent 79bc639 commit 413ca97
Show file tree
Hide file tree
Showing 7 changed files with 884 additions and 156 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/dry_run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
MASTODON_CLIENT_KEY: ${{ secrets.MASTODON_CLIENT_KEY }}
MASTODON_CLIENT_SECRET: ${{ secrets.MASTODON_CLIENT_SECRET }}
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
ATP_AUTH_HANDLE: ${{ secrets.ATP_AUTH_HANDLE }}
ATP_AUTH_PASSWORD: ${{ secrets.ATP_AUTH_PASSWORD }}
SLOW_ZONE_BOT_SLACK_WEBHOOK_URL: ${{ secrets.SLOW_ZONE_BOT_SLACK_WEBHOOK_URL }}
steps:
- name: Checkout repo
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
MASTODON_CLIENT_KEY: ${{ secrets.MASTODON_CLIENT_KEY }}
MASTODON_CLIENT_SECRET: ${{ secrets.MASTODON_CLIENT_SECRET }}
MASTODON_ACCESS_TOKEN: ${{ secrets.MASTODON_ACCESS_TOKEN }}
ATP_AUTH_HANDLE: ${{ secrets.ATP_AUTH_HANDLE }}
ATP_AUTH_PASSWORD: ${{ secrets.ATP_AUTH_PASSWORD }}
SLOW_ZONE_BOT_SLACK_WEBHOOK_URL: ${{ secrets.SLOW_ZONE_BOT_SLACK_WEBHOOK_URL }}
steps:
- name: Checkout repo
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![lint](https://github.com/transitmatters/mbta-slow-zone-bot/workflows/lint/badge.svg?branch=main)
![run](https://github.com/transitmatters/mbta-slow-zone-bot/workflows/run/badge.svg?branch=main)

A Twitter & Mastodon & Slack bot to track MBTA slow zones
A Twitter & Mastodon & Slack & Bluesky bot to track MBTA slow zones

https://twitter.com/mbtaslowzonebot

Expand Down
73 changes: 73 additions & 0 deletions domains/bluesky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from atproto import Client
from utils import format_fixed_slow_zone, format_new_slow_zone, format_updated_slow_zone
import re


def parse_urls(text: str) -> list[dict]:
"""
credit: https://raw.githubusercontent.com/bluesky-social/cookbook/refs/heads/main/python-bsky-post/create_bsky_post.py
"""
spans = []
# partial/naive URL regex based on: https://stackoverflow.com/a/3809435
# tweaked to disallow some training punctuation
url_regex = rb"[$|\W](https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(url_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"url": m.group(1).decode("UTF-8"),
}
)
return spans


def parse_facets(text: str) -> list[dict]:
"""
parses post text and returns a list of app.bsky.richtext.facet objects for any mentions (@handle.example.com) or URLs (https://example.com)
indexing must work with UTF-8 encoded bytestring offsets, not regular unicode string offsets, to match Bluesky API expectations
modified from: https://raw.githubusercontent.com/bluesky-social/cookbook/refs/heads/main/python-bsky-post/create_bsky_post.py
"""
facets = []

for u in parse_urls(text):
facets.append(
{
"index": {
"byteStart": u["start"],
"byteEnd": u["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
# NOTE: URI ("I") not URL ("L")
"uri": u["url"],
}
],
}
)
return facets


def send_new_slow_zone_bsky(sz, client: Client):
for line in sz:
for z in line:
output = format_new_slow_zone(z)
client.send_post(text=output, facets=parse_facets(output))


def send_fixed_slow_zone_bsky(sz, client: Client):
for line in sz:
for z in line:
output = format_fixed_slow_zone(z)
client.send_post(text=output, facets=parse_facets(output))


def send_updated_slow_zone_bsky(sz, client: Client):
for line in sz:
for z in line:
output = format_updated_slow_zone(z)
client.send_post(text=output, facets=parse_facets(output))
Loading

0 comments on commit 413ca97

Please sign in to comment.