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 all 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
34 changes: 26 additions & 8 deletions spotbot.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
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.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']
content = create_content(req_body)
messageId = call_target(content, 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)
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 +42,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 = f"{formatted_time} | {callsign} | {spot_deeplink} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef}"
return content

def create_spot_deeplink(source, callsign, wwffRef):
Expand All @@ -44,7 +55,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 +64,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 +73,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)
return response.json()['content']

def extract_message_id(response):
return response.json()['id']
16 changes: 10 additions & 6 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
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}
self.assertDictEqual(content, expected)
content = spotbot.create_content(req_body, dd)
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)
expected = {'content': 'KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD', 'flags': 4}
self.assertDictEqual(content, expected)
content = spotbot.create_content(req_body, dd)
expected = '01:05 | KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD'
self.assertEqual(content, expected)

if __name__ == '__main__':
unittest.main()
Loading