55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
# diag_numero_chatwoot.py - Ver los mensajes de un numero en Chatwoot y que campania hace match.
|
|
import os
|
|
from dotenv import load_dotenv
|
|
import psycopg2
|
|
load_dotenv()
|
|
|
|
TEL = "8099166706" # buscar por terminacion (sin prefijo pais)
|
|
|
|
dm_host=os.getenv("PG_HOST","191.98.134.81"); dm_db=os.getenv("PG_DATABASE","chatwoot_production")
|
|
dm_user=os.getenv("PG_USER","postgres"); dm_pass=os.getenv("PG_PASSWORD",""); dm_port=os.getenv("PG_PORT","5432")
|
|
conn=psycopg2.connect(host=dm_host,dbname=dm_db,user=dm_user,password=dm_pass,port=dm_port)
|
|
cur=conn.cursor()
|
|
|
|
# mensajes del contacto (por terminacion del telefono)
|
|
cur.execute("""
|
|
SELECT c.phone_number, m.id, m.created_at, m.sender_type, LEFT(m.content, 90)
|
|
FROM messages m
|
|
JOIN conversations cv ON m.conversation_id=cv.id
|
|
JOIN contacts c ON cv.contact_id=c.id
|
|
WHERE c.phone_number LIKE %s
|
|
ORDER BY m.id ASC
|
|
""", ('%'+TEL,))
|
|
rows=cur.fetchall()
|
|
print(f"\n=== Mensajes en Chatwoot del numero terminado en {TEL} ===\n")
|
|
if not rows:
|
|
print(" (no se encontro el numero en Chatwoot)")
|
|
for ph,mid,fecha,stype,cont in rows:
|
|
print(f" [{ph}] {fecha} {stype:8} | {cont}")
|
|
|
|
# ¿el primer mensaje del contacto hace match con alguna campania por texto?
|
|
print("\n=== Campanias cuyo texto aparece en algun mensaje de este numero ===")
|
|
cur.execute("""
|
|
SELECT map.codigo, map.cargo, map.fecha_inicio, map.fecha_fin, map.frase
|
|
FROM (VALUES
|
|
(' 🖥️ Estoy interesado en sus Seminarios','Seminarios','74a','2026-04-01','2026-07-30'),
|
|
('👾Estoy interesado en el Seminario Refrigeración','CO2','80a','2026-05-15','2026-07-30')
|
|
) AS map(frase, cargo, codigo, fecha_inicio, fecha_fin)
|
|
WHERE EXISTS (
|
|
SELECT 1 FROM messages m
|
|
JOIN conversations cv ON m.conversation_id=cv.id
|
|
JOIN contacts c ON cv.contact_id=c.id
|
|
WHERE c.phone_number LIKE %s AND m.sender_type='Contact'
|
|
AND m.content LIKE '%%'||map.frase||'%%'
|
|
)
|
|
""", ('%'+TEL,))
|
|
mt=cur.fetchall()
|
|
if not mt:
|
|
print(" (ninguna de las 2 frases de ejemplo hizo match textual - revisar frase exacta)")
|
|
for cod,cargo,fi,ff,frase in mt:
|
|
print(f" codigo={cod} cargo={cargo} rango={fi}..{ff} frase='{frase}'")
|
|
|
|
conn.close()
|
|
print("\nNOTA: si el match textual SI existe pero la fecha del mensaje esta fuera")
|
|
print("del rango de la campania, por eso NO cuenta como pauta (queda Copito).")
|