Skip to content

Commit c02e903

Browse files
committed
add SlackNotifier to notifier module
1 parent 0a5d669 commit c02e903

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ SCRAPER_TYPE="mock" # Options: "mock", "httpx", "youtube_transcript"
6969

7070
## Summarizer Settings
7171
SUMMARIZER_TYPE="mock" # Options: "mock", "llm"
72+
73+
## Notifier Settings
74+
NOTIFIER_TYPE="mock" # Options: "mock", "slack"
75+
NOTIFIER_SLACK_WEBHOOK_URL="https://hooks.slack.com/services/xxx"

template_langgraph/agents/news_summarizer_agent/agent.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,12 @@ def summarize_web_content(self, state: SummarizeWebContentState):
114114
def notify(self, state: AgentState) -> AgentState:
115115
"""Send notifications to the user."""
116116
logger.info(f"Sending notifications with state: {state}")
117-
# Simulate sending notifications
118-
# convert list of articles to a dictionary for notification
119117
summary = {}
120118
for i, article in enumerate(state.articles):
121-
summary[i] = article.model_dump()
119+
summary[i] = {
120+
"url": article.url,
121+
"structured_article": article.structured_article.model_dump(),
122+
}
122123
self.notifier.notify(
123124
text=summary.__str__(),
124125
)

template_langgraph/internals/notifiers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from enum import Enum
1212
from functools import lru_cache
1313

14+
import httpx
1415
from pydantic_settings import BaseSettings, SettingsConfigDict
1516

1617
from template_langgraph.loggers import get_logger
@@ -20,10 +21,12 @@
2021

2122
class NotifierType(str, Enum):
2223
MOCK = "mock"
24+
SLACK = "slack"
2325

2426

2527
class Settings(BaseSettings):
2628
notifier_type: NotifierType = NotifierType.MOCK
29+
notifier_slack_webhook_url: str = "https://hooks.slack.com/services/Txxx/Bxxx/xxx"
2730

2831
model_config = SettingsConfigDict(
2932
env_file=".env",
@@ -59,11 +62,30 @@ def notify(self, text: str):
5962
logger.info(f"Mock notify with text: {text}")
6063

6164

65+
class SlackNotifier(BaseNotifier):
66+
"""Slack notifier for sending notifications to a Slack channel."""
67+
68+
def __init__(self, settings=get_notifier_settings()):
69+
self.webhook_url = settings.notifier_slack_webhook_url
70+
71+
def notify(self, text: str):
72+
logger.info(f"Slack notify with text: {text}")
73+
with httpx.Client() as client:
74+
client.post(
75+
self.webhook_url,
76+
json={
77+
"text": text,
78+
},
79+
)
80+
81+
6282
def get_notifier(settings: Settings = None) -> BaseNotifier:
6383
if settings is None:
6484
settings = get_notifier_settings()
6585

6686
if settings.notifier_type == NotifierType.MOCK:
6787
return MockNotifier()
88+
elif settings.notifier_type == NotifierType.SLACK:
89+
return SlackNotifier(settings)
6890
else:
6991
raise ValueError(f"Unknown notifier type: {settings.notifier_type}")

0 commit comments

Comments
 (0)