Skip to content

Commit

Permalink
Drop redundant option + minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpakos committed Nov 7, 2024
1 parent 53ae3c4 commit 0a1248a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ppslack/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.3.0"
__version__ = "4.0.0"
12 changes: 4 additions & 8 deletions ppslack/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tool to send Slack messages
Copyright (c) 2019-2023 Peter Pakos. All rights reserved.
Copyright (c) 2019-2024 Peter Pakos. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -23,23 +23,19 @@
import sys

from pplogger import get_logger

from . import __version__
from .slack import Slack

__app_name__ = 'ppslack'

parser = argparse.ArgumentParser(description='Tool to send Slack messages', add_help=False)
parser.add_argument('--version', action='version', version='%s %s' % (__app_name__, __version__))
parser.add_argument('--version', action='version', version=f'{__app_name__} {__version__}')
parser.add_argument('--help', action='help', help='show this help message and exit')
parser.add_argument('--debug', action='store_true', dest='debug', help='debugging mode')
parser.add_argument('--verbose', action='store_true', dest='verbose', help='verbose debugging mode')
parser.add_argument('-f', '--from', dest='sender',
help='sender')
parser.add_argument('-f', '--from', dest='sender', help='sender')
parser.add_argument('-t', '--to', dest='recipients', nargs='+', required=True, help='recipient', default=[])
parser.add_argument('-s', '--subject', dest='subject', default='', help='subject')
parser.add_argument('-S', '--slack', action='store_true', dest='slack',
help='Slack message (keeping for backward compatibility)')
parser.add_argument('-H', '--code', dest='code', action='store_true', help='send code block')
args = parser.parse_args()

Expand All @@ -50,7 +46,7 @@ def main():
log.debug(args)

sender = args.sender if args.sender else os.getenv('USER') + '@' + platform.node()
log.debug('Sender: %s' % sender)
log.debug(f'Sender: {sender}')

message = ''
lines = 0
Expand Down
26 changes: 12 additions & 14 deletions ppslack/slack.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""This module implements sending Slack messages
Copyright (c) 2019-2023 Peter Pakos. All rights reserved.
Copyright (c) 2019-2024 Peter Pakos. All rights reserved.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand All @@ -18,9 +18,9 @@
"""

import logging

from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

from ppconfig import Config

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -58,18 +58,18 @@ def users(self):
except SlackApiError:
raise

for user in r["members"]:
for user in r['members']:
if not user['deleted'] and not user['is_bot'] and user['id'] != 'USLACKBOT':
self._users[user['name']] = user

log.debug('Active members: %s' % len(self._users))
log.debug(f'Active members: {len(self._users)}')

return self._users

def find_user_by_email(self, email):
for user in self.users.values():
if user['profile']['email'] == email:
log.debug('Found user with email %s: %s' % (email, user['name']))
log.debug(f'Found user with email {email}: {user['name']}')
return user

return
Expand All @@ -86,7 +86,7 @@ def channels(self):
if not channel['is_archived'] and not channel['is_private']:
self._channels[channel['name']] = channel

log.debug('Public channels: %s' % len(self._channels))
log.debug(f'Public channels: {len(self._channels)}')

return self._channels

Expand All @@ -98,22 +98,19 @@ def send(self, sender, recipients, subject, message, code=False):
recipients = recipientsl

if subject:
subject = '*%s*\n' % subject.strip()
subject = f'*{subject.strip()}*\n'

if code:
message = "```%s```" % message
message = f'```{message}```'

text = '%s%s' % (subject, message)

if code:
text = text.splitlines(True)
text = f'{subject}{message}'

failed = 0

for recipient in recipients:
recipient_id = None

if '@%s' % self._email_domain in recipient:
if f'@{self._email_domain}' in recipient:
user = self.find_user_by_email(recipient)
if user:
recipient_id = user['id']
Expand All @@ -131,10 +128,11 @@ def send(self, sender, recipients, subject, message, code=False):

if not recipient_id:
failed += 1
log.error('User or channel %s not found' % recipient)
log.error(f'User or channel {recipient} not found')
continue

if code:
text = text.splitlines(True)
to_print = []
length = 0

Expand Down

0 comments on commit 0a1248a

Please sign in to comment.