-
Notifications
You must be signed in to change notification settings - Fork 834
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add message metadata support (#1207)
Co-authored-by: Fil Maj <[email protected]>
- Loading branch information
Showing
7 changed files
with
246 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import logging | ||
import os | ||
import time | ||
import unittest | ||
|
||
from integration_tests.env_variable_names import SLACK_SDK_TEST_BOT_TOKEN | ||
from slack_sdk.models.metadata import Metadata | ||
from slack_sdk.web import WebClient | ||
|
||
|
||
class TestWebClient(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.logger = logging.getLogger(__name__) | ||
self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_publishing_message_metadata(self): | ||
client: WebClient = WebClient(token=self.bot_token) | ||
new_message = client.chat_postMessage( | ||
channel='#random', | ||
text="message with metadata", | ||
metadata={ | ||
"event_type": "procurement-task", | ||
"event_payload": { | ||
"id": "11111", | ||
"amount": 5000, | ||
"tags": ["foo", "bar", "baz"] | ||
}, | ||
} | ||
) | ||
self.assertIsNone(new_message.get("error")) | ||
self.assertIsNotNone(new_message.get("message").get("metadata")) | ||
|
||
history = client.conversations_history( | ||
channel=new_message.get("channel"), | ||
limit=1, | ||
include_all_metadata=True, | ||
) | ||
self.assertIsNone(history.get("error")) | ||
self.assertIsNotNone(history.get("messages")[0].get("metadata")) | ||
|
||
modification = client.chat_update( | ||
channel=new_message.get("channel"), | ||
ts=new_message.get("ts"), | ||
text="message with metadata (modified)", | ||
metadata={ | ||
"event_type": "procurement-task", | ||
"event_payload": { | ||
"id": "11111", | ||
"amount": 6000, | ||
}, | ||
} | ||
) | ||
self.assertIsNone(modification.get("error")) | ||
self.assertIsNotNone(modification.get("message").get("metadata")) | ||
|
||
scheduled = client.chat_scheduleMessage( | ||
channel=new_message.get("channel"), | ||
post_at=int(time.time()) + 30, | ||
text="message with metadata (scheduled)", | ||
metadata={ | ||
"event_type": "procurement-task", | ||
"event_payload": { | ||
"id": "11111", | ||
"amount": 10, | ||
}, | ||
} | ||
) | ||
self.assertIsNone(scheduled.get("error")) | ||
self.assertIsNotNone(scheduled.get("message").get("metadata")) | ||
|
||
def test_publishing_message_metadata_using_models(self): | ||
client: WebClient = WebClient(token=self.bot_token) | ||
new_message = client.chat_postMessage( | ||
channel='#random', | ||
text="message with metadata", | ||
metadata=Metadata( | ||
event_type="procurement-task", | ||
event_payload={ | ||
"id": "11111", | ||
"amount": 5000, | ||
"tags": ["foo", "bar", "baz"] | ||
} | ||
) | ||
) | ||
self.assertIsNone(new_message.get("error")) | ||
self.assertIsNotNone(new_message.get("message").get("metadata")) | ||
|
||
history = client.conversations_history( | ||
channel=new_message.get("channel"), | ||
limit=1, | ||
include_all_metadata=True, | ||
) | ||
self.assertIsNone(history.get("error")) | ||
self.assertIsNotNone(history.get("messages")[0].get("metadata")) | ||
|
||
modification = client.chat_update( | ||
channel=new_message.get("channel"), | ||
ts=new_message.get("ts"), | ||
text="message with metadata (modified)", | ||
metadata=Metadata( | ||
event_type="procurement-task", | ||
event_payload={ | ||
"id": "11111", | ||
"amount": 6000, | ||
} | ||
) | ||
) | ||
self.assertIsNone(modification.get("error")) | ||
self.assertIsNotNone(modification.get("message").get("metadata")) | ||
|
||
scheduled = client.chat_scheduleMessage( | ||
channel=new_message.get("channel"), | ||
post_at=int(time.time()) + 30, | ||
text="message with metadata (scheduled)", | ||
metadata=Metadata( | ||
event_type="procurement-task", | ||
event_payload={ | ||
"id": "11111", | ||
"amount": 10, | ||
} | ||
) | ||
) | ||
self.assertIsNone(scheduled.get("error")) | ||
self.assertIsNotNone(scheduled.get("message").get("metadata")) |
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,30 @@ | ||
from typing import Dict, Any | ||
from slack_sdk.models.basic_objects import JsonObject | ||
|
||
|
||
class Metadata(JsonObject): | ||
"""Message metadata | ||
https://api.slack.com/metadata | ||
""" | ||
|
||
attributes = { | ||
"event_type", | ||
"event_payload", | ||
} | ||
|
||
def __init__( | ||
self, | ||
event_type: str, | ||
event_payload: Dict[str, Any], | ||
**kwargs, | ||
): | ||
self.event_type = event_type | ||
self.event_payload = event_payload | ||
self.additional_attributes = kwargs | ||
|
||
def __str__(self): | ||
return str(self.get_non_null_attributes()) | ||
|
||
def __repr__(self): | ||
return self.__str__() |
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
Oops, something went wrong.