def call_originate(self, agente, campana_id, tipo_campana, contacto_id, telefono, click2call_type): variables = { 'IdCamp': str(campana_id), 'codCli': str(contacto_id), 'origin': click2call_type, 'Tipocamp': tipo_campana, 'FTSAGENTE': "{0}_{1}".format(agente.id, agente.user.get_full_name()) } channel = "Local/{0}@click2call/n".format(agente.sip_extension) # Genero la llamada via originate por AMI try: client = AmiManagerClient() client.connect() client.originate(channel, "from-oml", False, variables, True, exten=telefono, priority=1, timeout=45000) except Exception as e: error = _("Originate failed by {0} - contacto: {1}".format( e, telefono)) logger.exception(error) return error else: client.disconnect()
def _asignar_agente_a_campana(self, agente, campana, penalty=0): try: client = AmiManagerClient() client.connect() with transaction.atomic(): queue_member = QueueMember(penalty=penalty) queue_member_defaults = QueueMember.get_defaults( agente, campana) queue_member.member = agente queue_member.queue_name = campana.queue_campana queue_member.id_campana = queue_member_defaults['id_campana'] queue_member.membername = queue_member_defaults['membername'] queue_member.interface = queue_member_defaults['interface'] # por ahora no definimos 'paused' queue_member.paused = queue_member_defaults['paused'] queue_member.save() # adicionamos el agente a la cola actual que esta corriendo sip_agentes_logueados = obtener_sip_agentes_sesiones_activas() adicionar_agente_activo_cola(queue_member, campana, sip_agentes_logueados, client) activar_cola() client.disconnect() except Exception as e: print("Can't assign agent to campaign due to {0}".error(e)) raise e
def form_valid(self, form): campana = Campana.objects.get(pk=self.kwargs['pk_campana']) grupo_id = form.cleaned_data.get('grupo') grupo = Grupo.objects.get(pk=grupo_id) sip_agentes_logueados = obtener_sip_agentes_sesiones_activas() # agentes = grupo.agentes.filter(reported_by=self.request.user) agentes = grupo.agentes.filter(is_inactive=False) agentes_logueados_grupo = agentes.filter( sip_extension__in=sip_agentes_logueados) # agrega los agentes a la campana siempre cuando no se encuentren agregados try: client = AmiManagerClient() client.connect() with transaction.atomic(): for agente in agentes: queue_member, created = QueueMember.objects.get_or_create( member=agente, queue_name=campana.queue_campana, defaults=QueueMember.get_defaults(agente, campana)) if created and (agente in agentes_logueados_grupo): adicionar_agente_cola(agente, queue_member, campana, client) activar_cola() client.disconnect() except Exception as e: message = _("<strong>Operación Errónea!</strong> " "No se pudo confirmar la creación del dialplan debido " "al siguiente error: {0}".format(e)) messages.add_message( self.request, messages.ERROR, message, ) return super(GrupoAgenteCreateView, self).form_valid(form)
def delete(self, request, *args, **kwargs): self.object = self.get_object() if self.object.is_agente and self.object.get_agente_profile(): agente_profile = self.object.get_agente_profile() agente_profile.borrar() # ahora vamos a remover el agente de la cola de asterisk queues_member_agente = agente_profile.campana_member.all() try: client = AmiManagerClient() client.connect() except AMIManagerConnectorError: logger.exception(_("QueueRemove failed ")) for queue_member in queues_member_agente: campana = queue_member.queue_name.campana remover_agente_cola_asterisk(campana, agente_profile, client) client.disconnect() activar_cola() QueueMember.objects.borrar_member_queue(agente_profile) if self.object.is_supervisor and self.object.get_supervisor_profile(): self.object.get_supervisor_profile().borrar() if self.object.is_cliente_webphone and self.object.get_cliente_webphone_profile( ): self.object.get_cliente_webphone_profile().borrar() self.object.borrar() return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): campana = Campana.objects.get(pk=self.kwargs['pk_campana']) self.object = form.save(commit=False) # valido que este agente no se encuentre agregado en esta campana existe_member = QueueMember.objects.\ existe_member_queue(self.object.member, campana.queue_campana) if existe_member: message = _('Operación Errónea! \ Este miembro ya se encuentra en esta cola') messages.add_message( self.request, messages.ERROR, message, ) return self.form_invalid(form) else: try: client = AmiManagerClient() client.connect() with transaction.atomic(): agente = self.object.member queue_member_defaults = QueueMember.get_defaults( agente, campana) self.object.queue_name = campana.queue_campana self.object.id_campana = queue_member_defaults[ 'id_campana'] self.object.membername = queue_member_defaults[ 'membername'] self.object.interface = queue_member_defaults['interface'] # por ahora no definimos 'paused' self.object.paused = queue_member_defaults['paused'] self.object.save() # adicionamos el agente a la cola actual que esta corriendo sip_agentes_logueados = obtener_sip_agentes_sesiones_activas( ) adicionar_agente_activo_cola(self.object, campana, sip_agentes_logueados, client) activar_cola() client.disconnect() except Exception as e: message = _( "<strong>Operación Errónea!</strong> " "No se pudo confirmar la creación del dialplan debido " "al siguiente error: {0}".format(e)) messages.add_message( self.request, messages.ERROR, message, ) return super(QueueMemberCreateView, self).form_valid(form)
def _call_without_campaign(self, agente, tipo_destino, numero): variables = { 'origin': 'withoutCamp', 'FTSAGENTE': "{0}_{1}".format(agente.id, agente.user.get_full_name()) } if tipo_destino == self.AGENT: context = 'oml-dial-internal' exten = self.AGENT variables['agent2Call'] = numero else: # tipo_destino == self.EXTERNAL context = 'oml-dial-out' exten = numero channel = "Local/{0}@click2call/n".format(agente.sip_extension) # Genero la llamada via originate por AMI try: client = AmiManagerClient() client.connect() client.originate(channel, context, False, variables, True, exten=exten, priority=1, timeout=45000) except Exception as e: error = _( "Originate failed by {0} - tipo_destino: {1} - numero {2}". format(e, tipo_destino, numero)) logger.exception(error) return error else: client.disconnect()
def queue_member_delete_view(request, pk_queuemember, pk_campana): """Elimina agente asignado en la campana""" queue_member = QueueMember.objects.get(pk=pk_queuemember) agente = queue_member.member queue_member.delete() campana = Campana.objects.get(pk=pk_campana) # ahora vamos a remover el agente de la cola de asterisk try: client = AmiManagerClient() client.connect() except AMIManagerConnectorError: logger.exception(_("QueueRemove failed ")) remover_agente_cola_asterisk(campana, agente, client) client.disconnect() activar_cola() return HttpResponseRedirect( reverse('queue_member_campana', kwargs={"pk_campana": pk_campana}))
def save_agentes(self, form_list, index_form_agentes): campana_form = list(form_list)[int(self.INICIAL)] campana = campana_form.instance queue_member_formset = list(form_list)[index_form_agentes] queue_member_formset.instance = campana.queue_campana if queue_member_formset.is_valid(): # obtenemos los agentes que estan logueados sip_agentes_logueados = obtener_sip_agentes_sesiones_activas() # se asignan valores por defecto en cada una de las instancias # de QueueMember a salvar y se adicionan a sus respectivas colas en asterisk try: client = AmiManagerClient() client.connect() except AMIManagerConnectorError: logger.exception(_("QueueAdd failed ")) for queue_form in queue_member_formset.forms: if queue_form.cleaned_data != {}: # no se tienen en cuenta formularios vacíos agente = queue_form.instance.member queue_member_defaults = QueueMember.get_defaults( agente, campana) queue_form.instance.id_campana = queue_member_defaults[ 'id_campana'] queue_form.instance.membername = queue_member_defaults[ 'membername'] queue_form.instance.interface = queue_member_defaults[ 'interface'] # por ahora no definimos 'paused' queue_form.instance.paused = queue_member_defaults[ 'paused'] queue_form_created = True if queue_form.instance.pk is not None: queue_form_created = False queue_form.save(commit=False) if (agente.sip_extension in sip_agentes_logueados) and queue_form_created: adicionar_agente_cola(agente, queue_form.instance, campana, client) client.disconnect() queue_member_formset.save()