SELKIELogger  1.0.0
PushoverClient.py
Go to the documentation of this file.
1 # Copyright (C) 2023 Swansea University
2 #
3 # This file is part of the SELKIELogger suite of tools.
4 #
5 # SELKIELogger is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
9 #
10 # SELKIELogger is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this SELKIELogger product.
17 # If not, see <http://www.gnu.org/licenses/>.
18 
19 import http.client, urllib
20 
21 
22 
23 
25  """!A very simple Pushover client.
26  Will attempt to read the default configuration file from pushover-bash script.
27  """
28 
29  def __init__(self, config=None):
30  """! Create PushoverClient instance
31  @param[in] config Configuration file to use instead of default
32  """
33  if config is None:
34  config = "/etc/pushover/pushover-config"
35 
36  self.configconfig = {}
37  with open(config, "r") as cfile:
38  for line in cfile:
39  key, value = line.split("=")
40  key = key.lower().strip()
41  self.configconfig[key] = value.strip()
42 
43  def sendMessage(self, message, title=None):
44  """!Send specified message via Pushover, optionally customising title.
45  @param[in] message Message text
46  @param[in] title If set, override message title from configuration file
47  @returns None
48  """
49  if title is None:
50  title = self.configconfig.get("title", "SELKIELogger")
51 
52  conn = http.client.HTTPSConnection("api.pushover.net:443")
53  conn.request(
54  "POST",
55  "/1/messages.json",
56  urllib.parse.urlencode(
57  {
58  "token": self.configconfig["api_token"],
59  "user": self.configconfig["user_key"],
60  "message": message,
61  "title": title,
62  }
63  ),
64  {"Content-type": "application/x-www-form-urlencoded"},
65  )
66  conn.getresponse()
67 
68 
69 if __name__ == "__main__":
70  PushoverClient().sendMessage("Test message")
A very simple Pushover client.
def __init__(self, config=None)
Create PushoverClient instance.
def sendMessage(self, message, title=None)
Send specified message via Pushover, optionally customising title.