2021-06-17 13:25:00 -04:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
2021-10-06 15:55:53 -04:00
|
|
|
from aprsd import messaging, plugin, plugin_utils
|
2021-06-17 13:25:00 -04:00
|
|
|
|
|
|
|
import aprsd_slack_plugin
|
|
|
|
from aprsd_slack_plugin import base_plugin
|
|
|
|
|
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
|
|
|
|
2021-10-06 15:55:53 -04:00
|
|
|
class SlackLocationPlugin(base_plugin.SlackPluginBase, plugin.APRSDRegexCommandPluginBase):
|
2021-06-17 13:25:00 -04:00
|
|
|
"""SlackCommandPlugin.
|
|
|
|
|
|
|
|
This APRSD plugin looks for the location command comming in
|
|
|
|
to aprsd, then fetches the caller's location, and then reports
|
|
|
|
that location string to the configured slack channel.
|
|
|
|
|
|
|
|
To use this:
|
|
|
|
Create a slack bot for your workspace at api.slack.com.
|
|
|
|
A good source of information on how to create the app
|
|
|
|
and the tokens and permissions and install the app in your
|
|
|
|
workspace is here:
|
|
|
|
|
|
|
|
https://api.slack.com/start/building/bolt-python
|
|
|
|
|
|
|
|
|
|
|
|
You will need the signing secret from the
|
|
|
|
Basic Information -> App Credentials form.
|
|
|
|
You will also need the Bot User OAuth Access Token from
|
|
|
|
OAuth & Permissions -> OAuth Tokens for Your Team ->
|
|
|
|
Bot User OAuth Access Token.
|
|
|
|
|
|
|
|
Install the app/bot into your workspace.
|
|
|
|
|
|
|
|
Edit your ~/.config/aprsd/aprsd.yml and add the section
|
|
|
|
slack:
|
|
|
|
signing_secret: <signing secret token here>
|
|
|
|
bot_token: <Bot User OAuth Access Token here>
|
|
|
|
channel: <channel name here>
|
|
|
|
"""
|
|
|
|
|
|
|
|
version = aprsd_slack_plugin.__version__
|
|
|
|
|
|
|
|
# matches any string starting with h or H
|
|
|
|
command_regex = "^[lL]"
|
|
|
|
command_name = "location-slack"
|
|
|
|
|
2021-10-06 15:55:53 -04:00
|
|
|
def process(self, packet):
|
2021-06-17 13:25:00 -04:00
|
|
|
LOG.info("SlackCommandPlugin")
|
|
|
|
|
2021-07-15 20:34:51 -04:00
|
|
|
fromcall = packet["from"]
|
|
|
|
message = packet["message_text"]
|
|
|
|
|
2021-06-17 13:25:00 -04:00
|
|
|
is_setup = self.setup_slack()
|
|
|
|
if not is_setup:
|
|
|
|
return
|
|
|
|
|
|
|
|
# get last location of a callsign, get descriptive name from weather service
|
|
|
|
try:
|
2021-10-06 15:55:53 -04:00
|
|
|
self.config.exists(["services", "aprs.fi", "apiKey"])
|
2021-06-17 13:25:00 -04:00
|
|
|
except Exception as ex:
|
|
|
|
LOG.error("Failed to find config aprs.fi:apikey {}".format(ex))
|
|
|
|
return "No aprs.fi apikey found"
|
|
|
|
|
|
|
|
api_key = self.config["services"]["aprs.fi"]["apiKey"]
|
|
|
|
|
|
|
|
# optional second argument is a callsign to search
|
|
|
|
a = re.search(r"^.*\s+(.*)", message)
|
|
|
|
if a is not None:
|
|
|
|
searchcall = a.group(1)
|
|
|
|
searchcall = searchcall.upper()
|
|
|
|
else:
|
|
|
|
# if no second argument, search for calling station
|
|
|
|
searchcall = fromcall
|
|
|
|
|
|
|
|
try:
|
|
|
|
aprs_data = plugin_utils.get_aprs_fi(api_key, searchcall)
|
|
|
|
except Exception as ex:
|
|
|
|
LOG.error("Failed to fetch aprs.fi '{}'".format(ex))
|
|
|
|
return "Failed to fetch aprs.fi location"
|
|
|
|
|
|
|
|
LOG.debug("LocationPlugin: aprs_data = {}".format(aprs_data))
|
|
|
|
if not len(aprs_data["entries"]):
|
|
|
|
LOG.error("Didn't get any entries from aprs.fi")
|
|
|
|
return "Failed to fetch aprs.fi location"
|
|
|
|
|
|
|
|
lat = aprs_data["entries"][0]["lat"]
|
|
|
|
lon = aprs_data["entries"][0]["lng"]
|
|
|
|
try: # altitude not always provided
|
|
|
|
alt = float(aprs_data["entries"][0]["altitude"])
|
|
|
|
except Exception:
|
|
|
|
alt = 0
|
|
|
|
altfeet = int(alt * 3.28084)
|
|
|
|
aprs_lasttime_seconds = aprs_data["entries"][0]["lasttime"]
|
|
|
|
delta_seconds = time.time() - int(aprs_lasttime_seconds)
|
|
|
|
delta_hours = delta_seconds / 60 / 60
|
|
|
|
|
2021-07-09 11:24:46 -04:00
|
|
|
wx_data = None
|
|
|
|
try:
|
|
|
|
wx_data = plugin_utils.get_weather_gov_for_gps(lat, lon)
|
|
|
|
except Exception:
|
|
|
|
LOG.warning("Couldn't fetch forecast.weather.gov")
|
|
|
|
|
2021-06-17 13:25:00 -04:00
|
|
|
callsign_url = "<http://aprs.fi/info/a/{}|{}>".format(searchcall, searchcall)
|
|
|
|
|
|
|
|
aprs_url = "<http://aprs.fi/#!mt=roadmap&z=15&lat={}&lng={}|" " http://aprs.fi/>".format(
|
|
|
|
lat,
|
|
|
|
lon,
|
|
|
|
)
|
|
|
|
|
|
|
|
message = {}
|
|
|
|
message["username"] = "APRSD - Slack Location Plugin"
|
|
|
|
message["icon_emoji"] = ":satellite_antenna:"
|
|
|
|
message["attachments"] = [{}]
|
|
|
|
message["text"] = "{} - Location".format(callsign_url)
|
|
|
|
message["channel"] = "#random"
|
|
|
|
|
|
|
|
attachment = message["attachments"][0]
|
|
|
|
attachment["fallback"] = message["text"]
|
|
|
|
attachment["fields"] = []
|
2021-07-09 11:24:46 -04:00
|
|
|
|
|
|
|
# if the coordinates are outside of the US, we don't get this
|
|
|
|
# aread description
|
|
|
|
if wx_data and "location" in wx_data and "areaDescription" in wx_data["location"]:
|
|
|
|
attachment["fields"].append(
|
|
|
|
{
|
|
|
|
"title": "Location",
|
|
|
|
"value": wx_data["location"]["areaDescription"],
|
|
|
|
"short": True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-06-17 13:25:00 -04:00
|
|
|
attachment["fields"].append(
|
2021-07-09 11:24:46 -04:00
|
|
|
{"title": "Map Location", "value": aprs_url, "short": True},
|
2021-06-17 13:25:00 -04:00
|
|
|
)
|
|
|
|
attachment["fields"].append(
|
|
|
|
{
|
|
|
|
"title": "Altitude",
|
|
|
|
"value": altfeet,
|
|
|
|
"short": True,
|
|
|
|
"fallback": "Altitude - {}".format(altfeet),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
attachment["fields"].append(
|
|
|
|
{
|
|
|
|
"title": "Time",
|
|
|
|
"value": "{} h ago".format(round(delta_hours, 1)),
|
|
|
|
"short": True,
|
|
|
|
"fallback": "Time {} h ago".format(round(delta_hours, 1)),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
LOG.debug(message)
|
|
|
|
|
|
|
|
# self.swc.chat_postMessage(**message)
|
|
|
|
for channel in self.slack_channels:
|
|
|
|
message["channel"] = channel
|
|
|
|
self.swc.chat_postMessage(**message)
|
|
|
|
|
|
|
|
# Don't have aprsd try and send a reply
|
|
|
|
return messaging.NULL_MESSAGE
|