slackpy 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slackpy/__init__.py +3 -0
- slackpy/commandline.py +77 -0
- slackpy/slackpy.py +154 -0
- slackpy-3.0.0.dist-info/METADATA +169 -0
- slackpy-3.0.0.dist-info/RECORD +9 -0
- slackpy-3.0.0.dist-info/WHEEL +5 -0
- slackpy-3.0.0.dist-info/entry_points.txt +2 -0
- slackpy-3.0.0.dist-info/licenses/LICENSE +22 -0
- slackpy-3.0.0.dist-info/top_level.txt +1 -0
slackpy/__init__.py
ADDED
slackpy/commandline.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from slackpy import SlackLogger, LogLv, LOG_LEVELS
|
|
3
|
+
from argparse import ArgumentParser
|
|
4
|
+
|
|
5
|
+
__author__ = 'Takahiro Ikeuchi'
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def main():
|
|
9
|
+
parser = ArgumentParser(description='slackpy command line tool')
|
|
10
|
+
parser.add_argument('-m',
|
|
11
|
+
'--message',
|
|
12
|
+
type=str,
|
|
13
|
+
required=True,
|
|
14
|
+
help='Message body.')
|
|
15
|
+
parser.add_argument('-c',
|
|
16
|
+
'--channel',
|
|
17
|
+
required=False,
|
|
18
|
+
help='Slack Channel. channel must be started with # or @',
|
|
19
|
+
default=None)
|
|
20
|
+
parser.add_argument('-t',
|
|
21
|
+
'--title',
|
|
22
|
+
type=str,
|
|
23
|
+
required=False,
|
|
24
|
+
help='Message title.',
|
|
25
|
+
default='Slack Notification')
|
|
26
|
+
parser.add_argument('-n',
|
|
27
|
+
'--name',
|
|
28
|
+
type=str,
|
|
29
|
+
required=False,
|
|
30
|
+
help='Your bot\'s user name',
|
|
31
|
+
default='Logger')
|
|
32
|
+
|
|
33
|
+
# The purpose of backward compatibility, old args (1, 2, 3)
|
|
34
|
+
# are being retained.
|
|
35
|
+
# DEBUG == 10, INFO == 20, # WARNING == 30, ERROR == 40
|
|
36
|
+
parser.add_argument('-l',
|
|
37
|
+
'--level',
|
|
38
|
+
type=int,
|
|
39
|
+
default=20,
|
|
40
|
+
choices=LOG_LEVELS + [1, 2, 3])
|
|
41
|
+
|
|
42
|
+
args = parser.parse_args()
|
|
43
|
+
args.level = {1: LogLv.INFO, 2: LogLv.WARN, 3: LogLv.ERROR}.get(args.level, args.level)
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
web_hook_url = os.environ["SLACK_INCOMING_WEB_HOOK"]
|
|
47
|
+
|
|
48
|
+
except KeyError:
|
|
49
|
+
print('ERROR: Please set a SLACK_INCOMING_WEB_HOOK variable in ' +
|
|
50
|
+
'your environment.')
|
|
51
|
+
else:
|
|
52
|
+
|
|
53
|
+
client = SlackLogger(web_hook_url, args.channel, args.name)
|
|
54
|
+
|
|
55
|
+
# Command Line mode can use only DEBUG level.
|
|
56
|
+
client.set_log_level(LogLv.DEBUG)
|
|
57
|
+
|
|
58
|
+
if args.level == LogLv.DEBUG:
|
|
59
|
+
response = client.debug(args.message, args.title)
|
|
60
|
+
|
|
61
|
+
elif args.level == LogLv.INFO:
|
|
62
|
+
response = client.info(args.message, args.title)
|
|
63
|
+
|
|
64
|
+
elif args.level == LogLv.WARN:
|
|
65
|
+
response = client.warn(args.message, args.title)
|
|
66
|
+
|
|
67
|
+
elif args.level == LogLv.ERROR:
|
|
68
|
+
response = client.error(args.message, args.title)
|
|
69
|
+
|
|
70
|
+
else:
|
|
71
|
+
raise Exception("'Level' must be selected from among 10 to 40")
|
|
72
|
+
|
|
73
|
+
if response.status_code == 200:
|
|
74
|
+
print(True)
|
|
75
|
+
|
|
76
|
+
else:
|
|
77
|
+
print(False)
|
slackpy/slackpy.py
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import traceback
|
|
3
|
+
import requests
|
|
4
|
+
from enum import IntEnum, unique
|
|
5
|
+
from retrying import retry
|
|
6
|
+
|
|
7
|
+
__author__ = 'Takahiro Ikeuchi'
|
|
8
|
+
|
|
9
|
+
MULTIPLIER = 1000
|
|
10
|
+
MAX_DELAY = 30000
|
|
11
|
+
EXPONENTIAL_MAX = 8000
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@unique
|
|
15
|
+
class LogLv(IntEnum):
|
|
16
|
+
DEBUG = 10
|
|
17
|
+
INFO = 20
|
|
18
|
+
WARN = 30
|
|
19
|
+
ERROR = 40
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
LOG_LEVELS = list(map(int, LogLv))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SlackLogger:
|
|
26
|
+
def __init__(self, web_hook_url, channel=None, username='Logger', icon_url=None):
|
|
27
|
+
|
|
28
|
+
self.web_hook_url = web_hook_url
|
|
29
|
+
self.username = username
|
|
30
|
+
self.icon_url = icon_url
|
|
31
|
+
self.log_level = LogLv.INFO
|
|
32
|
+
|
|
33
|
+
if channel is None:
|
|
34
|
+
self.channel = None
|
|
35
|
+
|
|
36
|
+
elif channel.startswith('#') or channel.startswith('@'):
|
|
37
|
+
self.channel = channel
|
|
38
|
+
|
|
39
|
+
else:
|
|
40
|
+
raise ValueError('channel must be started with "#" or "@".')
|
|
41
|
+
|
|
42
|
+
def set_log_level(self, lv):
|
|
43
|
+
|
|
44
|
+
if lv in LOG_LEVELS:
|
|
45
|
+
self.log_level = lv
|
|
46
|
+
|
|
47
|
+
else:
|
|
48
|
+
raise ValueError('argument lv is invalid. Choose from values in ErrorLv Class.')
|
|
49
|
+
|
|
50
|
+
def __build_payload(self, message, title, title_link, color, fields):
|
|
51
|
+
|
|
52
|
+
__attachments = [{
|
|
53
|
+
'title': title,
|
|
54
|
+
'title_link': title_link,
|
|
55
|
+
'color': color,
|
|
56
|
+
'text': message,
|
|
57
|
+
'fields': fields,
|
|
58
|
+
'mrkdwn_in': ['text', 'fields', 'title']
|
|
59
|
+
}]
|
|
60
|
+
|
|
61
|
+
payload = {
|
|
62
|
+
'channel': self.channel,
|
|
63
|
+
'username': self.username,
|
|
64
|
+
'icon_url': self.icon_url,
|
|
65
|
+
'attachments': __attachments,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return payload
|
|
69
|
+
|
|
70
|
+
@retry(wait_exponential_multiplier=MULTIPLIER, stop_max_delay=MAX_DELAY, wait_exponential_max=EXPONENTIAL_MAX)
|
|
71
|
+
def __post(self, payload):
|
|
72
|
+
try:
|
|
73
|
+
response = requests.post(self.web_hook_url,
|
|
74
|
+
data=json.dumps(payload), timeout=3, allow_redirects=False)
|
|
75
|
+
|
|
76
|
+
except Exception:
|
|
77
|
+
raise Exception(traceback.format_exc())
|
|
78
|
+
|
|
79
|
+
else:
|
|
80
|
+
if response.status_code == 200:
|
|
81
|
+
return response
|
|
82
|
+
|
|
83
|
+
else:
|
|
84
|
+
raise Exception('failed to post a message to Slack.')
|
|
85
|
+
|
|
86
|
+
def __send_notification(self, message, title, title_link='', color='good',
|
|
87
|
+
fields='', log_level=LogLv.INFO):
|
|
88
|
+
"""Send a message to a channel.
|
|
89
|
+
Args:
|
|
90
|
+
title: Message title.
|
|
91
|
+
title_link: Link of the message title.
|
|
92
|
+
message: Message body.
|
|
93
|
+
color: Message line color on Slack. This parameter should be one of the following values: 'good', 'warning',
|
|
94
|
+
'danger' or any hex color code.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
response: Response of Slack API.
|
|
98
|
+
|
|
99
|
+
Raises:
|
|
100
|
+
Exception:
|
|
101
|
+
"""
|
|
102
|
+
if log_level < self.log_level:
|
|
103
|
+
return None
|
|
104
|
+
|
|
105
|
+
payload = self.__build_payload(message, title, title_link, color, fields)
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
response = self.__post(payload)
|
|
109
|
+
|
|
110
|
+
except Exception:
|
|
111
|
+
raise Exception(traceback.format_exc())
|
|
112
|
+
|
|
113
|
+
return response
|
|
114
|
+
|
|
115
|
+
def debug(self, message, title='Slack Notification', title_link='', fields=''):
|
|
116
|
+
return self.__send_notification(message=message,
|
|
117
|
+
title=title,
|
|
118
|
+
title_link=title_link,
|
|
119
|
+
color='#03A9F4',
|
|
120
|
+
fields=fields,
|
|
121
|
+
log_level=LogLv.DEBUG)
|
|
122
|
+
|
|
123
|
+
def info(self, message, title='Slack Notification', title_link='', fields=''):
|
|
124
|
+
return self.__send_notification(message=message,
|
|
125
|
+
title=title,
|
|
126
|
+
title_link=title_link,
|
|
127
|
+
color='good',
|
|
128
|
+
fields=fields,
|
|
129
|
+
log_level=LogLv.INFO)
|
|
130
|
+
|
|
131
|
+
def warn(self, message, title='Slack Notification', title_link='', fields=''):
|
|
132
|
+
return self.__send_notification(message=message,
|
|
133
|
+
title=title,
|
|
134
|
+
title_link=title_link,
|
|
135
|
+
color='warning',
|
|
136
|
+
fields=fields,
|
|
137
|
+
log_level=LogLv.WARN)
|
|
138
|
+
|
|
139
|
+
def error(self, message, title='Slack Notification', title_link='', fields=''):
|
|
140
|
+
return self.__send_notification(message=message,
|
|
141
|
+
title=title,
|
|
142
|
+
title_link=title_link,
|
|
143
|
+
color='danger',
|
|
144
|
+
fields=fields,
|
|
145
|
+
log_level=LogLv.ERROR)
|
|
146
|
+
|
|
147
|
+
def message(self, message, title='Slack Notification', title_link='', color='good', fields='',
|
|
148
|
+
log_level=LogLv.ERROR):
|
|
149
|
+
return self.__send_notification(message=message,
|
|
150
|
+
title=title,
|
|
151
|
+
title_link=title_link,
|
|
152
|
+
color=color,
|
|
153
|
+
fields=fields,
|
|
154
|
+
log_level=log_level)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: slackpy
|
|
3
|
+
Version: 3.0.0
|
|
4
|
+
Summary: A Simple and Human Friendly Slack Client for Logging.
|
|
5
|
+
Author-email: Takahiro Ikeuchi <takahiro.ikeuchi@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/iktakahiro/slackpy
|
|
8
|
+
Keywords: Slack,Slack Client
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
14
|
+
Classifier: Topic :: System :: Logging
|
|
15
|
+
Classifier: Topic :: Communications :: Chat
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/x-rst
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: requests>=2.32.5
|
|
20
|
+
Requires-Dist: certifi>=2026.10.19
|
|
21
|
+
Requires-Dist: retrying
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest; extra == "test"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
.. image:: https://img.shields.io/pypi/v/slackpy.svg
|
|
27
|
+
:target: https://pypi.python.org/pypi/slackpy
|
|
28
|
+
|
|
29
|
+
.. image:: https://img.shields.io/pypi/dm/slackpy.svg
|
|
30
|
+
:target: https://pypi.python.org/pypi/slackpy
|
|
31
|
+
|
|
32
|
+
slackpy
|
|
33
|
+
=======
|
|
34
|
+
|
|
35
|
+
A Simple and Human Friendly `Slack`_ Client for Logging.
|
|
36
|
+
|
|
37
|
+
Use Case
|
|
38
|
+
--------
|
|
39
|
+
|
|
40
|
+
- Web Application
|
|
41
|
+
- NEW! `AWS Lambda`_
|
|
42
|
+
- Bash Script
|
|
43
|
+
|
|
44
|
+
Install
|
|
45
|
+
-------
|
|
46
|
+
|
|
47
|
+
Requires Python 3.11 or newer.
|
|
48
|
+
|
|
49
|
+
.. code:: sh
|
|
50
|
+
|
|
51
|
+
pip install slackpy
|
|
52
|
+
|
|
53
|
+
Dependencies
|
|
54
|
+
------------
|
|
55
|
+
|
|
56
|
+
- requests>=2.32.5
|
|
57
|
+
- certifi>=2026.10.19
|
|
58
|
+
- retrying
|
|
59
|
+
|
|
60
|
+
Sample Code
|
|
61
|
+
-----------
|
|
62
|
+
|
|
63
|
+
.. code:: python
|
|
64
|
+
|
|
65
|
+
import slackpy
|
|
66
|
+
|
|
67
|
+
INCOMING_WEB_HOOK = 'your_web_hook_url'
|
|
68
|
+
CHANNEL = '#general'
|
|
69
|
+
USER_NAME = 'Logger'
|
|
70
|
+
ICON_URL = 'http://lorempixel.com/48/48'
|
|
71
|
+
|
|
72
|
+
# Create a new instance.
|
|
73
|
+
logging = slackpy.SlackLogger(INCOMING_WEB_HOOK, CHANNEL, USER_NAME, ICON_URL)
|
|
74
|
+
|
|
75
|
+
# You can set a log level. The default level is INFO.
|
|
76
|
+
logging.set_log_level(slackpy.LogLv.DEBUG) # Or logging.set_log_level(10)
|
|
77
|
+
|
|
78
|
+
## Minimum Parameter
|
|
79
|
+
## logging = slackpy.SlackLogger(INCOMING_WEB_HOOK)
|
|
80
|
+
|
|
81
|
+
# Simple Usage
|
|
82
|
+
logging.info('INFO Message')
|
|
83
|
+
|
|
84
|
+
# LogLevel's only required parameter is "message", all others are optional.
|
|
85
|
+
|
|
86
|
+
# LogLevel: DEBUG
|
|
87
|
+
logging.debug(message='DEBUG Message', title='DEBUG Title', fields='')
|
|
88
|
+
|
|
89
|
+
# LogLevel: INFO
|
|
90
|
+
logging.info(message='INFO Message', title='INFO Title', fields='')
|
|
91
|
+
|
|
92
|
+
# LogLevel: WARN
|
|
93
|
+
logging.warn(message='WARN Message', title='WARN Title', fields='')
|
|
94
|
+
|
|
95
|
+
# LogLevel: ERROR
|
|
96
|
+
logging.error(message='ERROR Message', title='ERROR Title', fields='')
|
|
97
|
+
|
|
98
|
+
# LogLevel: CUSTOM
|
|
99
|
+
logging.message(message='CUSTOM Message', title='CUSTOM Title', color='good',
|
|
100
|
+
fields=[{"title": "CUSTOM", "value": "test", "short": True}],
|
|
101
|
+
log_level=40)
|
|
102
|
+
|
|
103
|
+
# Title Link (New v2.1.0)
|
|
104
|
+
logging.info(message='INFO Message', title='slackpy Repository here',
|
|
105
|
+
title_link='https://github.com/iktakahiro/slackpy')
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
+-----------+----------------+------------------------+
|
|
109
|
+
| Method | LogLevel | Color |
|
|
110
|
+
+===========+================+========================+
|
|
111
|
+
| debug() | DEBUG (10) | #03A9F4 (Light Blue) |
|
|
112
|
+
+-----------+----------------+------------------------+
|
|
113
|
+
| info() | INFO (20) | good (green) |
|
|
114
|
+
+-----------+----------------+------------------------+
|
|
115
|
+
| warn() | WARNING (30) | warning (orange) |
|
|
116
|
+
+-----------+----------------+------------------------+
|
|
117
|
+
| error() | ERROR (40) | danger (red) |
|
|
118
|
+
+-----------+----------------+------------------------+
|
|
119
|
+
|
|
120
|
+
The definition of the log level is based on the standard library.
|
|
121
|
+
(https://docs.python.org/3/library/logging.html#levels)
|
|
122
|
+
|
|
123
|
+
Command line
|
|
124
|
+
------------
|
|
125
|
+
|
|
126
|
+
.. code:: sh
|
|
127
|
+
|
|
128
|
+
export SLACK_INCOMING_WEB_HOOK='your_web_hook_url'
|
|
129
|
+
|
|
130
|
+
# LogLevel: DEBUG
|
|
131
|
+
slackpy -c '#your_channel' -m 'DEBUG Message' -l 10
|
|
132
|
+
|
|
133
|
+
# LogLevel: INFO
|
|
134
|
+
slackpy -c '#your_channel' -m 'INFO Message' -l 20
|
|
135
|
+
|
|
136
|
+
# LogLevel: WARN
|
|
137
|
+
slackpy -c '#your_channel' -m 'WARN Message' -l 30
|
|
138
|
+
|
|
139
|
+
# LogLevel: ERROR
|
|
140
|
+
slackpy -c '#your_channel' -m 'ERROR Message' -l 40
|
|
141
|
+
|
|
142
|
+
# LogLevel: DEBUG (without specifying #channel)
|
|
143
|
+
slackpy -m 'DEBUG Message' -l 10
|
|
144
|
+
|
|
145
|
+
# LogLevel: INFO (with Message Title)
|
|
146
|
+
slackpy -c '#your_channel' -t 'INFO Message Title' -m 'INFO Message' -l 20
|
|
147
|
+
|
|
148
|
+
For AWS Lamdba
|
|
149
|
+
--------------
|
|
150
|
+
|
|
151
|
+
.. code:: sh
|
|
152
|
+
|
|
153
|
+
# First, pip install to top of project directory.
|
|
154
|
+
pip install slackpy -t .
|
|
155
|
+
|
|
156
|
+
# Second, archive your source code and dependency packages.
|
|
157
|
+
zip -r src.zip lambda_function.py slackpy requests certifi charset_normalizer idna urllib3 retrying
|
|
158
|
+
|
|
159
|
+
# Finally, upload src.zip
|
|
160
|
+
|
|
161
|
+
About Versioning
|
|
162
|
+
----------------
|
|
163
|
+
|
|
164
|
+
slackpy is following `Semantic Versioning 2.0.0 <http://semver.org/spec/v2.0.0.html>`_.
|
|
165
|
+
|
|
166
|
+
.. _Slack: https://slack.com
|
|
167
|
+
|
|
168
|
+
.. _AWS Lambda: https://aws.amazon.com/lambda/
|
|
169
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
slackpy/__init__.py,sha256=_ET2CF_zFVk5sEJx9RlVCLIgORcf0juBfjBpcB0vt6c,85
|
|
2
|
+
slackpy/commandline.py,sha256=c39p_-Qd0pmahY_hiEn9x5jQDnB44yjFhpDP06aUHMg,2548
|
|
3
|
+
slackpy/slackpy.py,sha256=7MH8VzIp4IULOgEmzXNu3jkR2fTjoE9irc9coIPSWZU,5245
|
|
4
|
+
slackpy-3.0.0.dist-info/licenses/LICENSE,sha256=dE-ncbTW9uBwH_G0BJ1ha9ZwBb-7yv5OnoXvvqFxAzk,1089
|
|
5
|
+
slackpy-3.0.0.dist-info/METADATA,sha256=_f2uLDZZPORFjFuhOlYfx4q6eYYNByfySXB0BFvIni0,4745
|
|
6
|
+
slackpy-3.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
slackpy-3.0.0.dist-info/entry_points.txt,sha256=B95JRYKS-RykfC8QVrDnNydg2-iScNHhtNiYm70MV88,53
|
|
8
|
+
slackpy-3.0.0.dist-info/top_level.txt,sha256=b3AeS6VqtNIKFc3V_sMWsewz0r40qQADmaDZE2-JlXU,8
|
|
9
|
+
slackpy-3.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014-2017 Takahiro Ikeuchi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
slackpy
|