From 908449dbbfa2bc9aada319ffe3b464ab86945589 Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Sun, 13 Oct 2024 04:22:07 +0000 Subject: [PATCH 01/10] keeps a running log of messages, now with timestamps --- spotbot.py | 29 ++++++++++++++++++++++------- test.py | 12 ++++++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/spotbot.py b/spotbot.py index e4e6824..726fb57 100644 --- a/spotbot.py +++ b/spotbot.py @@ -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') @@ -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): @@ -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 @@ -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: @@ -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'] \ No newline at end of file diff --git a/test.py b/test.py index 00c35f2..1175872 100644 --- a/test.py +++ b/test.py @@ -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__': From 3fca94e75de345c8e78f22cb55528fce5415db21 Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Sun, 13 Oct 2024 04:26:11 +0000 Subject: [PATCH 02/10] try pulling time out --- spotbot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spotbot.py b/spotbot.py index 726fb57..c8657e9 100644 --- a/spotbot.py +++ b/spotbot.py @@ -39,9 +39,9 @@ def create_content(req_body, dd): wwffRef = req_body.get('wwffRef', '') spot_deeplink = create_spot_deeplink(source, callsign, wwffRef) - + formatted_time = dd.strftime("%H:%M") # 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} | {dd.strftime("%H:%M")}", "flags": 4} + content = {"content": f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {formatted_time}", "flags": 4} return content def create_spot_deeplink(source, callsign, wwffRef): From 3f19d7c0d57f36bfff8838e1691b9c039c3863cf Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Sun, 13 Oct 2024 04:27:30 +0000 Subject: [PATCH 03/10] lets require pytz --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index fa0f337..92739e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ azure-functions azure-data-tables -requests \ No newline at end of file +requests +pytz \ No newline at end of file From 2cae78bf997d15f85a198f84d44527c6bb1cf09f Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Thu, 17 Oct 2024 23:17:50 +0000 Subject: [PATCH 04/10] correct path to datetime --- spotbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotbot.py b/spotbot.py index c8657e9..851a06c 100644 --- a/spotbot.py +++ b/spotbot.py @@ -8,7 +8,7 @@ def run(req): logging.info('Python HTTP trigger function processed a request.') - dd = datetime.now(timezone('US/Pacific')) + dd = datetime.datetime.now(timezone('US/Pacific')) req_body = req.get_json() logging.info(f"Received JSON: {req_body}") From 0c5b82ea0b2853a8e4c8c8b53ee1b719a9f2944b Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Thu, 17 Oct 2024 23:26:26 +0000 Subject: [PATCH 05/10] pass the rquired param... --- spotbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotbot.py b/spotbot.py index 851a06c..c93baae 100644 --- a/spotbot.py +++ b/spotbot.py @@ -15,7 +15,7 @@ def run(req): callsign = req_body.get('callsign') - content = create_content(req_body) + content = create_content(req_body, dd) table = tables.get_table() entity = tables.query_for_entity(table, callsign) From 910e6f1e0697cef21d61937a1f7d7110a9f13789 Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Thu, 17 Oct 2024 23:33:40 +0000 Subject: [PATCH 06/10] return! --- spotbot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotbot.py b/spotbot.py index c93baae..25a709a 100644 --- a/spotbot.py +++ b/spotbot.py @@ -75,7 +75,7 @@ def get_previous_message(messageId): verb = "GET" target_url = target_url + f"/messages/{messageId}" response = requests.request(verb, url=target_url) - response.json()['content'] + return response.json()['content'] def extract_message_id(response): return response.json()['id'] \ No newline at end of file From 83e37f36dacbc25156a82da57ab1a7628cc047ae Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Fri, 18 Oct 2024 01:02:26 +0000 Subject: [PATCH 07/10] change what we return --- spotbot.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spotbot.py b/spotbot.py index 25a709a..4ad853d 100644 --- a/spotbot.py +++ b/spotbot.py @@ -27,7 +27,9 @@ def run(req): existingMessage = get_previous_message(messageId) content = existingMessage + "\n" + content - messageId = post_message(content, messageId) + content_payload = {"content":content, "flags": 4} + + messageId = post_message(content_payload, messageId) tables.upsert_entity(table, callsign, messageId) def create_content(req_body, dd): @@ -41,7 +43,8 @@ def create_content(req_body, dd): spot_deeplink = create_spot_deeplink(source, callsign, wwffRef) formatted_time = dd.strftime("%H:%M") # 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} | {formatted_time}", "flags": 4} + #content = {"content": f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {formatted_time}", "flags": 4} + content = f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {formatted_time}" return content def create_spot_deeplink(source, callsign, wwffRef): From 5e296fae588ae741e7a8f75b44ab234bc865bd4e Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Fri, 18 Oct 2024 01:07:22 +0000 Subject: [PATCH 08/10] fix unittests --- test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test.py b/test.py index 1175872..52e5fc4 100644 --- a/test.py +++ b/test.py @@ -9,15 +9,15 @@ 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, 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) + expected = 'KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052 | 01:05' + self.assertEqual(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, 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) + expected = 'KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD | 01:05' + self.assertEqual(content, expected) if __name__ == '__main__': unittest.main() From 00a778c54a5130981fd768987d7d95fa684897b3 Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Fri, 18 Oct 2024 03:39:35 +0000 Subject: [PATCH 09/10] cleanup --- spotbot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spotbot.py b/spotbot.py index 4ad853d..b800182 100644 --- a/spotbot.py +++ b/spotbot.py @@ -27,6 +27,7 @@ def run(req): existingMessage = get_previous_message(messageId) 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) @@ -42,8 +43,7 @@ def create_content(req_body, dd): spot_deeplink = create_spot_deeplink(source, callsign, wwffRef) formatted_time = dd.strftime("%H:%M") - # 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} | {formatted_time}", "flags": 4} + content = f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {formatted_time}" return content From effdde973b8864a0501d9b532b897d40f53becd9 Mon Sep 17 00:00:00 2001 From: Trevor Blanarik Date: Sat, 19 Oct 2024 03:05:02 +0000 Subject: [PATCH 10/10] reformat and strikethrough old messages --- spotbot.py | 6 +++--- test.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spotbot.py b/spotbot.py index b800182..41c7b02 100644 --- a/spotbot.py +++ b/spotbot.py @@ -24,8 +24,8 @@ def run(req): if is_entity_recent(entity): messageId = entity['MessageId'] - existingMessage = get_previous_message(messageId) - content = existingMessage + "\n" + content + 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} @@ -44,7 +44,7 @@ def create_content(req_body, dd): spot_deeplink = create_spot_deeplink(source, callsign, wwffRef) formatted_time = dd.strftime("%H:%M") - content = f"{callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {formatted_time}" + content = f"{formatted_time} | {callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef}" return content def create_spot_deeplink(source, callsign, wwffRef): diff --git a/test.py b/test.py index 52e5fc4..51348e1 100644 --- a/test.py +++ b/test.py @@ -9,14 +9,14 @@ 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, dd) - expected = 'KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052 | 01:05' + expected = '01:05 | KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052' self.assertEqual(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, dd) - expected = 'KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD | 01:05' + expected = '01:05 | KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD' self.assertEqual(content, expected) if __name__ == '__main__':