Skip to content

Commit 71e9eb7

Browse files
committed
now storing and retrieving last messageId
1 parent 8eeb142 commit 71e9eb7

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

function_app.py

+47-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import azure.functions as func
22
from azure.data.tables import TableServiceClient
3+
from azure.data.tables import UpdateMode
4+
import json
5+
import datetime
36
import logging
47
import requests
58
import os
@@ -20,7 +23,18 @@ def spotbot(req: func.HttpRequest) -> func.HttpResponse:
2023
return func.HttpResponse("Invalid JSON", status_code=400)
2124

2225
content = create_content(req_body)
23-
return call_target(content)
26+
callsign = req_body.get('callsign')
27+
28+
table = get_table()
29+
entity = query_for_entity(table, callsign)
30+
messageId = None
31+
if is_entity_recent(entity):
32+
messageId = entity.metadata['MessageId']
33+
response = call_target(content, messageId)
34+
messageId = extract_message_id(response)
35+
upsert_entity(table, callsign, messageId)
36+
37+
return response
2438

2539
def create_content(req_body):
2640
callsign = req_body.get('callsign', 'Unknown')
@@ -36,9 +50,13 @@ def create_content(req_body):
3650
content = {"content": f"{callsign} | {source} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef} | {spot_deeplink}", "flags": 4}
3751
return content
3852

39-
def call_target(content):
53+
def call_target(content, messageId=None):
4054
target_url = os.getenv('TARGET_URL')
41-
response = requests.post(target_url, json=content)
55+
verb = "POST"
56+
if messageId is not None:
57+
target_url = target_url + f"/messages/{messageId}"
58+
verb = "PATCH"
59+
response = requests.request(verb, url=target_url, params={"wait": "true"}, json=content)
4260
return func.HttpResponse(response.text, status_code=response.status_code)
4361

4462
def create_spot_deeplink(source, callsign, wwffRef):
@@ -50,17 +68,35 @@ def create_spot_deeplink(source, callsign, wwffRef):
5068
case _:
5169
return ""
5270

53-
def store_in_azure_table(callsign, messageId):
71+
def get_table():
5472
connection_string = os.getenv('AzureWebJobsStorage')
5573
table_name = os.getenv('TABLE_NAME')
5674
table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string)
57-
table_client = table_service_client.create_table(table_name=table_name)
58-
59-
entities = table_client.query_entities(f"PartitionKey eq '{callsign}' and RowKey eq '{callsign}'")
60-
61-
62-
my_entity = {
75+
table_client = table_service_client.get_table_client(table_name=table_name)
76+
return table_client
77+
78+
def query_for_entity(table_client, callsign):
79+
entities = [ent for ent in table_client.query_entities(f"PartitionKey eq '{callsign}' and RowKey eq '{callsign}'")]
80+
if len(entities) > 0:
81+
logging.info(f"Entity already exists for {callsign}")
82+
return entities[0] if len(entities) > 0 else None
83+
84+
def is_entity_recent(entity):
85+
if entity is None:
86+
return False
87+
ent_time = entity.metadata['timestamp']
88+
cur_time = datetime.datetime.now(datetime.timezone.utc)
89+
two_hours_in_seconds = 60 * 60 * 2
90+
return (cur_time - ent_time).total_seconds() < two_hours_in_seconds
91+
92+
def upsert_entity(table_client, callsign, messageId):
93+
entity = {
6394
u'PartitionKey': callsign,
6495
u'RowKey': callsign,
6596
u'MessageId': messageId
66-
}
97+
}
98+
table_client.upsert_entity(mode=UpdateMode.REPLACE, entity=entity)
99+
100+
def extract_message_id(response):
101+
resp = json.loads(response.get_body())
102+
return resp['id']

0 commit comments

Comments
 (0)