Skip to content

Commit e309e02

Browse files
committed
- alerts
1 parent 3483c4e commit e309e02

File tree

7 files changed

+809
-102
lines changed

7 files changed

+809
-102
lines changed

config/config.ini

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ ELASTICSEARCH_PASSWORD =
181181
[HolidaySettings]
182182
EnableHolidayNotification = true
183183

184+
# ~~~~~~~~~~~~~~~~~~~~~~~~~
185+
# User-assignable reminders
186+
# ~~~~~~~~~~~~~~~~~~~~~~~~~
187+
[Reminders]
188+
# Enable or disable the reminder/alert functionality
189+
EnableReminders = True
190+
191+
# Maximum number of pending reminders per user
192+
MaxAlertsPerUser = 30
193+
194+
# How often (in seconds) the bot checks for due reminders
195+
PollingIntervalSeconds = 5
196+
184197
# ~~~~~~~~~~~~~~~
185198
# Perplexity API
186199
# ~~~~~~~~~~~~~~~

src/config_paths.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
NWS_RETRIES = 0
3535
NWS_RETRY_DELAY = 2
3636

37+
# read the reminders db
38+
data_directory_name = 'data' # Default name for data directory
39+
REMINDERS_DB_FILENAME = 'reminders.db' # Default name for the reminders DB file
40+
3741
# Attempt to read the configuration file
3842
if CONFIG_PATH.exists():
3943
try:
@@ -48,7 +52,10 @@
4852

4953
# Ensure the logs directory exists
5054
LOGS_DIR.mkdir(parents=True, exist_ok=True)
51-
55+
56+
# Read data directory name from config
57+
data_directory_name = config['DEFAULT'].get('DataDirectory', 'data')
58+
5259
# Update log file paths
5360
LOG_FILE_PATH = LOGS_DIR / config['DEFAULT'].get('LogFile', 'bot.log')
5461
CHAT_LOG_FILE_PATH = LOGS_DIR / config['DEFAULT'].get('ChatLogFile', 'chat.log')
@@ -106,6 +113,18 @@
106113
# CHAT_LOG_MAX_SIZE already set to 10 MB
107114
# Elasticsearch settings already set to defaults
108115

116+
# Define the Data Directory path
117+
DATA_DIR = BASE_DIR / data_directory_name
118+
# Ensure the data directory exists
119+
try:
120+
DATA_DIR.mkdir(parents=True, exist_ok=True)
121+
except OSError as e:
122+
logger.error(f"Could not create data directory {DATA_DIR}: {e}")
123+
124+
# Path for the reminders database
125+
REMINDERS_DB_PATH = DATA_DIR / REMINDERS_DB_FILENAME
126+
logger.info(f"Reminders database path set to: {REMINDERS_DB_PATH}")
127+
109128
# Define paths for token files
110129
TOKEN_FILE_PATH = BASE_DIR / 'config' / 'bot_token.txt'
111130
API_TOKEN_PATH = BASE_DIR / 'config' / 'api_token.txt'

src/custom_functions.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@
55
# you can add your own custom bot functionalities via OpenAI's API function calls with this.
66

77
import logging
8+
import configparser
9+
from config_paths import CONFIG_PATH
810

911
# from api_get_openrouteservice import get_route, get_directions_from_addresses
1012
# from elasticsearch_handler import search_es # Import the Elasticsearch search function
1113

14+
# load and use logger
15+
logger = logging.getLogger(__name__)
16+
logger.setLevel(logging.INFO)
17+
18+
# Read the config for enabled/disabled function calls
19+
config = configparser.ConfigParser()
20+
config.read(CONFIG_PATH)
21+
try: # Use try-except for safety
22+
enable_reminders = config.getboolean('Reminders', 'EnableReminders', fallback=False)
23+
except (configparser.NoSectionError, configparser.NoOptionError):
24+
enable_reminders = False
25+
26+
# silently observe the chat
1227
async def observe_chat():
1328
# Log observation or perform any silent monitoring if needed
1429
logging.info("Bot is currently observing the chat.")
@@ -149,6 +164,149 @@ async def observe_chat():
149164
}
150165
})
151166

