-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding slack integration and fixing private variable issue * updating Readme about new information
- Loading branch information
1 parent
3d946fb
commit 7a0f6ab
Showing
17 changed files
with
231 additions
and
142 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import logging | ||
import os | ||
import sys | ||
from datetime import datetime, timedelta | ||
|
||
import pytz | ||
|
||
from obsei.analyzer.dummy_analyzer import DummyAnalyzer, DummyAnalyzerConfig | ||
from obsei.misc.utils import DATETIME_STRING_PATTERN | ||
from obsei.processor import Processor | ||
from obsei.sink.slack_sink import SlackSink, SlackSinkConfig | ||
from obsei.source.reddit_source import RedditConfig, RedditSource | ||
from obsei.workflow.store import WorkflowStore | ||
from obsei.workflow.workflow import Workflow, WorkflowConfig | ||
|
||
|
||
def print_state(id: str): | ||
logger.info(f'Source State: {source.store.get_source_state(id)}') | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(stream=sys.stdout, level=logging.INFO) | ||
|
||
since_time = datetime.utcnow().astimezone(pytz.utc) + timedelta(hours=-1) | ||
|
||
workflow_store = WorkflowStore() | ||
|
||
source_config = RedditConfig( | ||
subreddits=["wallstreetbets"], | ||
lookup_period=since_time.strftime(DATETIME_STRING_PATTERN) | ||
) | ||
|
||
source = RedditSource(store=workflow_store) | ||
|
||
sink_config = SlackSinkConfig( | ||
slack_token=os.environ['SLACK_TOKEN'], | ||
channel_id="C01LRS6CT9Q" | ||
) | ||
sink = SlackSink(store=workflow_store) | ||
|
||
analyzer_config = DummyAnalyzerConfig() | ||
analyzer = DummyAnalyzer() | ||
|
||
workflow = Workflow( | ||
config=WorkflowConfig( | ||
source_config=source_config, | ||
sink_config=sink_config, | ||
analyzer_config=analyzer_config | ||
), | ||
) | ||
workflow_store.add_workflow(workflow) | ||
|
||
processor = Processor( | ||
analyzer=analyzer, | ||
sink=sink, | ||
source=source, | ||
analyzer_config=analyzer_config | ||
) | ||
|
||
processor.process(workflow=workflow) | ||
|
||
print_state(workflow.id) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
from typing import Any, List | ||
|
||
from pydantic import Field, PrivateAttr, SecretStr | ||
from slack_sdk import WebClient | ||
|
||
from obsei.sink.base_sink import BaseSink, BaseSinkConfig, Convertor | ||
from obsei.analyzer.base_analyzer import AnalyzerResponse | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class SlackSinkConfig(BaseSinkConfig): | ||
# This is done to avoid exposing member to API response | ||
_slack_client: WebClient = PrivateAttr() | ||
TYPE: str = "Slack" | ||
|
||
slack_token: SecretStr = Field(None, env='SLACK_TOKEN') | ||
channel_id: str | ||
|
||
def __init__(self, **data: Any): | ||
super().__init__(**data) | ||
self._slack_client=WebClient(token=self.slack_token.get_secret_value()) | ||
|
||
def get_slack_client(self): | ||
return self._slack_client | ||
|
||
|
||
class SlackSink(BaseSink): | ||
def __init__(self, **data: Any): | ||
super().__init__(**data) | ||
|
||
def send_data( | ||
self, | ||
analyzer_responses: List[AnalyzerResponse], | ||
config: SlackSinkConfig, | ||
**kwargs | ||
): | ||
responses = [] | ||
payloads = [] | ||
for analyzer_response in analyzer_responses: | ||
payloads.append(self.convertor.convert( | ||
analyzer_response=analyzer_response | ||
)) | ||
|
||
for payload in payloads: | ||
response = config.get_slack_client().chat_postMessage( | ||
channel=config.channel_id, | ||
text=f'```\n{payload["processed_text"]}\n```' | ||
) | ||
logger.info(f"response='{response}'") | ||
responses.append(response) | ||
|
||
return responses |
Oops, something went wrong.