Skip to content

Commit f3f7a6f

Browse files
authored
Uses the position, or callsign to determine the Bing search market. (#18)
1 parent 2630a41 commit f3f7a6f

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/aprs_assistant/_bot.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from ._bing import bing_search
1919
from ._bandcond import get_band_conditions
2020
from ._weather import get_weather, format_noaa_alerts, get_noaa_alerts
21-
from ._callsign import get_callsign_info
21+
from ._callsign import get_callsign_info, itu_prefix_lookup
2222

2323
from ._tool_definitions import (
2424
TOOL_WEB_SEARCH,
@@ -73,11 +73,17 @@ def _generate_reply(fromcall, messages):
7373
bulletins_str = ""
7474
dts = ""
7575

76+
country_code = None
77+
7678
if position is not None:
7779
position_str = "\n\nTheir last known position is:\n\n" + json.dumps(
7880
position, indent=4
7981
)
8082

83+
# Extract the country code
84+
if "address" in position and "country_code" in position["address"]:
85+
country_code = position["address"]["country_code"]
86+
8187
# Attempt to get the timezone from the user's position
8288
user_local_tzname = tf.timezone_at(
8389
lat=position["latitude"], lng=position["longitude"]
@@ -128,6 +134,12 @@ def _generate_reply(fromcall, messages):
128134
f"\n\nYou looked up {fromcall}'s callsign and found:\n\n{callsign_info}"
129135
)
130136

137+
# Messy, but we will clean this up later
138+
if country_code is None:
139+
itu_prefix = itu_prefix_lookup(fromcall)
140+
if itu_prefix is not None:
141+
country_code = itu_prefix.country_code
142+
131143
system_message = {
132144
"role": "system",
133145
"content": f"""You are an AI HAM radio operator, with call sign {BOT_CALLSIGN()}. You were created by KK7CMT. You are at home, in your cozy ham shack, monitoring the gobal APRS network. You have a computer and high-speed access to the Internet. You are answering questions from other human operators in the field who lack an internet connection. To this end, you are relaying vital information. Questions can be about anything -- not just HAM radio.
@@ -221,9 +233,10 @@ def _generate_reply(fromcall, messages):
221233
args["query"],
222234
lat=position["latitude"],
223235
lon=position["longitude"],
236+
market=country_code,
224237
)
225238
else:
226-
results = bing_search(args["query"])
239+
results = bing_search(args["query"], market=country_code)
227240

228241
elif function_name == TOOL_CALLSIGN_SEARCH["function"]["name"]:
229242
results = get_callsign_info(args["callsign"])
@@ -234,11 +247,10 @@ def _generate_reply(fromcall, messages):
234247
results = get_band_conditions()
235248

236249
elif function_name == TOOL_USER_WEATHER["function"]["name"]:
237-
country_code = position.get("address", {}).get("country_code", "")
238250
results = get_weather(
239251
lat=position["latitude"],
240252
lon=position["longitude"],
241-
metric=False if country_code == "us" else True,
253+
metric=False if country_code.upper() == "US" else True,
242254
)
243255

244256
elif function_name == TOOL_REGIONAL_WEATHER["function"]["name"]:

0 commit comments

Comments
 (0)