diff --git a/discord_http.py b/discord_http.py index 2646b79..5768af6 100644 --- a/discord_http.py +++ b/discord_http.py @@ -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" diff --git a/function_app.py b/function_app.py index 41d743c..0f40f96 100644 --- a/function_app.py +++ b/function_app.py @@ -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) diff --git a/requirements.txt b/requirements.txt index 80288fb..92739e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ azure-functions azure-data-tables requests -pytz -requests-mock \ No newline at end of file +pytz \ No newline at end of file diff --git a/spotbot.py b/spotbot.py index e85fb1c..c0bd5d9 100644 --- a/spotbot.py +++ b/spotbot.py @@ -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 @@ -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) @@ -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) \ No newline at end of file + return (cur_time - ent_time).total_seconds() < lookback_seconds \ No newline at end of file