Something went wrong. Try again.
Python script for notifications for Teable
Something went wrong. Try again.
TeableWatcher teablewatcher.py
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329import requestsimport jsonimport osimport yaml # <-- NEW IMPORTimport time # <-- NEW IMPORT for main loop
# Define the path to the configuration fileconfig_file = os.path.join(os.path.dirname(__file__), "data/config.yaml")
# =================================================================# CONFIGURATION LOADING# =================================================================
def load_config(): """Loads configuration from the YAML file.""" try: with open(config_file, "r") as f: config = yaml.safe_load(f) return config except FileNotFoundError: print(f"Error: Configuration file not found at {config_file}") exit(1) except yaml.YAMLError as e: print(f"Error parsing YAML configuration: {e}") exit(1)
# Load configuration immediatelyconfig = load_config()
# =================================================================# TEABLE CONFIGURATION (VALUES NOW FROM YAML)# =================================================================
# Teable instance URL; if using the official instance this should be app.teable.ioteable_url = config['teable_url']
# API key; generate this by clicking on your name -> Access Tokenapi_key = config['teable_api_key']
# Array of Base IDs to watchbase_watches = config['watched_bases']
# Dictionary of base IDs and a LIST of table IDs to ignore within that base.# IMPORTANT: This is different from the other script's structure.ignored_tables_raw = config.get('ignored_tables', {})ignored_tables = {}# The old script uses a dict of {base_id: [list of table_ids]}, so we process the YAML list herefor item in ignored_tables_raw: base_id = item['baseid'] table_id = item['tableid'] if base_id not in ignored_tables: ignored_tables[base_id] = [] ignored_tables[base_id].append(table_id)
# Dictionary for specific table watches, mapping Base IDs to a list of specific Table IDs.table_watches = config.get('watched_tables_individual', {})# The YAML structure is a list of objects, we convert it to the script's required dict structure:# { "baseId": [ {"id": "tableId1", "name": "Table Name 1"}, ... ] }table_watches_processed = {}for item in config.get('watched_tables', []): base_id = item['baseid'] table_id = item['tableid'] table_name = item.get('name', table_id) # Use ID as name if name is missing if base_id not in table_watches_processed: table_watches_processed[base_id] = [] table_watches_processed[base_id].append({"id": table_id, "name": table_name})
table_watches = table_watches_processed
# Records cache folder; this will store the previous state of the table to compare against# Using the value from the new config structure for consistencyrecords_cache_dir = os.path.join(os.path.dirname(__file__), "data/records-cache")
# =================================================================# NTFY CONFIGURATION (VALUES NOW FROM YAML)# =================================================================
ntfy_settings = config['ntfy_settings']ntfy_topic = ntfy_settings['topic']ntfy_url = ntfy_settings['server']ntfy_username = ntfy_settings.get('username')ntfy_password = ntfy_settings.get('password')
check_interval = int(config.get('check_interval', 60))
# Function to send an ntfy notificationdef send_ntfy_notification(title, body, click_url=None): """Sends a notification using the ntfy service via HTTP POST.""" headers = { "Title": "Teable Watcher: " + title, "Priority": "5", # 'urgent' for new records "Tags": "bell,teable,new_record", "Content-Type": "text/plain" } # Add the Click header if a URL is provided if click_url: headers["Click"] = click_url
auth = None if ntfy_username and ntfy_password: auth = (ntfy_username, ntfy_password)
try: # The message body is sent as the POST data response = requests.post( f"{ntfy_url}/{ntfy_topic}", data=body.encode('utf-8'), headers=headers, auth=auth ) response.raise_for_status() print(f"ntfy notification sent successfully to topic '{ntfy_topic}'!") except requests.exceptions.RequestException as e: print(f"Error sending ntfy notification: {e}") # =================================================================# INITIALIZATION & EXECUTION# =================================================================
def run_watcher(): """The main logic for checking Teable and sending notifications.""" # Checking connection to Teable print("Checking connection to Teable")
response = None if len(base_watches) > 0: response = requests.get(f"{teable_url}/api/base/{base_watches[0]}/table", headers={"Authorization": f"Bearer {api_key}"}) elif len(table_watches) > 0: # Pick the first table from the first base in the table_watches dict for the check first_base_id = list(table_watches.keys())[0] first_table_id = table_watches[first_base_id][0]['id'] if first_table_id: response = requests.get(f"{teable_url}/api/table/{first_table_id}/record", headers={"Authorization": f"Bearer {api_key}"}) else: print("Table watch configuration error.") return else: print("Nothing to watch.") return
if response.status_code != 200: print(f"Error connecting to Teable: {response.text}") return else: print("Connected to Teable") # Get the list of files in the records cache folder cache_files = [] try: cache_files = os.listdir(records_cache_dir) print(f"Files in records cache folder: {cache_files}") except FileNotFoundError: print(f"Records cache folder '{records_cache_dir}' not found.") os.makedirs(records_cache_dir) cache_files = [] # ================================================================= # WATCH BASES # ================================================================= for base in base_watches: print(f"Watching base {base}") try: response = requests.get(f"{teable_url}/api/base/{base}/table", headers={"Authorization": f"Bearer {api_key}"}) except requests.exceptions.RequestException as e: print(f"Exception occurred while getting tables in base {base}: {e}") continue
if response.status_code != 200: print(f"Error getting tables in base {base}: {response.text}") continue tables = json.loads(response.text) # Get the list of ignored table IDs for the current base tables_to_ignore = ignored_tables.get(base, []) for table in tables: # Check if the table should be ignored if table["id"] in tables_to_ignore: print(f"Ignoring table {table['name']} based on config.") continue print(f"Watching table {table['name']} as part of base {base}") # Fetch records for the current table try: response = requests.get(f"{teable_url}/api/table/{table['id']}/record", headers={"Authorization": f"Bearer {api_key}"}) except requests.exceptions.RequestException as e: print(f"Exception occurred while getting records in table {table['name']}: {e}") continue if response.status_code != 200: print(f"Error fetching records for table {table['name']}: {response.text}") continue
records = json.loads(response.text) # Check if the records cache file exists cache_file = f"{base}-{table['id']}.json" cache_file_fullpath = os.path.join(records_cache_dir, cache_file) cache = None if cache_file in cache_files: try: with open(cache_file_fullpath, "r") as f: cache = json.load(f) except (json.JSONDecodeError, FileNotFoundError): print(f"Error reading JSON from cache file {cache_file}. Skipping comparison.") if not cache: print(f"Cache file {cache_file} not found or corrupted; creating cache for next run.") with open(cache_file_fullpath, "w") as f: json.dump(records, f) continue # Compare the records to the cache cache_record_ids = {r['id'] for r in cache['records']} for record in records['records']: if record['id'] not in cache_record_ids: print(f"New record detected in {table['name']}: {record['id']}") # Construct the Teable URL for the notification click action record_url = f"{teable_url}/base/{base}/{table['id']}" # Construct a more readable notification body notification_body = ( f"A new record has been added to the table '{table['name']}'.\n\n" f"Record Link: {record_url}\n\n" f"Record Details:\n{json.dumps(record, indent=2)}" ) send_ntfy_notification( f"New Record in {table['name']}", notification_body, click_url=record_url # Pass the URL for the Click action ) # Write the current state to the cache for the next run with open(cache_file_fullpath, "w") as f: json.dump(records, f) # ================================================================= # WATCH INDIVIDUAL TABLES # ================================================================= for base_id, tables_info in table_watches.items(): for table_info in tables_info: # Extract info from the new dictionary structure table_id = table_info.get('id') table_name = table_info.get('name', 'Unknown Table') if not table_id: print(f"Skipping malformed table watch config in base {base_id}: {table_info}") continue print(f"Watching table {table_name} in base {base_id}") try: response = requests.get(f"{teable_url}/api/table/{table_id}/record", headers={"Authorization": f"Bearer {api_key}"}) except requests.exceptions.RequestException as e: print(f"Exception occurred while getting records in table {table_name}: {e}") continue if response.status_code != 200: print(f"Error fetching records for table {table_name}: {response.text}") continue
records = json.loads(response.text) # Check if the records cache file exists cache_file = f"{base_id}-{table_id}.json" cache_file_fullpath = os.path.join(records_cache_dir, cache_file) cache = None if cache_file in cache_files: try: with open(cache_file_fullpath, "r") as f: cache = json.load(f) except (json.JSONDecodeError, FileNotFoundError): print(f"Error reading JSON from cache file {cache_file}. Skipping comparison.")
if not cache: print(f"Cache file {cache_file} not found or corrupted; creating cache for next run.") with open(cache_file_fullpath, "w") as f: json.dump(records, f) continue # Compare the records to the cache cache_record_ids = {r['id'] for r in cache['records']} for record in records['records']: if record['id'] not in cache_record_ids: print(f"New record detected in {table_name}: {record['id']}") # Construct the Teable URL for the notification click action record_url = f"{teable_url}/base/{base_id}/{table_id}"
# Construct a more readable notification body notification_body = ( f"A new record has been added to the table '{table_name}'.\n\n" f"Record Link: {record_url}\n\n" f"Record Details:\n{json.dumps(record, indent=2)}" ) send_ntfy_notification( f"New Record in {table_name}", notification_body, click_url=record_url # Pass the URL for the Click action )
# Write the records to the cache with open(cache_file_fullpath, "w") as f: json.dump(records, f)
print("Monitoring round complete.")
# =================================================================# MAIN EXECUTION LOOP (Added for continuous Docker operation)# =================================================================
print(f"Starting Teable Watcher. Checking every {check_interval} seconds.")while True: run_watcher() print(f"Waiting for {check_interval} seconds until next check.") time.sleep(check_interval)