Skip to content

Commit 08c9497

Browse files
Keywords for message format
1 parent a5f349f commit 08c9497

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ In order to run the example code, you need to provide at least one valid target
2323
|``Send Apprise Message``|Sends a push message through Apprise|
2424
|``Set Clients`` and ``Set Attachments``|Sets a new value list and replace the previous values|
2525
|``Add Client`` and ``Add Attachment``|Adds a value to an existing list|
26-
|``Remove Client`` and ``Remove Attachment``|Removes a value from an existing list (if present)|
26+
|``Remove Client`` and ``Remove Attachment``|Removes a value from an existing list (if present). Trying to remove a non-existing entry will NOT result in an error|
2727
|``Clear All Clients`` and ``Clear All Attachments``|Completely removes the current values from the respective list|
2828
|``Set Delimiter``|Optional reconfiguration of this Robot Framework library's delimiter. See details below|
29+
|``Set Notify Type``|Sets one of Apprise's [supported notify types](https://github.com/caronc/apprise/wiki/Development_API#message-types-and-themes). Valid values are ``info``,``success``,``warning``, and ``failure``. Default notify type is ``info``|
30+
|``Set Body Format``|Sets one of Apprise's [supported body formats](https://github.com/caronc/apprise/wiki/Development_API#notify--send-notifications). Valid values are ``html``,``text``, and ``markdown``. Default body format is ``html``|
2931

3032

31-
All ``clients`` and ``attachments`` options can be passed as a ``List`` type variable or as a string. If you use a string, the default delimiter is a comma ``,``. In case you need to use a different delimiter, use the ``Set Delimiter`` keyword.
33+
All ``clients`` and ``attachments`` options can be passed as a ``List`` type variable or as a ``string``. If you use a ``string``, the default delimiter is a comma ``,``. In case you need to use a different delimiter for your string, use the ``Set Delimiter`` keyword.
34+
35+
All ``Set ...`` keywords provide corresponding ``Get ...`` keywords.
3236

3337
``Attachments`` are purely optional. Providing at least one ``Client`` is mandatory, though. Both ``Attachments`` and ``Clients`` can either be provided as a ``List`` item or as a separated string.
3438

@@ -49,4 +53,4 @@ Examples:
4953
## Known issues
5054

5155
- This library uses Apprise's default async behavior. Currently, you cannot send messages in a synchronous way.
52-
- The current version of this library does not permit you to set the target format (text, HTML). It uses Apprise's default format which is HTML.
56+
- The current version of this library does not support Apprise's whole feature set. Options such as tagging are not implemented.

src/AppriseLibrary.py

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class AppriseNotificationType(Enum):
4545
failure = apprise.NotifyType.FAILURE
4646

4747

48+
# Enum which contains Apprise's supported message body formats
49+
class AppriseBodyFormat(Enum):
50+
html = apprise.NotifyFormat.HTML
51+
text = apprise.NotifyFormat.TEXT
52+
markdown = apprise.NotifyFormat.MARKDOWN
53+
54+
4855
@library(scope="GLOBAL", auto_keywords=True)
4956
class AppriseLibrary:
5057

@@ -56,6 +63,7 @@ class AppriseLibrary:
5663
DEFAULT_DELIMITER = ","
5764
DEFAULT_CONFIG_FILE = ""
5865
DEFAULT_NOTIFY_TYPE = apprise.NotifyType.INFO
66+
DEFAULT_BODY_FORMAT = apprise.NotifyFormat.HTML
5967

6068
# Class-internal Apprise parameters
6169
__title = None
@@ -65,6 +73,7 @@ class AppriseLibrary:
6573
__delimiter = None
6674
__config_file = None
6775
__notify_type = None
76+
__body_format = None
6877

6978
# __instance represents the Apprise core object
7079
__apprise_instance = None
@@ -78,6 +87,7 @@ def __init__(
7887
attachments: list = DEFAULT_ATTACHMENTS,
7988
delimiter: str = DEFAULT_DELIMITER,
8089
notify_type: str = DEFAULT_NOTIFY_TYPE,
90+
body_format: str = DEFAULT_BODY_FORMAT,
8191
):
8292
self.__config_file = config_file
8393
self.__title = title
@@ -89,6 +99,7 @@ def __init__(
8999
attachments=attachments
90100
)
91101
self.__notify_type = self.__transform_notify_type(notify_type=notify_type)
102+
self.__body_format = self.__transform_body_format(body_format=body_format)
92103

93104
def __transform_apprise_clients(self, clients: object):
94105
# we will either accept a list item or a string
@@ -127,6 +138,19 @@ def __transform_notify_type(self, notify_type: object):
127138
# enum exists, let's get the value
128139
return AppriseNotificationType[__nt].value
129140

