Per-article summarization and preference matching

This commit is contained in:
Adrian Rumpold
2025-07-01 14:16:00 +02:00
parent 77497ed56b
commit 547da4517a
2 changed files with 181 additions and 63 deletions

View File

@@ -1,52 +1,81 @@
import logging
import os
from langchain_core.documents import Document
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
def prepare_message_blocks(stories: list[Document]) -> list:
blocks = []
for story in stories:
block = [
{
"type": "header",
"text": {"type": "plain_text", "text": story.metadata["title"]},
def format_story(story: dict) -> list:
title_text = (
f"<{story['source_url']}|{story['title']}>"
if story["source_url"]
else story["title"]
)
return [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*{title_text}*",
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": f"Categories: {', '.join(story.metadata.get('categories', []))}",
},
],
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": f"Categories: {', '.join(story['categories'])}"
if story["categories"]
else "No categories",
}
],
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": story["summary"],
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": f"Posted on: {story.metadata['created_at']}",
}
],
},
{"type": "section", "text": {"type": "mrkdwn", "text": story.page_content}},
]
},
]
def format_slack_blocks(grouped_stories: dict[str, list[dict]]) -> list[dict]:
"""Format grouped stories into Slack block format."""
blocks = []
# Header block
blocks.append(
{"type": "header", "text": {"type": "plain_text", "text": "🚀 Tech Updates"}}
)
# Add stories for each group
for group_name, stories in grouped_stories.items():
# Group section header
section_title = (
"*Other Stories*" if group_name == "Other" else f"*{group_name}*"
)
blocks.append(
{"type": "section", "text": {"type": "mrkdwn", "text": section_title}}
)
for story in stories:
blocks.extend(format_story(story))
# Add divider after each group (except the last one)
blocks.append({"type": "divider"})
blocks.append(block)
return blocks
def send_message(channel: str, text: str) -> None:
def send_message(channel: str, blocks: list) -> None:
client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])
try:
response = client.chat_postMessage(
channel=channel,
username="HN Ragandy",
text=text,
text="Tech updates",
blocks=blocks,
unfurl_links=False,
)
response.validate()