Skip to content

Commit

Permalink
little cleanup:
Browse files Browse the repository at this point in the history
  • Loading branch information
tblanarik committed Oct 19, 2024
1 parent 1a90297 commit 081a264
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 35 deletions.
1 change: 1 addition & 0 deletions discord_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def __init__(self):
self.session = requests.Session()

def post_message(self, content, messageId=None):
# flags = 4 means it will suppress embeds: https://discord.com/developers/docs/resources/message#message-object-message-flags
content_payload = {"content": content, "flags": 4}
target_url = os.getenv('TARGET_URL')
verb = "POST"
Expand Down
2 changes: 1 addition & 1 deletion function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@app.route(route="spotbot", methods=[func.HttpMethod.POST])
def spotbot(req: func.HttpRequest) -> func.HttpResponse:
try:
sb.run(req)
sb.SpotBot(req).process()
except Exception as _excpt:
logging.error(f"Exception occurred: {_excpt}")
return func.HttpResponse(body=f"Exception occurred: {_excpt}", status_code=500)
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
azure-functions
azure-data-tables
requests
pytz
requests-mock
pytz
33 changes: 1 addition & 32 deletions spotbot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
import os
import datetime
from pytz import timezone
import tables
from hamalertmessage import HamAlertMessage
from tables import HamAlertTable
from discord_http import DiscordHttp
Expand All @@ -16,7 +14,6 @@ def __init__(self, http_req):

def process(self):
logging.info('Processing HamAlert message')

previous_message, message_id = self.get_last_message()
previous_message = self.strikethrough_mesage(previous_message)
content = self.combine_messages(previous_message, self.ham)
Expand All @@ -43,32 +40,4 @@ def is_entity_recent(self, entity):
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


def run(req):
logging.info('Python HTTP trigger function processed a request.')
dd = datetime.datetime.now(timezone('US/Pacific'))

req_body = req.get_json()
logging.info(f"Received JSON: {req_body}")

callsign = req_body.get('callsign')

content = create_content(req_body, dd)

table = tables.get_table()
entity = tables.query_for_entity(table, callsign)
messageId = None
existingMessage = None

if is_entity_recent(entity):
messageId = entity['MessageId']
existingMessage = get_previous_message(messageId).replace("~", "")
content = "~~" + existingMessage + "~~\n" + content

# flags = 4 means it will suppress embeds: https://discord.com/developers/docs/resources/message#message-object-message-flags
content_payload = {"content":content, "flags": 4}

messageId = post_message(content_payload, messageId)
tables.upsert_entity(table, callsign, messageId)
return (cur_time - ent_time).total_seconds() < lookback_seconds

0 comments on commit 081a264

Please sign in to comment.