client/#75 - added functionality to send different message to different channel(s)

This commit is contained in:
Petr Kalis 2021-06-09 18:52:01 +02:00
parent 80833c7b79
commit 069ba7b3af
6 changed files with 70 additions and 43 deletions

View file

@ -30,7 +30,6 @@ class CollectSlackFamilies(pyblish.api.InstancePlugin):
profile = filter_profiles(self.profiles, key_values,
logger=self.log)
self.log.debug("profile ::{}".format(profile))
# make slack publishable
if profile:
if instance.data.get('families'):
@ -38,8 +37,8 @@ class CollectSlackFamilies(pyblish.api.InstancePlugin):
else:
instance.data['families'] = ['slack']
instance.data["slack_channel"] = profile["channel"]
instance.data["slack_message"] = profile["message"]
instance.data["slack_channel_message_profiles"] = \
profile["channel_messages"]
slack_token = (instance.context.data["project_settings"]
["slack"]

View file

@ -23,8 +23,30 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
optional = True
def process(self, instance):
message_templ = instance.data["slack_message"]
published_path = self._get_thumbnail_path(instance)
for message_profile in instance.data["slack_channel_message_profiles"]:
message = self._get_filled_message(message_profile["message"],
instance)
if not message:
return
for channel in message_profile["channels"]:
if six.PY2:
self._python2_call(instance.data["slack_token"],
channel,
message,
published_path,
message_profile["upload_thumbnail"])
else:
self._python3_call(instance.data["slack_token"],
channel,
message,
published_path,
message_profile["upload_thumbnail"])
def _get_filled_message(self, message_templ, instance):
"""Use message_templ and data from instance to get message content."""
fill_data = copy.deepcopy(instance.context.data["anatomyData"])
fill_pairs = (
@ -40,27 +62,15 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
multiple_case_variants = prepare_template_data(fill_pairs)
fill_data.update(multiple_case_variants)
message = None
try:
message = message_templ.format(**fill_data)
except Exception:
self.log.warning(
"Some keys are missing in {}".format(message_templ),
exc_info=True)
return
published_path = self._get_thumbnail_path(instance)
for channel in instance.data["slack_channel"]:
if six.PY2:
self._python2_call(instance.data["slack_token"],
channel,
message,
published_path)
else:
self._python3_call(instance.data["slack_token"],
channel,
message,
published_path)
return message
def _get_thumbnail_path(self, instance):
"""Returns abs url for thumbnail if present in instance repres"""
@ -79,11 +89,13 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
break
return published_path
def _python2_call(self, token, channel, message, published_path):
def _python2_call(self, token, channel, message,
published_path, upload_thumbnail):
from slackclient import SlackClient
try:
client = SlackClient(token)
if published_path and os.path.exists(published_path):
if upload_thumbnail and \
published_path and os.path.exists(published_path):
with open(published_path, 'rb') as pf:
response = client.api_call(
"files.upload",
@ -108,12 +120,14 @@ class IntegrateSlackAPI(pyblish.api.InstancePlugin):
error_str = self._enrich_error(str(e), channel)
self.log.warning("Error happened: {}".format(error_str))
def _python3_call(self, token, channel, message, published_path):
def _python3_call(self, token, channel, message,
published_path, upload_thumbnail):
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
try:
client = WebClient(token=token)
if published_path and os.path.exists(published_path):
if upload_thumbnail and \
published_path and os.path.exists(published_path):
_ = client.files_upload(
channels=channel,
initial_comment=message,

View file

@ -1,20 +1,15 @@
{
"token": "xoxb-2134660737317-2122995982087-cALVXgj3mpBhdf2nO36cdWok",
"token": "",
"publish": {
"CollectSlackFamilies": {
"enabled": true,
"optional": true,
"profiles": [
{
"families": [
"workfile"
],
"families": [],
"tasks": [],
"hosts": [],
"channel": [
"things"
],
"message": "{Asset} was publishet {subset} with {thumbnail}"
"channel_messages": []
}
]
}

View file

@ -68,16 +68,32 @@
"type": "separator"
},
{
"key": "channel_messages",
"label": "Messages to channels",
"type": "list",
"object_type": "text",
"key": "channel",
"label": "Channel"
},
{
"type": "text",
"multiline": true,
"key": "message",
"label": "Message"
"use_label_wrap": true,
"object_type": {
"type": "dict",
"children": [
{
"type": "list",
"object_type": "text",
"key": "channels",
"label": "Channels"
},
{
"type": "boolean",
"key": "upload_thumbnail",
"label": "Upload thumbnail"
},
{
"type": "text",
"multiline": false,
"key": "message",
"label": "Message"
}
]
}
}
]
}