forked from FlavienRx/tgtg_notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier.py
67 lines (55 loc) · 2.17 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import requests
import slack
import telegram
import asyncio
from emoji import emojize
class Notifier:
def __init__(self, user):
# Init notifier
self.send_notif_on = user["send_notif_on"]
if self.send_notif_on == "slack":
try:
self.slack_user_id = user["slack_user_id"]
self.slack_client = slack.WebClient(token=user["slack_token"])
except IndexError:
raise Exception(
"Slack user id or token is missing for user {}".format(
user["email"]
)
)
elif self.send_notif_on == "telegram":
try:
self.telegram_chan_id = user["telegram_chan_id"]
self.telegram_bot = telegram.Bot(token=user["telegram_token"])
except IndexError:
raise Exception(
"Telegram chan id or token is missing for user {}".format(
user["email"]
)
)
elif self.send_notif_on == "discord":
try:
self.discord_webhook_url = user["discord_webhook_url"]
except IndexError:
raise Exception(
"Discord webhook is missing for user {}".format(user["email"])
)
else:
raise Exception("No notifier set for user {}".format(user["email"]))
def send_notification(self, text):
# Send notif on Slack
if self.send_notif_on == "slack":
self.slack_client.chat_postMessage(
channel=self.slack_user_id,
text=":sandwich: {}".format(text),
)
# Send notif on Telegram
elif self.send_notif_on == "telegram":
asyncio.get_event_loop().run_until_complete(self.telegram_bot.send_message(
chat_id=self.telegram_chan_id,
text=emojize(":hamburger: {}".format(text)),
))
# Send notif on Discord
elif self.send_notif_on == "discord":
data = {"content": ":sandwich: {}".format(text)}
requests.post(self.discord_webhook_url, data=data)