diff --git a/README.md b/README.md index b39a08a..43a0088 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ Full list of commands for the bot. - `wx` and `wxc` returns local weather forecast, (wxc is metric value), NOAA or Open Meteo for weather forecasting. - `wxa` and `wxalert` return NOAA alerts. Short title or expanded details - `joke` tells a joke + - `wiki:` search wikipedia, return the first few sentances of first result if a match - `messages` Replay the last messages heard, like Store and Forward - `motd` or to set the message `motd $New Message Of the day` - `lheard` returns the last 5 heard nodes with SNR, can also use `sitrep` @@ -186,6 +187,7 @@ pip install beautifulsoup4 pip install dadjokes pip install geopy pip install schedule +pip install wikipedia ``` The following is needed for open-meteo use ``` diff --git a/config.template b/config.template index 7aa0030..eb269e7 100644 --- a/config.template +++ b/config.template @@ -34,6 +34,8 @@ welcome_message = MeshBot, here for you like a friend who is not. Try sending: p DadJokes = True # enable or disable the Solar module spaceWeather = True +# enable or disable the wikipedia search module +wikipedia = True # StoreForward Enabled and Limits StoreForward = True StoreLimit = 3 diff --git a/mesh_bot.py b/mesh_bot.py index 0df2601..3caefd8 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -22,6 +22,7 @@ def auto_response(message, snr, rssi, hop, message_from_id, channel_number, devi "wxa": lambda: handle_wxalert(message_from_id, deviceID, message), "wxc": lambda: handle_wxc(message_from_id, deviceID, 'wxc'), "wx": lambda: handle_wxc(message_from_id, deviceID, 'wx'), + "wiki:": lambda: handle_wiki(message), "joke": tell_joke, "bbslist": bbs_list_messages, "bbspost": lambda: handle_bbspost(message, message_from_id, deviceID), @@ -93,6 +94,14 @@ def handle_wxalert(message_from_id, deviceID, message): return weatherAlert +def handle_wiki(message): + if "wiki:" in message.lower(): + search = message.split(":")[1] + search = search.rstrip() + return get_wikipedia_summary(search) + else: + return "Please add a search term example:wiki: travelling gnome" + def handle_wxc(message_from_id, deviceID, cmd): location = get_node_location(message_from_id, deviceID) if use_meteo_wxApi and not "wxc" in cmd and not use_metric: diff --git a/modules/settings.py b/modules/settings.py index 823957c..6ec61c3 100644 --- a/modules/settings.py +++ b/modules/settings.py @@ -25,6 +25,7 @@ max_retry_count2 = 4 # max retry count for interface 2 retry_int1 = False retry_int2 = False scheduler_enabled = False # enable the scheduler currently config via code only +wiki_return_limit = 3 # limit the number of sentences returned off the first paragraph first hit # Read the config file, if it does not exist, create basic config file config = configparser.ConfigParser() @@ -93,7 +94,8 @@ try: welcome_message = (f"{welcome_message}").replace('\\n', '\n') # allow for newlines in the welcome message motd_enabled = config['general'].getboolean('motdEnabled', True) dad_jokes_enabled = config['general'].getboolean('DadJokes', True) - solar_conditions_enabled = config['general'].getboolean('spaceWeather', True) + solar_conditions_enabled = config['general'].getboolean('spaceWeather', True) + wikipedia_enabled = config['general'].getboolean('wikipedia', True) sentry_enabled = config['sentry'].getboolean('SentryEnabled', False) # default False secure_channel = config['sentry'].getint('SentryChannel', 2) # default 2 diff --git a/modules/system.py b/modules/system.py index 6ab2f5f..0ec3088 100644 --- a/modules/system.py +++ b/modules/system.py @@ -64,6 +64,12 @@ if dad_jokes_enabled: trap_list = trap_list + ("joke",) help_message = help_message + ", joke" +# Wikipedia Search Configuration +if wikipedia_enabled: + import wikipedia # pip install wikipedia + trap_list = trap_list + ("wiki:",) + help_message = help_message + ", wiki:" + # Scheduled Broadcast Configuration if scheduler_enabled: import schedule # pip install schedule @@ -483,6 +489,16 @@ def tell_joke(): else: return '' +def get_wikipedia_summary(search_term): + # search wikipedia for a summary of the search term + try: + logger.debug(f"System: Searching Wikipedia for:{search_term}") + summary = wikipedia.summary(search_term, sentences=wiki_return_limit) + return summary + except Exception as e: + logger.warning(f"System: Error searching Wikipedia:{e}") + return ERROR_FETCHING_DATA + def messageTrap(msg): # Check if the message contains a trap word message_list=msg.split(" ") diff --git a/requirements.txt b/requirements.txt index 9e20a58..8592490 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,5 @@ retry_requests numpy geopy schedule +wikipedia +