Skip to content

Commit

Permalink
keeps a running log of messages, now with timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
tblanarik committed Oct 13, 2024
1 parent e037b74 commit 908449d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
29 changes: 22 additions & 7 deletions spotbot.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import logging
import os
import datetime
from pytz import timezone
import requests
import tables


def run(req):
logging.info('Python HTTP trigger function processed a request.')
dd = 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)

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

if is_entity_recent(entity):
messageId = entity['MessageId']
content = create_content(req_body)
messageId = call_target(content, messageId)
existingMessage = get_previous_message(messageId)
content = existingMessage + "\n" + content

messageId = post_message(content, messageId)
tables.upsert_entity(table, callsign, messageId)
def create_content(req_body):

def create_content(req_body, dd):
callsign = req_body.get('callsign', 'Unknown')
source = req_body.get('source', 'Unknown')
frequency = req_body.get('frequency', 'Unknown')
Expand All @@ -33,7 +41,7 @@ def create_content(req_body):
spot_deeplink = create_spot_deeplink(source, callsign, wwffRef)

# flags = 4 means it will suppress embeds: https://discord.com/developers/docs/resources/message#message-object-message-flags
content = {"content": f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef}", "flags": 4}
content = {"content": f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {dd.strftime("%H:%M")}", "flags": 4}
return content

def create_spot_deeplink(source, callsign, wwffRef):
Expand All @@ -44,7 +52,7 @@ def create_spot_deeplink(source, callsign, wwffRef):
return f"[{source}](https://api.pota.app/spot/comments/{callsign}/{wwffRef})"
case _:
return ""

def is_entity_recent(entity):
if entity is None:
return False
Expand All @@ -53,7 +61,7 @@ def is_entity_recent(entity):
lookback_seconds = int(os.getenv('LOOKBACK_SECONDS', 7200))
return (cur_time - ent_time).total_seconds() < lookback_seconds

def call_target(content, messageId=None):
def post_message(content, messageId=None):
target_url = os.getenv('TARGET_URL')
verb = "POST"
if messageId is not None:
Expand All @@ -62,5 +70,12 @@ def call_target(content, messageId=None):
response = requests.request(verb, url=target_url, params={"wait": "true"}, json=content)
return extract_message_id(response)

def get_previous_message(messageId):
target_url = os.getenv('TARGET_URL')
verb = "GET"
target_url = target_url + f"/messages/{messageId}"
response = requests.request(verb, url=target_url)
response.json()['content']

def extract_message_id(response):
return response.json()['id']
12 changes: 8 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import unittest
import spotbot
from datetime import datetime
from pytz import timezone

class TestSpotBot(unittest.TestCase):

def test_function_app_basic(self):
dd = datetime.strptime("2024-10-13T01:05:03", "%Y-%m-%dT%H:%M:%S")
req_body = {"callsign":"KI7HSG", "source": "pota", "frequency": "14.074", "mode": "FT8", "wwffRef":"US-0052"}
content = spotbot.create_content(req_body)
expected = {'content': 'KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052', 'flags': 4}
content = spotbot.create_content(req_body, dd)
expected = {'content': 'KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052 | 01:05', 'flags': 4}
self.assertDictEqual(content, expected)

def test_function_app(self):
dd = datetime.strptime("2024-10-13T01:05:03", "%Y-%m-%dT%H:%M:%S")
req_body = {"callsign":"KI7HSG", "source": "sotawatch", "frequency": "14.074", "mode": "FT8", "summitRef": "ABCD"}
content = spotbot.create_content(req_body)
expected = {'content': 'KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD', 'flags': 4}
content = spotbot.create_content(req_body, dd)
expected = {'content': 'KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD | 01:05', 'flags': 4}
self.assertDictEqual(content, expected)

if __name__ == '__main__':
Expand Down

0 comments on commit 908449d

Please sign in to comment.