Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- import socket
- import datetime
- import time
- import logging
- # Configuração do Logging
- logging.basicConfig(
- level=logging.DEBUG,
- format="%(asctime)s %(message)s",
- datefmt="%d-%m-%Y %H:%M:%S",
- filename="/var/log/medical/discador/discador.log"
- )
- # Database configuration
- DB_CONFIG = {
- 'host': 'localhost',
- 'user': 'XXXXXXXXX',
- 'password': 'XXXXXXXXX',
- 'database': 'medical'
- }
- # Connect to the database
- db_conn = pymysql.connect(**DB_CONFIG)
- cursor = db_conn.cursor()
- # AMI connection details
- AMI_SERVER = 'localhost'
- AMI_PORT = 5038
- AMI_USER = 'medical'
- AMI_SECRET = 'XXXXXXXXX'
- # Connect to AMI
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect((AMI_SERVER, AMI_PORT))
- print("Conectado ao AMI")
- auth_cmd = f"Action: Login\r\nUsername: {AMI_USER}\r\nSecret: {AMI_SECRET}\r\n\r\n"
- s.sendall(auth_cmd.encode())
- print("Dados de Autenticação Enviados com Sucesso")
- # Search all cell phone numbers and count the times for each one
- query = "SELECT celular, COUNT(*) as count FROM marcacoes GROUP BY celular"
- cursor.execute(query)
- results = cursor.fetchall()
- # Close database connection
- cursor.close()
- db_conn.close()
- for row in results:
- celular, count = row
- if not celular:
- continue # Skip if the celular value is empty or None
- # Remove o prefixo '71' se o número começar com ele
- if celular.startswith('71'):
- celular = celular[2:]
- # Choose context based on number of times
- context = 'ura-medical-multiple' if count > 1 else 'ura-medical'
- originate_cmd = f'''Action:Originate
- CallerID:<7132457455>
- Channel:PJSIP/{celular}@Algar
- Context:{context}
- Exten:s
- Priority:1
- Variable: TARGETNUM={celular}
- \r\n\r\n'''
- s.sendall(originate_cmd.encode())
- print(f"Chamada iniciada para {celular}")
- time.sleep(2) # Pausa de 2 segundos
- # Capturar a resposta do AMI
- response = ""
- while True:
- part = s.recv(4096).decode()
- response += part
- if "Response: Success" in part or "Response: Error" in part:
- break
- print("Resposta completa do AMI:", response)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement