Skip to content

Commit

Permalink
Merged release/2.0.0 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
iktakahiro committed Apr 27, 2016
2 parents 077a4f0 + e277f95 commit 0852ada
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 115 deletions.
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
env:
global:
secure: iECe64CY8tqQrGNrsb6OnVrSgIpjQeqWDn3vlpFxEx6gAQ0qiivHfUJ8DU2gc/hqJgTo/KRPWkHpiR09LLSf/tYMbXx5PG4RDLzXFQoUBZLsfRqswUWKqFeKZnDTLR6zOlYzXo8bD+qFHxQBbWzm/ewReOYORDhm9fGRqFffW05vA0QkJhQPB1sID94k4yGjdtdmVpJDXyEckzHhHzBvWCI3ZuzU2zMbSVlPF7mONdm7gRUTCzAVo9cM/GEFLeM/QTBjJ5uv0OeFhRmXMhKR73zXNf/++YbjFY/WOmtnyGResjTRfRRaYE1OxeejwkLUg0edXE6r0IcU474GVKU8k1lmXGRdi0K58Ed67wro3YxXLkPkV3ri3BveyiRVVVktdusRKrmy45C9uvXJsBaCHcjCn+nTOi/8EjxxMbwEbG/Ya54WQw4gBRaANttvLQAdV+K3EouNODZ+oqPuuCytjRLgA6SOcCgvhUOk7E4O7mPso74shXcqJd6n1XtynAArmeCzDfKjLCd5qfroEI/PzGnQpBC9+fWSGljIOR0yW+nj3nXzlt2biUn/Et+8d6cdpfZ0Qr+sQMrWstbZvitvRqIpag+L6zWBKoDuFqAwbALbM+uEtSPPKwwLFVFvjYH9/yjvp79ZaFCdheIFiy+RwJCQXsH/YfgP+z13goKPKWc=
language: python
python:
- "2.7"
- "3.4"
- "3.5"
# command to install dependencies
install: "pip install -r test-requirements.txt"
# command to run tests
- '2.7'
- '3.4'
- '3.5'
install: pip install -r test-requirements.txt
script: py.test tests/

10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ Sample Code
# LogLevel's only required parameter is "message", all others are optional.
# LogLevel: DEBUG
logging.debug(message='DEBUG Message', title='DEBUG Title', fallback='', fields='')
logging.debug(message='DEBUG Message', title='DEBUG Title', fields='')
# LogLevel: INFO
logging.info(message='INFO Message', title='INFO Title', fallback='', fields='')
logging.info(message='INFO Message', title='INFO Title', fields='')
# LogLevel: WARN
logging.warn(message='WARN Message', title='WARN Title', fallback='', fields='')
logging.warn(message='WARN Message', title='WARN Title', fields='')
# LogLevel: ERROR
logging.error(message='ERROR Message', title='ERROR Title', fallback='', fields='')
logging.error(message='ERROR Message', title='ERROR Title', fields='')
# LogLevel: CUSTOM
logging.message(message='CUSTOM Message', title='CUSTOM Title', fallback='CUSTOM Fallback', color='good',
logging.message(message='CUSTOM Message', title='CUSTOM Title', color='good',
fields=[{"title": "CUSTOM", "value": "test", "short": "true"}],
log_level=40)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name="slackpy",
version="1.4.6",
version="2.0.0",
packages=['slackpy'],
install_requires=requires,
extras_require=extras_requires,
Expand Down
22 changes: 8 additions & 14 deletions slackpy/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ def main():
required=False,
help='Your bot\'s user name',
default='Logger')
parser.add_argument('-f',
'--fallback',
type=str,
required=False,
help='Plain-text summary of the attachment',
default='')

# The purpose of backward compatibility, old args (1, 2, 3)
# are being retained.
Expand All @@ -64,19 +58,19 @@ def main():
client.set_log_level(LogLv.DEBUG)

if args.level == LogLv.DEBUG:
response = client.debug(args.message, args.title, args.fallback)
response = client.debug(args.message, args.title)

elif args.level == LogLv.INFO or args.level == 1:
response = client.info(args.message, args.title, args.fallback)
elif args.level == LogLv.INFO:
response = client.info(args.message, args.title)

elif args.level == LogLv.WARN or args.level == 2:
response = client.warn(args.message, args.title, args.fallback)
elif args.level == LogLv.WARN:
response = client.warn(args.message, args.title)

elif args.level == LogLv.ERROR or args.level == 3:
response = client.error(args.message, args.title, args.fallback)
elif args.level == LogLv.ERROR:
response = client.error(args.message, args.title)

else:
raise Exception("'Level' must be selected from among 1 to 3")
raise Exception("'Level' must be selected from among 10 to 40")

if response.status_code == 200:
print(True)
Expand Down
64 changes: 22 additions & 42 deletions slackpy/slackpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,62 +45,47 @@ def set_log_level(self, lv):
else:
raise ValueError('argument lv is invalid. Choose from values in ErrorLv Class.')

def __build_payload(self, message, title, color, fallback, fields):

if fields is '':
__fields = {
"title": title,
"text": message,
"color": color,
"fallback": fallback
}

__attachments = {
"fields": __fields
}
else:
__attachments = [{
"fallback": fallback,
"color": color,
"text": message,
"fields": fields
}]
def __build_payload(self, message, title, color, fields):

__attachments = [{
"title": title,
"color": color,
"text": message,
"fields": fields,
"mrkdwn_in": ['text', 'fields', 'title']
}]

payload = {
"channel": self.channel,
"username": self.username,
"attachments": __attachments
"attachments": __attachments,
}

return payload

def __send_notification(self, message, title, color='good', fallback='',
def __send_notification(self, message, title, color='good',
fields='', log_level=LogLv.INFO):
"""Send a message to a channel.
Args:
title: The message title.
message: The message body.
color: Can either be one of 'good', 'warning', 'danger',
or any hex color code
fallback: What is shown to IRC/fallback clients
or any hex color code.
Returns:
api_response:
response: A Response of Slack API.
Raises:
TODO:
Exception:
"""
if log_level < self.log_level:
return None

if fallback is '':
fallback = title

payload = self.__build_payload(message, title, color, fallback, fields)
payload = self.__build_payload(message, title, color, fields)

try:
response = requests.post(self.web_hook_url,
data=json.dumps(payload))
data=json.dumps(payload), allow_redirects=False)

except Exception:
raise Exception(traceback.format_exc())
Expand All @@ -110,49 +95,44 @@ def __send_notification(self, message, title, color='good', fallback='',
return response

else:
raise Exception(response.content.decode())
raise Exception('POST failed.')

def debug(self, message, title='Slack Notification', fallback='',
def debug(self, message, title='Slack Notification',
fields=''):
return self.__send_notification(message=message,
title=title,
color='#03A9F4',
fallback=fallback,
fields=fields,
log_level=LogLv.DEBUG)

def info(self, message, title='Slack Notification', fallback='',
def info(self, message, title='Slack Notification',
fields=''):
return self.__send_notification(message=message,
title=title,
color='good',
fallback=fallback,
fields=fields,
log_level=LogLv.INFO)

def warn(self, message, title='Slack Notification', fallback='',
def warn(self, message, title='Slack Notification',
fields=''):
return self.__send_notification(message=message,
title=title,
color='warning',
fallback=fallback,
fields=fields,
log_level=LogLv.WARN)

def error(self, message, title='Slack Notification', fallback='',
def error(self, message, title='Slack Notification',
fields=''):
return self.__send_notification(message=message,
title=title,
color='danger',
fallback=fallback,
fields=fields,
log_level=LogLv.ERROR)

def message(self, message, title='Slack Notification', fallback='',
def message(self, message, title='Slack Notification',
color='good', fields='', log_level=LogLv.ERROR):
return self.__send_notification(message=message,
title=title,
color=color,
fallback=fallback,
fields=fields,
log_level=log_level)

0 comments on commit 0852ada

Please sign in to comment.