-
Notifications
You must be signed in to change notification settings - Fork 1
/
spotbot.py
44 lines (38 loc) · 1.66 KB
/
spotbot.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
import logging
import os
import datetime
from hamalertmessage import HamAlertMessage
class SpotBot:
def __init__(self, http_req, table, discord_http):
self.http_req = http_req
self.ham = HamAlertMessage(http_req.get_json())
self.table = table
self.discord_http = discord_http
def process(self):
logging.info('Processing HamAlert message')
previous_message, message_id = self.get_last_message()
if previous_message:
previous_message = self.strikethrough_mesage(previous_message)
content = self.combine_messages(previous_message, self.ham)
else:
content = str(self.ham)
message_id = self.discord_http.post_message(content, message_id)
self.table.upsert_entity(self.ham.callsign, message_id)
def strikethrough_mesage(self, message):
return f"~~{message}~~"
def combine_messages(self, m1, m2):
return f"{m1}\n{m2}"
def get_last_message(self):
last_message_entity = self.table.query_for_entity(self.ham.callsign)
if self.is_entity_recent(last_message_entity):
messageId = last_message_entity['MessageId']
existing_message = self.discord_http.get_message_from_id(messageId)
return existing_message.replace("~", ""), messageId
return "", None
def is_entity_recent(self, entity):
if entity is None:
return False
ent_time = entity.metadata['timestamp']
cur_time = datetime.datetime.now(datetime.timezone.utc)
lookback_seconds = int(os.getenv('LOOKBACK_SECONDS', 7200))
return (cur_time - ent_time).total_seconds() < lookback_seconds