Source code for saltext.pushover.modules.pushover_notify
"""Send notifications via `Pushover <https://www.pushover.net>`_... important:: See :ref:`Configuration <pushover-setup>` for a description of available configuration parameters."""importloggingimporturllib.parsefromsalt.exceptionsimportSaltInvocationErrorimportsaltext.pushover.utils.pushoverlog=logging.getLogger(__name__)__virtualname__="pushover"def__virtual__():return__virtualname__
[docs]defpost_message(user=None,device=None,message=None,title=None,priority=0,expire=None,retry=None,sound=None,api_version=1,# pylint: disable=unused-argumenttoken=None,):""" Send a message to a Pushover user or group. CLI Example: .. code-block:: bash salt '*' pushover.post_message user='uQiRzpo4DXghDmr9QzzfQu27cmVRsG' title='Message from Salt' message='Build is done' salt '*' pushover.post_message user='uQiRzpo4DXghDmr9QzzfQu27cmVRsG' title='Message from Salt' message='Build is done' priority='2' expire='720' retry='5' user The user or group of users to send the message to. Must be a user/group ID (key), not a name or an email address. Required if not specified in the configuration. device The name of the device to send the message to. message The message to send to the Pushover user or group. Required. title The message title to use. Defaults to ``Message from SaltStack``. priority The priority of the message (integers between ``-2`` and ``2``). Defaults to ``0``. .. note:: Emergency priority (``2``) requires ``expire`` and ``retry`` parameters to be set. expire Stop notifying the user after the specified amount of seconds. The message is still shown after expiry. retry Repeat the notification after this amount of seconds. Minimum: ``30``. sound The `notification sound <https://pushover.net/api#sounds>`_ to play. token The authentication token to use for the Pushover API. Required if not specified in the configuration. """ifnottoken:token=__salt__["config.get"]("pushover.token")or__salt__["config.get"]("pushover:token")ifnottoken:raiseSaltInvocationError("Pushover token is unavailable.")ifnotuser:user=__salt__["config.get"]("pushover.user")or__salt__["config.get"]("pushover:user")ifnotuser:raiseSaltInvocationError("Pushover user key is unavailable.")ifnotmessage:raiseSaltInvocationError('Required parameter "message" is missing.')ifprioritynotinrange(-2,3):raiseSaltInvocationError(f"Invalid priority {priority}. Needs to be an integer between -2 and 2 (inclusive)")ifpriority==2andnot(expireandretry):raiseSaltInvocationError("Emergency messages require `expire` and `retry` parameters to be set")ifretryandretry<30:raiseSaltInvocationError("`retry` needs to be at least 30 (seconds)")user_validate=saltext.pushover.utils.pushover.validate_user(user,device,token)ifnotuser_validate["result"]:returnuser_validateifnottitle:title="Message from SaltStack"parameters={}parameters["user"]=userifdeviceisnotNone:parameters["device"]=deviceparameters["token"]=tokenparameters["title"]=titleparameters["priority"]=priorityifexpireisnotNone:parameters["expire"]=expireifretryisnotNone:parameters["retry"]=retryparameters["message"]=messageifsoundandsaltext.pushover.utils.pushover.validate_sound(sound,token)["res"]:parameters["sound"]=soundresult=saltext.pushover.utils.pushover.query(function="message",method="POST",header_dict={"Content-Type":"application/x-www-form-urlencoded"},data=urllib.parse.urlencode(parameters),opts=__opts__,)ifresult["res"]:returnTruereturnresult