"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/salt/utils/slack.py" (18 Nov 2020, 3446 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "slack.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3002.1_vs_3002.2.

    1 """
    2 Library for interacting with Slack API
    3 
    4 .. versionadded:: 2016.3.0
    5 
    6 :configuration: This module can be used by specifying the name of a
    7     configuration profile in the minion config, minion pillar, or master
    8     config.
    9 
   10     For example:
   11 
   12     .. code-block:: yaml
   13 
   14         slack:
   15           api_key: peWcBiMOS9HrZG15peWcBiMOS9HrZG15
   16 """
   17 
   18 import logging
   19 
   20 import salt.ext.six.moves.http_client
   21 
   22 # pylint: enable=import-error,no-name-in-module
   23 import salt.utils.http
   24 
   25 # pylint: disable=import-error,no-name-in-module,redefined-builtin
   26 from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
   27 from salt.version import __version__
   28 
   29 log = logging.getLogger(__name__)
   30 
   31 
   32 def query(
   33     function,
   34     api_key=None,
   35     args=None,
   36     method="GET",
   37     header_dict=None,
   38     data=None,
   39     opts=None,
   40 ):
   41     """
   42     Slack object method function to construct and execute on the API URL.
   43 
   44     :param api_key:     The Slack api key.
   45     :param function:    The Slack api function to perform.
   46     :param method:      The HTTP method, e.g. GET or POST.
   47     :param data:        The data to be sent for POST method.
   48     :return:            The json response from the API call or False.
   49     """
   50 
   51     ret = {"message": "", "res": True}
   52 
   53     slack_functions = {
   54         "rooms": {"request": "channels.list", "response": "channels"},
   55         "users": {"request": "users.list", "response": "members"},
   56         "message": {"request": "chat.postMessage", "response": "channel"},
   57     }
   58 
   59     if not api_key:
   60         api_key = __salt__["config.get"]("slack.api_key") or __salt__["config.get"](
   61             "slack:api_key"
   62         )
   63 
   64         if not api_key:
   65             log.error("No Slack api key found.")
   66             ret["message"] = "No Slack api key found."
   67             ret["res"] = False
   68             return ret
   69 
   70     api_url = "https://slack.com"
   71     base_url = _urljoin(api_url, "/api/")
   72     path = slack_functions.get(function).get("request")
   73     url = _urljoin(base_url, path, False)
   74 
   75     if not isinstance(args, dict):
   76         query_params = {}
   77     else:
   78         query_params = args.copy()
   79     query_params["token"] = api_key
   80 
   81     if header_dict is None:
   82         header_dict = {}
   83 
   84     if method != "POST":
   85         header_dict["Accept"] = "application/json"
   86 
   87     result = salt.utils.http.query(
   88         url,
   89         method,
   90         params=query_params,
   91         data=data,
   92         decode=True,
   93         status=True,
   94         header_dict=header_dict,
   95         opts=opts,
   96     )
   97 
   98     if result.get("status", None) == salt.ext.six.moves.http_client.OK:
   99         _result = result["dict"]
  100         response = slack_functions.get(function).get("response")
  101         if "error" in _result:
  102             ret["message"] = _result["error"]
  103             ret["res"] = False
  104             return ret
  105         ret["message"] = _result.get(response)
  106         return ret
  107     elif result.get("status", None) == salt.ext.six.moves.http_client.NO_CONTENT:
  108         return True
  109     else:
  110         log.debug(url)
  111         log.debug(query_params)
  112         log.debug(data)
  113         log.debug(result)
  114         if "dict" in result:
  115             _result = result["dict"]
  116             if "error" in _result:
  117                 ret["message"] = result["error"]
  118                 ret["res"] = False
  119                 return ret
  120             ret["message"] = "Unknown response"
  121             ret["res"] = False
  122         else:
  123             ret["message"] = "invalid_auth"
  124             ret["res"] = False
  125         return ret