-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from chirag-jn/dev
Support for Slack Notifications
- Loading branch information
Showing
16 changed files
with
190 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Notipyer | ||
##### Notification Triggers for Python | ||
Send async email notifications via Python. Get updates/crashlogs from your scripts with ease. | ||
#### Notification Triggers for Python | ||
Send async email and slack notifications via Python. Get updates/crashlogs from your scripts with ease. | ||
|
||
## Installation | ||
```bash | ||
|
@@ -16,7 +16,8 @@ Notipyer currently supports Gmail accounts as senders. To allow the library to u | |
2. Create an app password. [Ref](https://support.google.com/mail/answer/185833) | ||
3. While creating an app password, select app as "Other (Custom name)" and enter a name of your choice. | ||
4. Use the password obtained from app password for the configuration step below. | ||
|
||
5. More information can be obtained on the wiki page [here](https://github.com/chirag-jn/notipyer/wiki/Notifications-via-Email) | ||
|
||
### Configuration | ||
```python | ||
from notipyer.email_notify import set_email_config | ||
|
@@ -38,5 +39,30 @@ bcc_recipients = ['[email protected]', '[email protected]'] # Can be N | |
send_email(subject, body, to_recipients, cc_recipients, bcc_recipients) | ||
``` | ||
|
||
## Slack Notifications | ||
Notipyer currently supports running a single workplace install only. | ||
|
||
For setting up token keys for using slack notifications, follow the wiki page [here](https://github.com/chirag-jn/notipyer/wiki/Notifications-via-Slack) | ||
|
||
### Configuration | ||
```python | ||
from notipyer.slack_notify import set_slack_token_config | ||
|
||
# Follow the wiki for getting the bot token | ||
BOT_TOKEN = 'xoxb-12345678990123-1234567890123-abcdefghijklmnopqrstuvwx' | ||
set_slack_token_config(BOT_TOKEN) | ||
``` | ||
### Sending Message | ||
```python | ||
from notipyer.slack_notify import send_message | ||
|
||
# the bot should be added to the channel | ||
channel = 'my-channel-name' | ||
message = 'my-message' | ||
|
||
set_slack_token_config(SLACK_TOKEN) | ||
send_message(channel, message) | ||
``` | ||
|
||
## Contact | ||
[Chirag Jain](https://github.com/chirag-jn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from notipyer.slack_notify import set_slack_token_config, send_message | ||
|
||
SLACK_TOKEN = 'xoxb-12345678990123-1234567890123-abcdefghijklmnopqrstuvwx' | ||
|
||
channel = 'my-channel-name' | ||
message = 'my-message' | ||
|
||
set_slack_token_config(SLACK_TOKEN) | ||
send_message(channel, message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
__version__ = '0.1.5' | ||
from .credential_handler import credentials | ||
|
||
__version__ = '0.2.0' | ||
__name__ = 'Notipyer' | ||
__short_desc__ = 'Notification Triggers for Python' | ||
__url__ = 'https://github.com/chirag-jn/notipyer' | ||
|
||
|
||
creds = None | ||
|
||
|
||
def _get_creds(): | ||
global creds | ||
if creds is None: | ||
creds = credentials() | ||
return creds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from .credential_handler import _set_slack_token_credentials | ||
from .utils.async_decorator import Async | ||
from .utils.errors import SlackApiException | ||
from slack import WebClient | ||
from slack.errors import SlackApiError | ||
from notipyer import _get_creds | ||
|
||
slack_creds = _get_creds() | ||
slack_client = None | ||
|
||
|
||
def set_slack_token_config(slack_token): | ||
global slack_creds | ||
_set_slack_token_credentials(slack_creds, slack_token) | ||
|
||
|
||
def _get_slack_client(): | ||
global slack_creds, slack_client | ||
if slack_client is None: | ||
slack_client = WebClient(token=slack_creds.SLACK_TOKEN) | ||
return slack_client | ||
|
||
|
||
@Async | ||
def send_message(channel, text): | ||
try: | ||
_get_slack_client().chat_postMessage( | ||
channel=channel, | ||
text=text | ||
) | ||
except SlackApiError as e: | ||
raise SlackApiException(e) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from notipyer import __url__ as url | ||
|
||
|
||
class RecipientNotPresentException(Exception): | ||
def __init__(self): | ||
message = 'No recipients found.' | ||
super().__init__(message) | ||
|
||
|
||
class GmailLoginException(Exception): | ||
def __init__(self): | ||
message = "The App password doesn't work." \ | ||
f" Please refer README at {url}" | ||
super().__init__(message) | ||
|
||
|
||
class SlackApiException(Exception): | ||
def __init__(self, e): | ||
message = "The Slack trigger for the bot failed" \ | ||
f" with the error: {e.response}" | ||
super().__init__(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
aiohttp==3.7.4.post0 | ||
async-timeout==3.0.1 | ||
atomicwrites==1.4.0 | ||
attrs==21.2.0 | ||
bleach==3.3.0 | ||
certifi==2020.12.5 | ||
chardet==4.0.0 | ||
colorama==0.4.4 | ||
docutils==0.17.1 | ||
flake8==3.9.2 | ||
idna==2.10 | ||
importlib-metadata==4.3.1 | ||
iniconfig==1.1.1 | ||
keyring==23.0.1 | ||
mccabe==0.6.1 | ||
multidict==5.1.0 | ||
packaging==20.9 | ||
pkginfo==1.7.0 | ||
pluggy==0.13.1 | ||
py==1.10.0 | ||
pycodestyle==2.7.0 | ||
pyflakes==2.3.1 | ||
Pygments==2.9.0 | ||
pyparsing==2.4.7 | ||
pytest==6.2.4 | ||
pywin32-ctypes==0.2.0 | ||
readme-renderer==29.0 | ||
requests==2.25.1 | ||
requests-toolbelt==0.9.1 | ||
rfc3986==1.5.0 | ||
six==1.16.0 | ||
slackclient==2.9.3 | ||
toml==0.10.2 | ||
tqdm==4.61.0 | ||
twine==3.4.1 | ||
typing-extensions==3.10.0.0 | ||
urllib3==1.26.5 | ||
webencodings==0.5.1 | ||
yarl==1.6.3 | ||
zipp==3.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
aiohttp==3.7.4.post0 | ||
async-timeout==3.0.1 | ||
attrs==21.2.0 | ||
bleach==3.3.1 | ||
certifi==2021.5.30 | ||
chardet==4.0.0 | ||
charset-normalizer==2.0.3 | ||
colorama==0.4.4 | ||
docutils==0.17.1 | ||
idna==3.2 | ||
importlib-metadata==4.6.1 | ||
keyring==23.0.1 | ||
multidict==5.1.0 | ||
packaging==21.0 | ||
pkginfo==1.7.1 | ||
Pygments==2.9.0 | ||
pyparsing==2.4.7 | ||
pywin32-ctypes==0.2.0 | ||
readme-renderer==29.0 | ||
requests==2.26.0 | ||
requests-toolbelt==0.9.1 | ||
rfc3986==1.5.0 | ||
six==1.16.0 | ||
slackclient==2.9.3 | ||
tqdm==4.61.2 | ||
twine==3.4.2 | ||
typing-extensions==3.10.0.0 | ||
urllib3==1.26.6 | ||
webencodings==0.5.1 | ||
yarl==1.6.3 | ||
zipp==3.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
username = "[email protected]" | ||
password = "myapppassword" | ||
target_email = "[email protected]" | ||
slack_token = 'xoxb-12345678990123-1234567890123-abcdefghijklmnopqrstuvwx' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from credentials import * | ||
|
||
from notipyer.slack_notify import set_slack_token_config, send_message | ||
|
||
set_slack_token_config(slack_token) | ||
send_message("random", "my message") |