Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keeps a running log of messages, now with timestamps #20

Merged
merged 10 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
azure-functions
azure-data-tables
requests
requests
pytz
31 changes: 23 additions & 8 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'))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope


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 @@ -31,9 +39,9 @@ def create_content(req_body):
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}", "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):
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
Loading