-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
93 lines (54 loc) · 3.33 KB
/
bot.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import logging
from hop import stream, Stream
from hop.io import StartPosition
from hop.auth import Auth
from slack import WebClient
from alerts import Alert
from slack_token import SLACK_TOKEN, hop_username, hop_pw
from utils import create_new_channel, send_message_to_channel
logging.getLogger().setLevel(logging.INFO)
# Auth for hop
auth = Auth(hop_username, hop_pw)
stream = Stream(auth=auth)
if __name__ == '__main__':
with stream.open("kafka://kafka.scimma.org/igwn.gwalert", "r") as s:
logging.info("Hop Skotch stream open. Creating Slack client...")
# Connecting to the slack client for api calls
client = WebClient(token=SLACK_TOKEN)
for message in s:
data = message.content
for instance in data:
# Only continue if the event is real.
if instance['superevent_id'][0] == 'S' or instance['superevent_id'][0] == 's':
logging.info(f"=====\nIncoming alert of length {len(data)}:")
logging.info(f"{instance['alert_type']}: {instance['superevent_id']}")
try:
alert = Alert(instance, ignore_skymap=False)
event_channel = alert.slack_channel
general_channel = "bot-alerts"
cuts_channel = "bot-alerts-good"
# Making sure the alert is real and passes the preliminary cuts and was not already sent to slack.
if alert.is_real:
if alert.is_retraction == False:
message_text = alert.get_GCW_detailed_message()
########
# TODO: Whatever processing you want. Make plots, run analysis, classify event, call other api's etc
########
# This creates a new slack channel for the alert
create_new_channel(client, event_channel)
# We are assuming #bot-alerts already exists and the bot is added to it
send_message_to_channel(client, general_channel, message_text)
# This is sending a message sent to the new channel
send_message_to_channel(client, event_channel, message_text)
# Send message to high quality event channel
if alert.passes_GCW_general_cut():
send_message_to_channel(client, cuts_channel, message_text)
# RETRACTION
else:
retraction_message = alert.get_GCW_retraction_message()
send_message_to_channel(client, event_channel, retraction_message)
except KeyError:
logging.warning('Bad data formatting...skipping message')
except Exception as e:
logging.warning('Something went wrong...')
logging.warning(e)