Skip to content

Commit 908449d

Browse files
committed
keeps a running log of messages, now with timestamps
1 parent e037b74 commit 908449d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

spotbot.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
import logging
22
import os
33
import datetime
4+
from pytz import timezone
45
import requests
56
import tables
67

78

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

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

1416
callsign = req_body.get('callsign')
1517

18+
content = create_content(req_body)
19+
1620
table = tables.get_table()
1721
entity = tables.query_for_entity(table, callsign)
1822
messageId = None
23+
existingMessage = None
24+
1925
if is_entity_recent(entity):
2026
messageId = entity['MessageId']
21-
content = create_content(req_body)
22-
messageId = call_target(content, messageId)
27+
existingMessage = get_previous_message(messageId)
28+
content = existingMessage + "\n" + content
29+
30+
messageId = post_message(content, messageId)
2331
tables.upsert_entity(table, callsign, messageId)
24-
25-
def create_content(req_body):
32+
33+
def create_content(req_body, dd):
2634
callsign = req_body.get('callsign', 'Unknown')
2735
source = req_body.get('source', 'Unknown')
2836
frequency = req_body.get('frequency', 'Unknown')
@@ -33,7 +41,7 @@ def create_content(req_body):
3341
spot_deeplink = create_spot_deeplink(source, callsign, wwffRef)
3442

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

3947
def create_spot_deeplink(source, callsign, wwffRef):
@@ -44,7 +52,7 @@ def create_spot_deeplink(source, callsign, wwffRef):
4452
return f"[{source}](https://api.pota.app/spot/comments/{callsign}/{wwffRef})"
4553
case _:
4654
return ""
47-
55+
4856
def is_entity_recent(entity):
4957
if entity is None:
5058
return False
@@ -53,7 +61,7 @@ def is_entity_recent(entity):
5361
lookback_seconds = int(os.getenv('LOOKBACK_SECONDS', 7200))
5462
return (cur_time - ent_time).total_seconds() < lookback_seconds
5563

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

73+
def get_previous_message(messageId):
74+
target_url = os.getenv('TARGET_URL')
75+
verb = "GET"
76+
target_url = target_url + f"/messages/{messageId}"
77+
response = requests.request(verb, url=target_url)
78+
response.json()['content']
79+
6580
def extract_message_id(response):
6681
return response.json()['id']

test.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import unittest
22
import spotbot
3+
from datetime import datetime
4+
from pytz import timezone
35

46
class TestSpotBot(unittest.TestCase):
57

68
def test_function_app_basic(self):
9+
dd = datetime.strptime("2024-10-13T01:05:03", "%Y-%m-%dT%H:%M:%S")
710
req_body = {"callsign":"KI7HSG", "source": "pota", "frequency": "14.074", "mode": "FT8", "wwffRef":"US-0052"}
8-
content = spotbot.create_content(req_body)
9-
expected = {'content': 'KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052', 'flags': 4}
11+
content = spotbot.create_content(req_body, dd)
12+
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}
1013
self.assertDictEqual(content, expected)
1114

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

1822
if __name__ == '__main__':

0 commit comments

Comments
 (0)