Add MySQL contacts table + mysql backend for vm_contacts
- CREATE TABLE contacts (number_e164 UNIQUE, name, email) in asterisk DB - new lookup_mysql() backend, registered as first in _BACKENDS - vm_import_contacts.py: import VCF into MySQL (527 added) - contacts.conf: backends=mysql,file - live: 16 of 130 messages resolved via MySQL contacts
This commit is contained in:
+38
-1
@@ -3,6 +3,7 @@
|
||||
Caller-ID -> contact name resolution for Asterisk voicemail notifications.
|
||||
|
||||
Backends (tried in the order given by contacts.conf 'backends='):
|
||||
mysql - MySQL contacts table on the asterisk DB (primary, fast, offline).
|
||||
file - local vCard (.vcf) or CSV export; no auth, offline, fast.
|
||||
google - Google People API via the Hermes OAuth token.
|
||||
carddav - generic CardDAV with username + app password.
|
||||
@@ -21,6 +22,7 @@ import configparser
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
@@ -311,6 +313,41 @@ def lookup_google(cfg, num_digits, n, log):
|
||||
return None
|
||||
|
||||
|
||||
# ----------------------------------------------------------- mysql backend
|
||||
def lookup_mysql(cfg, num_digits, n, log):
|
||||
"""Look up a contact in the asterisk MySQL DB contacts table.
|
||||
|
||||
Uses vm_store.connect() so it shares the same asterisk DB config and
|
||||
PyMySQL dependency already required by the portal. If the table does
|
||||
not not exist yet, run vm_import_contacts.py to create and populate it.
|
||||
"""
|
||||
try:
|
||||
sys_path = os.path.dirname(os.path.abspath(__file__))
|
||||
if sys_path not in sys.path:
|
||||
sys.path.insert(0, sys_path)
|
||||
import vm_store # noqa: E402 (local import to avoid hard dep at import time)
|
||||
except Exception as e:
|
||||
log("contacts mysql backend: vm_store unavailable (%s)" % e)
|
||||
return None
|
||||
|
||||
num = extract_number(num_digits)
|
||||
if not num:
|
||||
return None
|
||||
try:
|
||||
con = vm_store.connect()
|
||||
row = con.execute(
|
||||
"SELECT name, email FROM contacts "
|
||||
"WHERE number_e164 = ? AND name IS NOT NULL AND name != '' "
|
||||
"ORDER BY id LIMIT 1",
|
||||
(num,)).fetchone()
|
||||
con.close()
|
||||
if row:
|
||||
return (row["name"], row.get("email") or None)
|
||||
except Exception as e:
|
||||
log("contacts mysql backend error: %s" % e)
|
||||
return None
|
||||
|
||||
|
||||
# ---------------------------------------------------------- carddav backend
|
||||
def lookup_carddav(cfg, num_digits, n, log):
|
||||
import ssl
|
||||
@@ -356,7 +393,7 @@ def lookup_carddav(cfg, num_digits, n, log):
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- entrypoint
|
||||
_BACKENDS = {"file": lookup_file, "google": lookup_google, "carddav": lookup_carddav}
|
||||
_BACKENDS = {"mysql": lookup_mysql, "file": lookup_file, "google": lookup_google, "carddav": lookup_carddav}
|
||||
|
||||
|
||||
def resolve(caller_id, log=print):
|
||||
|
||||
Reference in New Issue
Block a user