Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Slack support #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ansible-role/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The role currently support only *mattermost* module.
| .mattermost | no | | Definition of Mattermost backend (parameters are `webhook`, `channel` and `username`) |
| .hipchat | no | | Definition of Hipchat backend (parameters are `url`, `room`, `token` and `color`) |
| .rocketchat | no | | Definition of Rocket Chat backend (parameters are `webhook` and `channel`) |
| .slack | no | | Definition of Slack backend (the only parameter is `webhook` url) |

Default `jidlobot_cron` variable is set to:
```
Expand Down
7 changes: 7 additions & 0 deletions ansible-role/templates/jidlobot.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ BACKENDS:
{% if jidlobot_backend.rocketchat is defined %}
- rocketchat
{% endif %}
{% if jidlobot_backend.slack is defined %}
- slack
{% endif %}

{% if jidlobot_backend.mail is defined %}
MAIL_FROM: {{ jidlobot_backend.mail.from }}
Expand Down Expand Up @@ -58,3 +61,7 @@ HIPCHAT_COLOR: {{ jidlobot_backend.hipchat.color|default('yellow') }}
ROCKETCHAT_WEBHOOK: {{ jidlobot_backend.rocketchat.webhook }}
ROCKETCHAT_CHANNEL: {{ jidlobot_backend.rocketchat.channel }}
{% endif %}

{% if jidlobot_backend.slack is defined %}
SLACK_WEBHOOK: {{ jidlobot_backend.slack.webhook }}
{% endif %}
3 changes: 3 additions & 0 deletions jidlobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from console import send_console
from hipchat import send_hipchat
from rocketchat import send_rocketchat
from slack import send_slack

with open("jidlobot.yml", "r") as conf_file:
config = yaml.safe_load(conf_file)
Expand All @@ -20,5 +21,7 @@
send_hipchat(menus, title, config)
if "rocketchat" in config["BACKENDS"]:
send_rocketchat(menus, title, config)
if "slack" in config["BACKENDS"]:
send_slack(menus, title, config)
if "mail" in config["BACKENDS"]:
send_mail(menus, title, config)
7 changes: 7 additions & 0 deletions jidlobot.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ HIPCHAT_ROOM: 1
HIPCHAT_TOKEN: …
HIPCHAT_COLOR: random

ROCKETCHAT_WEBHOOK: https://rocketchat/hooks/…
ROCKETCHAT_CHANNEL: channel

SLACK_WEBHOOK: https://hooks.slack.com/services/…

BACKENDS:
- mattermost
- mail
- hipchat
- rocketchat
- slack
- console

ZOMATO_UA: 'Mozilla/5.0 (X11; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0'
Expand Down
13 changes: 13 additions & 0 deletions slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import json
import re
import requests


def send_slack(body, subject, config):
body_slack = re.sub(r'^#### (.*)$', r'*\1*', body, flags=re.MULTILINE).replace('\n\n', '\n')
message = "*" + subject + "*\n" + body_slack
payload = json.dumps({
"text": message
})

requests.post(config["SLACK_WEBHOOK"], data=payload, timeout=config["HTTP_TIMEOUT"])