Advertisement
Guest User

Context AI Calls

a guest
Mar 19th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import pymysql
  2. import socket
  3. import datetime
  4. import time
  5. import logging
  6.  
  7. # Configuração do Logging
  8. logging.basicConfig(
  9.     level=logging.DEBUG,
  10.     format="%(asctime)s %(message)s",
  11.     datefmt="%d-%m-%Y %H:%M:%S",
  12.     filename="/var/log/medical/discador/discador.log"
  13. )
  14.  
  15. # Database configuration
  16. DB_CONFIG = {
  17.     'host': 'localhost',
  18.     'user': 'XXXXXXXXX',
  19.     'password': 'XXXXXXXXX',
  20.     'database': 'medical'
  21. }
  22.  
  23. # Connect to the database
  24. db_conn = pymysql.connect(**DB_CONFIG)
  25. cursor = db_conn.cursor()
  26.  
  27. # AMI connection details
  28. AMI_SERVER = 'localhost'
  29. AMI_PORT = 5038
  30. AMI_USER = 'medical'
  31. AMI_SECRET = 'XXXXXXXXX'
  32.  
  33. # Connect to AMI
  34. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  35. s.connect((AMI_SERVER, AMI_PORT))
  36. print("Conectado ao AMI")
  37.  
  38. auth_cmd = f"Action: Login\r\nUsername: {AMI_USER}\r\nSecret: {AMI_SECRET}\r\n\r\n"
  39. s.sendall(auth_cmd.encode())
  40. print("Dados de Autenticação Enviados com Sucesso")
  41.  
  42. # Search all cell phone numbers and count the times for each one
  43. query = "SELECT celular, COUNT(*) as count FROM marcacoes GROUP BY celular"
  44. cursor.execute(query)
  45. results = cursor.fetchall()
  46.  
  47. # Close database connection
  48. cursor.close()
  49. db_conn.close()
  50.  
  51. for row in results:
  52.     celular, count = row
  53.     if not celular:
  54.         continue  # Skip if the celular value is empty or None
  55.  
  56.     # Remove o prefixo '71' se o número começar com ele
  57.     if celular.startswith('71'):
  58.         celular = celular[2:]
  59.  
  60.     # Choose context based on number of times
  61.     context = 'ura-medical-multiple' if count > 1 else 'ura-medical'
  62.  
  63.     originate_cmd = f'''Action:Originate
  64. CallerID:<7132457455>
  65. Channel:PJSIP/{celular}@Algar
  66. Context:{context}
  67. Exten:s
  68. Priority:1
  69. Variable: TARGETNUM={celular}
  70. \r\n\r\n'''
  71.  
  72.     s.sendall(originate_cmd.encode())
  73.     print(f"Chamada iniciada para {celular}")
  74.  
  75.     time.sleep(2)  # Pausa de 2 segundos
  76.  
  77.     # Capturar a resposta do AMI
  78.     response = ""
  79.     while True:
  80.         part = s.recv(4096).decode()
  81.         response += part
  82.         if "Response: Success" in part or "Response: Error" in part:
  83.             break
  84.  
  85.     print("Resposta completa do AMI:", response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement