Skip to content

Commit

Permalink
feat: Slack sink
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jun 24, 2021
1 parent ae75f3b commit 0737466
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 0.1.16 - 2021-MM-DD
===========================

- New sink: send messages to Slack channel

Version 0.1.15 - 2021-06-03
===========================

Expand Down
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ install_requires =
tuyapy==0.1.4
twilio>=6.58.0,<7
jinja2>=2.11.3,<3
slack_sdk>=3.7.0,<4


[options.packages.find]
Expand Down Expand Up @@ -126,6 +127,7 @@ senor_octopus.plugins =
sink.log = senor_octopus.sinks.log:log
sink.mqtt = senor_octopus.sinks.mqtt:mqtt
sink.pushover = senor_octopus.sinks.pushover:pushover
sink.slack = senor_octopus.sinks.slack:slack
sink.sms = senor_octopus.sinks.sms:sms
sink.tuya = senor_octopus.sinks.tuya:tuya
sink.db.postgresql = senor_octopus.sinks.db.postgresql:postgresql
Expand Down
32 changes: 32 additions & 0 deletions src/senor_octopus/sinks/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import logging

from senor_octopus.types import Stream
from slack_sdk import WebClient

_logger = logging.getLogger(__name__)


async def slack(
stream: Stream,
token: str,
channel: str,
) -> None:
"""
Send events as messages to a Slack channel.
This sink can be used to send events to a Slack channel.
The value of the event is sent as the message; its name
is ignored.
Parameters
----------
stream
The incoming stream of events
token
The authentication token for the bot
channel
The Slack channel ID
"""
client = WebClient(token=token)
async for event in stream: # pragma: no cover
client.chat_postMessage(channel=channel, text=str(event["value"]))
20 changes: 20 additions & 0 deletions tests/sinks/test_slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import random

import pytest
from senor_octopus.sinks.slack import slack
from senor_octopus.sources.rand import rand


@pytest.mark.asyncio
async def test_slack(mocker) -> None:
WebClient = mocker.patch("senor_octopus.sinks.slack.WebClient")
client = WebClient.return_value

random.seed(42)

await slack(rand(1), "XXX", "C0123456789")

client.chat_postMessage.assert_called_with(
channel="C0123456789",
text="0.6394267984578837",
)
2 changes: 1 addition & 1 deletion tests/sinks/test_sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@pytest.mark.asyncio
async def test_pushover(mocker) -> None:
async def test_sms(mocker) -> None:
Client = mocker.patch("senor_octopus.sinks.sms.Client")
client = Client.return_value

Expand Down

0 comments on commit 0737466

Please sign in to comment.