Skip to content

Commit b85bb4a

Browse files
authored
Merge pull request #10 from chirag-jn/dev
Support for Slack Notifications
2 parents 619a890 + f57f8ab commit b85bb4a

16 files changed

+190
-23
lines changed

.github/workflows/package-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
push:
88
branches: [dev, workflows]
99
pull_request:
10-
branches: [dev, master]
10+
branches: [master]
1111

1212
jobs:
1313
build:

.github/workflows/python-linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
push:
88
branches: [dev, workflows]
99
pull_request:
10-
branches: [dev, master]
10+
branches: [master]
1111

1212
jobs:
1313
build:

README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Notipyer
2-
##### Notification Triggers for Python
3-
Send async email notifications via Python. Get updates/crashlogs from your scripts with ease.
2+
#### Notification Triggers for Python
3+
Send async email and slack notifications via Python. Get updates/crashlogs from your scripts with ease.
44

55
## Installation
66
```bash
@@ -16,7 +16,8 @@ Notipyer currently supports Gmail accounts as senders. To allow the library to u
1616
2. Create an app password. [Ref](https://support.google.com/mail/answer/185833)
1717
3. While creating an app password, select app as "Other (Custom name)" and enter a name of your choice.
1818
4. Use the password obtained from app password for the configuration step below.
19-
19+
5. More information can be obtained on the wiki page [here](https://github.com/chirag-jn/notipyer/wiki/Notifications-via-Email)
20+
2021
### Configuration
2122
```python
2223
from notipyer.email_notify import set_email_config
@@ -38,5 +39,30 @@ bcc_recipients = ['[email protected]', '[email protected]'] # Can be N
3839
send_email(subject, body, to_recipients, cc_recipients, bcc_recipients)
3940
```
4041

42+
## Slack Notifications
43+
Notipyer currently supports running a single workplace install only.
44+
45+
For setting up token keys for using slack notifications, follow the wiki page [here](https://github.com/chirag-jn/notipyer/wiki/Notifications-via-Slack)
46+
47+
### Configuration
48+
```python
49+
from notipyer.slack_notify import set_slack_token_config
50+
51+
# Follow the wiki for getting the bot token
52+
BOT_TOKEN = 'xoxb-12345678990123-1234567890123-abcdefghijklmnopqrstuvwx'
53+
set_slack_token_config(BOT_TOKEN)
54+
```
55+
### Sending Message
56+
```python
57+
from notipyer.slack_notify import send_message
58+
59+
# the bot should be added to the channel
60+
channel = 'my-channel-name'
61+
message = 'my-message'
62+
63+
set_slack_token_config(SLACK_TOKEN)
64+
send_message(channel, message)
65+
```
66+
4167
## Contact
4268
[Chirag Jain](https://github.com/chirag-jn)

examples/slack_example.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from notipyer.slack_notify import set_slack_token_config, send_message
2+
3+
SLACK_TOKEN = 'xoxb-12345678990123-1234567890123-abcdefghijklmnopqrstuvwx'
4+
5+
channel = 'my-channel-name'
6+
message = 'my-message'
7+
8+
set_slack_token_config(SLACK_TOKEN)
9+
send_message(channel, message)

notipyer/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
__version__ = '0.1.5'
1+
from .credential_handler import credentials
2+
3+
__version__ = '0.2.0'
4+
__name__ = 'Notipyer'
5+
__short_desc__ = 'Notification Triggers for Python'
6+
__url__ = 'https://github.com/chirag-jn/notipyer'
7+
8+
9+
creds = None
10+
11+
12+
def _get_creds():
13+
global creds
14+
if creds is None:
15+
creds = credentials()
16+
return creds

notipyer/credential_handler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class credentials:
22
EMAIL_ID = ''
33
EMAIL_PASS = ''
4+
SLACK_TOKEN = ''
45

56
def __init__(self):
67
pass
@@ -9,3 +10,7 @@ def __init__(self):
910
def _set_email_credentials(cred_obj, email, password):
1011
cred_obj.EMAIL_ID = email
1112
cred_obj.EMAIL_PASS = password
13+
14+
15+
def _set_slack_token_credentials(cred_obj, token):
16+
cred_obj.SLACK_TOKEN = token

notipyer/email_notify.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from .credential_handler import credentials, _set_email_credentials
2-
from .errors import GmailLoginException, RecipientNotPresentException
3-
from .async_decorator import Async
1+
from .credential_handler import _set_email_credentials
2+
from .utils.errors import GmailLoginException, RecipientNotPresentException
3+
from .utils.async_decorator import Async
4+
from notipyer import _get_creds
45
import smtplib
56

67
SMTP_GMAIL_URL = 'smtp.gmail.com'
78

8-
mail_cred = credentials()
9+
mail_cred = _get_creds()
910

1011

1112
def _check_valid_string(string):

notipyer/errors.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

notipyer/slack_notify.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .credential_handler import _set_slack_token_credentials
2+
from .utils.async_decorator import Async
3+
from .utils.errors import SlackApiException
4+
from slack import WebClient
5+
from slack.errors import SlackApiError
6+
from notipyer import _get_creds
7+
8+
slack_creds = _get_creds()
9+
slack_client = None
10+
11+
12+
def set_slack_token_config(slack_token):
13+
global slack_creds
14+
_set_slack_token_credentials(slack_creds, slack_token)
15+
16+
17+
def _get_slack_client():
18+
global slack_creds, slack_client
19+
if slack_client is None:
20+
slack_client = WebClient(token=slack_creds.SLACK_TOKEN)
21+
return slack_client
22+
23+
24+
@Async
25+
def send_message(channel, text):
26+
try:
27+
_get_slack_client().chat_postMessage(
28+
channel=channel,
29+
text=text
30+
)
31+
except SlackApiError as e:
32+
raise SlackApiException(e)
File renamed without changes.

0 commit comments

Comments
 (0)