167+
# ~~~~~~~~~~~~~~~~~~~~~~
168+
# reminders (if enabled)
169+
# ~~~~~~~~~~~~~~~~~~~~~~
170+
171+
if enable_reminders:
172+
manage_reminder_function = {
173+
'name': 'manage_reminder',
174+
'description': """Manages user reminders (alerts). Specify the action: 'add' to create, 'view' to list pending, 'delete' to remove by ID, or 'edit' to modify by ID.
175+
- For 'add': requires 'reminder_text' and exact 'due_time_utc' (ISO 8601 format, e.g., '2025-04-04T10:00:00Z'). Calculate UTC from user input based on current system UTC time.
176+
- For 'view': no other parameters needed.
177+
- For 'delete': requires 'reminder_id'.
178+
- For 'edit': requires 'reminder_id' and at least one of 'reminder_text' or 'due_time_utc'.""",
179+
'parameters': {
180+
'type': 'object',
181+
'properties': {
182+
'action': {
183+
'type': 'string',
184+
'enum': ['add', 'view', 'delete', 'edit'],
185+
'description': "The operation: 'add', 'view', 'delete', or 'edit'."
186+
},
187+
'reminder_text': {
188+
'type': 'string',
189+
'description': "Text of the reminder. Required for 'add', optional for 'edit'."
190+
},
191+
'due_time_utc': {
192+
'type': 'string',
193+
'description': "Due time in UTC ISO 8601 format (YYYY-MM-DDTHH:MM:SSZ). Required for 'add', optional for 'edit'."
194+
},
195+
'reminder_id': {
196+
'type': 'integer',
197+
'description': "ID of the reminder. Required for 'delete' and 'edit'."
198+
}
199+
},
200+
'required': ['action']
201+
}
202+
}
203+
custom_functions.append(manage_reminder_function) # Directly append if enabled
204+
logger.info("Reminder function 'manage_reminder' appended to custom_functions list.")
205+
else:
206+
logger.info("Reminders disabled in config.ini => 'manage_reminder' function not added.")
207+
208+
# # original reminder method (tryout)
209+
# if enable_reminders:
210+
# # 1) Add a reminder
211+
# custom_functions.append({
212+
# 'name': 'add_reminder',
213+
# 'description': (
214+
# "[Use if the user wants to set a reminder for a future time.] "
215+
# "Accept user text and a date/time in UTC (YYYY-MM-DDTHH:MM:SSZ). "
216+
# "If user says 'in 5 minutes', parse that to a UTC time. "
217+
# "Return success/failure, and the ID of the reminder if successful."
218+
# ),
219+
# 'parameters': {
220+
# 'type': 'object',
221+
# 'properties': {
222+
# 'due_time_utc': {
223+
# 'type': 'string',
224+
# 'description': (
225+
# "The date/time in UTC, e.g. 2025-01-02T13:00:00Z. "
226+
# "If user says something like 'in 5 minutes', parse into UTC. "
227+
# "If date/time is missing, ask user for clarification."
228+
# )
229+
# },
230+
# 'reminder_text': {
231+
# 'type': 'string',
232+
# 'description': (
233+
# "What does the user want to be reminded of? E.g. 'Take out the trash'."
234+
# )
235+
# }
236+
# },
237+
# 'required': ['due_time_utc', 'reminder_text']
238+
# }
239+
# })
240+
241+
# # 2) View all pending reminders
242+
# custom_functions.append({
243+
# 'name': 'view_reminders',
244+
# 'description': (
245+
# "[Use if the user wants to see their current/pending reminders.] "
246+
# "No arguments needed."
247+
# ),
248+
# 'parameters': {
249+
# 'type': 'object',
250+
# 'properties': {},
251+
# 'required': []
252+
# }
253+
# })
254+
255+
# # 3) Delete a reminder
256+
# custom_functions.append({
257+
# 'name': 'delete_reminder',
258+
# 'description': (
259+
# "[Use if the user wants to delete/cancel an existing reminder by ID.] "
260+
# "Reminders are typically identified by an integer ID."
261+
# ),
262+
# 'parameters': {
263+
# 'type': 'object',
264+
# 'properties': {
265+
# 'reminder_id': {
266+
# 'type': 'integer',
267+
# 'description': (
268+
# "The ID number of the reminder to delete. "
269+
# "If user doesn't know the ID, prompt them to /viewreminders first or if they ask for you to show them their reminders."
270+
# )
271+
# }
272+
# },
273+
# 'required': ['reminder_id']
274+
# }
275+
# })
276+
277+
# # 4) Edit a reminder (optional)
278+
# custom_functions.append({
279+
# 'name': 'edit_reminder',
280+
# 'description': (
281+
# "[Use if user wants to update an existing reminder. Provide the ID plus new text/time.] "
282+
# "Either 'due_time_utc' or 'reminder_text' or both can be changed."
283+
# ),
284+
# 'parameters': {
285+
# 'type': 'object',
286+
# 'properties': {
287+
# 'reminder_id': {
288+
# 'type': 'integer',
289+
# 'description': "The ID of the reminder to edit."
290+
# },
291+
# 'due_time_utc': {
292+
# 'type': 'string',
293+
# 'description': (
294+
# "The new date/time in UTC, e.g. 2025-01-02T13:00:00Z. "
295+
# "If user says 'tomorrow 10am', parse that into a UTC string."
296+
# )
297+
# },
298+
# 'reminder_text': {
299+
# 'type': 'string',
300+
# 'description': "The updated reminder text."
301+
# }
302+
# },
303+
# 'required': ['reminder_id']
304+
# }
305+
# })
306+
307+
# else:
308+
# logging.info("Reminders are disabled in config.ini => not adding reminder functions.")
309+
152310
# # jul 26 / 2024
153311
# custom_functions.append({
154312
# 'name': 'get_rss_feed',

0 commit comments

Comments
 (0)