Skip to content

Commit

Permalink
Add DM bot for Trending posts, if user allows messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
richardr1126 committed Dec 4, 2024
1 parent e36be2f commit 86c98d3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
5 changes: 1 addition & 4 deletions firehose/data_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import defaultdict
from atproto import models, Client, IdResolver
from utils.logger import logger
from firehose.database import db, Post
from firehose.database import db, Post, init_client

PHRASES = [
'17th shard',
Expand Down Expand Up @@ -141,9 +141,6 @@
'mormon',
]

# Compile regex patterns

# Helper function to escape and join phrases/tokens
def compile_pattern(items, word_boundary=True, optional_prefix=None, plural=False):
escaped = [re.escape(item) for item in items]
if plural:
Expand Down
34 changes: 33 additions & 1 deletion firehose/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from apscheduler.triggers.date import DateTrigger
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.events import EVENT_JOB_EXECUTED, EVENT_JOB_ERROR
from atproto import Client, IdResolver, SessionEvent, Session, exceptions
from atproto import Client, IdResolver, SessionEvent, Session, exceptions, models

# Configure logging
logging.basicConfig(level=logging.INFO)
Expand Down Expand Up @@ -152,6 +152,38 @@ def hydrate_posts_with_interactions(client: Client, batch_size: int = 25):
# Round the hot_score to an integer for consistency
hot_score = int(hot_score)

if hot_score > 400:
try:
# create client proxied to Bluesky Chat service
dm_client = client.with_bsky_chat_proxy()
# create shortcut to convo methods
dm = dm_client.chat.bsky.convo
# resolve DID
chat_to = fetched_post.author.did
# create or get conversation with chat_to
convo = dm.get_convo_for_members(
models.ChatBskyConvoGetConvoForMembers.Params(members=[chat_to]),
).convo
# print convo info
logger.info(f'\nConvo ID: {convo.id}')
logger.info('Convo members:')
for member in convo.members:
logger.info(f'- {member.display_name} ({member.did})')

# send a message to the conversation
dm.send_message(
models.ChatBskyConvoSendMessage.Data(
convo_id=convo.id,
message=models.ChatBskyConvoDefs.MessageInput(
text='Your post is trending highly on the Cosmere Feed! 🚀\nhttps://bsky.app/profile/richardroberson.dev/feed/cosmere',
),
)
)
logger.info('\nMessage sent!')
except exceptions.BadRequestError as e:
logger.warning(f'Cannot dm: {chat_to} - {e.response.content.message}')


# Fetch the current interaction score from the database
current_post = Post.get_or_none(Post.uri == uri)
if current_post and current_post.interactions != hot_score:
Expand Down
2 changes: 1 addition & 1 deletion publish_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# (Optional) A description of your feed
# Ex: Top trending content from the whole network
DESCRIPTION: str = "Realtime gateway into discussion being had about the Cosmere by Brandon Sanderson. #Stormlight #Mistborn\n\nCosmere feed is a mix of chronological and trending cosmere posts. Be careful for spoilers!\n\nCode @ https://github.com/richardr1126/cosmere-feed-bsky?tab=readme-ov-file#features"
DESCRIPTION: str = "Realtime gateway into discussion being had about the Cosmere by Brandon Sanderson. #Stormlight #Mistborn\n\nCosmere feed is a mix of chronological and trending cosmere posts. Be careful for spoilers!\n\nKeywords + Code @ https://github.com/richardr1126/cosmere-feed-bsky?tab=readme-ov-file#filters"

# (Optional) The path to an image to be used as your feed's avatar
# Ex: ./path/to/avatar.jpeg
Expand Down

0 comments on commit 86c98d3

Please sign in to comment.