141+
def __transform_body_format(self, body_format: object):
142+
143+
if not body_format:
144+
raise ValueError("No value for 'body_format' has been specified")
145+
146+
# Convert to lower case and check if it exists in our enum
147+
__bf = body_format.lower()
148+
if __bf not in AppriseBodyFormat.__members__:
149+
raise ValueError("Unsupported value for 'body_format' has been specified")
150+
151+
# enum exists, let's get the value
152+
return AppriseBodyFormat[__bf].value
153+
130154
# Python "Getter" methods
131155
#
132156
# Note that adding an additional Robot decorator (@keyword) will not
@@ -164,6 +188,10 @@ def apprise_instance(self):
164188
def notify_type(self):
165189
return self.__notify_type
166190

191+
@property
192+
def body_format(self):
193+
return self.__body_format
194+
167195
# Python "Setter" methods
168196
#
169197
# Note that adding an additional Robot decorator (@keyword) will not
@@ -218,6 +246,10 @@ def apprise_instance(self, apprise_instance: object):
218246
def notify_type(self, notify_type: str):
219247
self.__notify_type = self.__transform_notify_type(notify_type=notify_type)
220248

249+
@body_format.setter
250+
def body_format(self, body_format: str):
251+
self.__body_format = self.__transform_body_format(body_format=body_format)
252+
221253
#
222254
# Robot-specific "getter" keywords
223255
#
@@ -249,6 +281,10 @@ def get_attachments(self):
249281
def get_notify_type(self):
250282
return self.notify_type
251283

284+
@keyword("Get Body Format")
285+
def get_body_format(self):
286+
return self.body_format
287+
252288
#
253289
# Robot-specific "setter" keywords
254290
#
@@ -274,15 +310,11 @@ def set_delimiter(self, delimiter: str = None):
274310

275311
@keyword("Set Notify Type")
276312
def set_notify_type(self, notify_type: str = None):
313+
self.notify_type = self.__transform_notify_type(notify_type=notify_type)
277314

278-
# Convert to lower case and check if it exists in our enum
279-
__nt = notify_type.lower()
280-
if __nt not in AppriseNotificationType.__members__:
281-
raise ValueError("Invalid value for 'notify_type' has been specified")
282-
283-
# enum exists, let's get the value
284-
logger.debug(msg="Setting 'delimiter' attribute")
285-
self.notify_type = AppriseNotificationType[__nt].value
315+
@keyword("Set Body Format")
316+
def set_body_format(self, body_format: str = None):
317+
self.body_format = self.__transform_body_format(body_format=body_format)
286318

287319
@keyword("Set Clients")
288320
def set_clients(self, clients: object):
@@ -342,10 +374,14 @@ def send_apprise_message(
342374
clients=None,
343375
attachments=None,
344376
config_file=None,
345-
notification_type=None,
377+
notify_type=None,
378+
body_format=None,
346379
):
380+
# This is our Apprise config item in case
381+
# the user has specified a config file
347382
_apprise_config = None
348383

384+
# We don't have an instance yet?
349385
if not self.apprise_instance:
350386
logger.debug(msg="Apprise instance not defined; creating it for the user")
351387
self.apprise_instance = apprise.Apprise()
@@ -377,6 +413,14 @@ def send_apprise_message(
377413
if attachments:
378414
self.attachments = self.__transform_apprise_attachments(attachments)
379415

416+
# If user has submitted a notify type, look it up
417+
if notify_type:
418+
self.notify_type = self.__transform_notify_type(notify_type=notify_type)
419+
420+
# If user has submitted a body format, look it up
421+
if body_format:
422+
self.body_format = self.__transform_body_format(body_format=body_format)
423+
380424
# Check if we have received at least one client
381425
if len(self.clients) < 1 and not apprise_config:
382426
raise ValueError("You need to specify at least one target client")
@@ -411,21 +455,24 @@ def send_apprise_message(
411455
self.body = body if body else self.body
412456

413457
# send the content to Apprise
414-
logger.debug(msg="Sending message")
458+
logger.debug(
459+
msg=f"Sending message with type '{self.notify_type}' and format '{self.body_format}'"
460+
)
415461

416462
result = self.apprise_instance.notify(
417463
title=self.title,
418464
body=self.body,
419465
attach=self.attachments,
420-
notify_type=notification_type,
466+
notify_type=self.notify_type,
467+
body_format=self.body_format,
421468
)
422469
return result
423470

424471

425472
if __name__ == "__main__":
426-
appr = AppriseLibrary(
427-
body="My body", title="My title", clients="SECRET_CLIENT_ID", notify_type="info"
473+
appr = AppriseLibrary(clients="MEIN_SECRET")
474+
appr.send_apprise_message(
475+
body="My Body", title="My Title", notify_type="warning", body_format="markdown"
428476
)
429-
appr.send_apprise_message()
430477

431478
pass

0 commit comments

Comments
 (0)