Esempio n. 1
0
def init_callrequest(callrequest_id, campaign_id):
    """This task outbounds the call

    **Attributes**:

        * ``callrequest_id`` - Callrequest ID
    """
    logger = init_callrequest.get_logger()
    obj_callrequest = Callrequest.objects.get(id=callrequest_id)
    obj_callrequest.status = 7  # Update to Process
    obj_callrequest.save()
    logger.info("TASK :: init_callrequest - status = %s" %
                str(obj_callrequest.status))

    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        logger.error("Can't find the campaign : %s" % campaign_id)
        return False

    if obj_callrequest.aleg_gateway:
        id_aleg_gateway = obj_callrequest.aleg_gateway.id
        dialout_phone_number = phonenumber_change_prefix(
            obj_callrequest.phone_number, id_aleg_gateway)
    else:
        dialout_phone_number = obj_callrequest.phone_number
    logger.info("dialout_phone_number : %s" % dialout_phone_number)

    if settings.DIALERDEBUG:
        dialout_phone_number = settings.DIALERDEBUG_PHONENUMBER

    #Retrieve the Gateway for the A-Leg
    gateways = obj_callrequest.aleg_gateway.gateways
    gateway_codecs = obj_callrequest.aleg_gateway.gateway_codecs
    gateway_timeouts = obj_callrequest.aleg_gateway.gateway_timeouts
    gateway_retries = obj_callrequest.aleg_gateway.gateway_retries
    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string
    callmaxduration = obj_campaign.callmaxduration

    #Sanitize gateways
    gateways = gateways.strip()
    if gateways[-1] != '/':
        gateways = gateways + '/'

    if obj_campaign.content_type.app_label == 'survey':
        #Use Survey Statemachine
        answer_url = settings.PLIVO_DEFAULT_SURVEY_ANSWER_URL
    else:
        answer_url = settings.PLIVO_DEFAULT_ANSWER_URL

    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string
    if obj_callrequest.user.userprofile.accountcode and \
        obj_callrequest.user.userprofile.accountcode > 0:
        originate_dial_string = originate_dial_string + \
            ',accountcode=' + str(obj_callrequest.user.userprofile.accountcode)

    #Send Call to API
    #http://ask.github.com/celery/userguide/remote-tasks.html
    """
    #this could be needed if we want to call a different API / Twilio
    import httplib, urllib
    params = urllib.urlencode({'From': '900900000', 'To': '1000',})
    headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
    conn = httplib.HTTPConnection("127.0.0.1:8000")
    conn.request("POST", "/api/dialer_cdr/testcall/", params, headers)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    conn.close()
    """
    if settings.NEWFIES_DIALER_ENGINE.lower() == 'dummy':
        #Use Dummy TestCall
        res = dummy_testcall.delay(callerid=obj_callrequest.callerid,
                                   phone_number=dialout_phone_number,
                                   gateway=gateways)
        result = res.get()
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    elif settings.NEWFIES_DIALER_ENGINE.lower() == 'plivo':
        try:
            #Request Call via Plivo
            from telefonyhelper import call_plivo
            result = call_plivo(callerid=obj_callrequest.callerid,
                                phone_number=dialout_phone_number,
                                Gateways=gateways,
                                GatewayCodecs=gateway_codecs,
                                GatewayTimeouts=gateway_timeouts,
                                GatewayRetries=gateway_retries,
                                ExtraDialString=originate_dial_string,
                                AnswerUrl=answer_url,
                                HangupUrl=settings.PLIVO_DEFAULT_HANGUP_URL,
                                TimeLimit=str(callmaxduration))
        except:
            logger.error('error : call_plivo')
            obj_callrequest.status = 2  # Update to Failure
            obj_callrequest.save()
            if obj_callrequest.campaign_subscriber \
                and obj_callrequest.campaign_subscriber.id:
                obj_subscriber = CampaignSubscriber.objects.get(
                    id=obj_callrequest.campaign_subscriber.id)
                obj_subscriber.status = 4  # Fail

                obj_subscriber.save()
            return False
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    else:
        logger.error('No other method supported, use one of these options :'\
                     'dummy ; plivo')
        return False

    #Update CampaignSubscriber
    if obj_callrequest.campaign_subscriber \
        and obj_callrequest.campaign_subscriber.id:
        obj_subscriber = CampaignSubscriber.objects.get(
            id=obj_callrequest.campaign_subscriber.id)
        if obj_subscriber.count_attempt == None \
            or not obj_subscriber.count_attempt >= 0:
            obj_subscriber.count_attempt = 1
        else:
            obj_subscriber.count_attempt = obj_subscriber.count_attempt + 1
        obj_subscriber.last_attempt = datetime.now()
        obj_subscriber.save()

    #Update CallRequest Object
    obj_callrequest.request_uuid = result['RequestUUID']
    obj_callrequest.save()

    #lock to limit running process, do so per campaign
    #http://ask.github.com/celery/cookbook/tasks.html

    return True
Esempio n. 2
0
def init_callrequest(callrequest_id, campaign_id):
    """This task outbounds the call

    **Attributes**:

        * ``callrequest_id`` - Callrequest ID
    """
    logger = init_callrequest.get_logger()
    obj_callrequest = Callrequest.objects.get(id=callrequest_id)
    logger.info("TASK :: init_callrequest - status = %s" %
                                        str(obj_callrequest.status))

    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        logger.error("Can't find the campaign : %s" % campaign_id)
        return False
    
    phone_number = obj_callrequest.phone_number
    if obj_callrequest.aleg_gateway:
        id_aleg_gateway = obj_callrequest.aleg_gateway.id
        dialout_phone_number = phonenumber_change_prefix(phone_number,
                                         id_aleg_gateway)
    else:
        dialout_phone_number = phone_number
    logger.info("dialout_phone_number : %s" % dialout_phone_number)

    #Construct the dialing out path
    """
    **Gateway Attributes**:

        * ``name`` - Gateway name.
        * ``description`` - Description about Gateway.
        * ``addprefix`` - Add prefix.
        * ``removeprefix`` - Remove prefix.
        * ``gateways`` - "user/,user", # Gateway string to try dialing \
        separated by comma. First in list will be tried first
        * ``gateway_codecs`` - "'PCMA,PCMU','PCMA,PCMU'", \
        # Codec string as needed by FS for each gateway separated by comma
        * ``gateway_timeouts`` - "10,10", # Seconds to timeout in string for\
        each gateway separated by comma
        * ``gateway_retries`` - "2,1", # Retry String for Gateways separated\
        by a comma, on how many times each gateway should be retried
        * ``originate_dial_string`` - originate_dial_string
        * ``secondused`` -
        * ``failover`` -
        * ``addparameter`` -
        * ``count_call`` -
        * ``count_in_use`` -
        * ``maximum_call`` -
        * ``status`` - Gateway status
    """

    #Retrieve the Gateway for the A-Leg
    gateways = obj_callrequest.aleg_gateway.gateways
    gateway_codecs = obj_callrequest.aleg_gateway.gateway_codecs
    gateway_timeouts = obj_callrequest.aleg_gateway.gateway_timeouts
    gateway_retries = obj_callrequest.aleg_gateway.gateway_retries
    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string
    callmaxduration = obj_campaign.callmaxduration

    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string

    #Send Call to API
    #http://ask.github.com/celery/userguide/remote-tasks.html

    """
    #this could be needed if we want to call a different API / Twilio
    import httplib, urllib
    params = urllib.urlencode({'From': '900900000', 'To': '1000',})
    headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
    conn = httplib.HTTPConnection("127.0.0.1:8000")
    conn.request("POST", "/api/dialer_cdr/testcall/", params, headers)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    conn.close()
    """
    if settings.NEWFIES_DIALER_ENGINE.lower() == 'dummy':
        #Use Dummy TestCall
        res = dummy_testcall.delay(callerid=obj_callrequest.callerid,
                                   phone_number=obj_callrequest.phone_number,
                                   gateway=gateways)
        result = res.get()
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    elif settings.NEWFIES_DIALER_ENGINE.lower() == 'plivo':
        try:
            #Request Call via Plivo
            from telefonyhelper import call_plivo
            result= call_plivo(callerid=obj_callrequest.callerid,
                        phone_number=obj_callrequest.phone_number,
                        Gateways=gateways,
                        GatewayCodecs=gateway_codecs,
                        GatewayTimeouts=gateway_timeouts,
                        GatewayRetries=gateway_retries,
                        ExtraDialString=originate_dial_string,
                        AnswerUrl=settings.PLIVO_DEFAULT_ANSWER_URL,
                        #AnswerUrl='http://localhost/~areski/django/MyProjects/plivohelper-php/examples/test.php?answer=1',
                        HangupUrl=settings.PLIVO_DEFAULT_HANGUP_URL,
                        TimeLimit=str(callmaxduration))
        except:
            logger.error('error : call_plivo')
            obj_callrequest.status = 2 # Update to Failure
            obj_callrequest.save()
            obj_subscriber = CampaignSubscriber.objects.get(id=obj_callrequest.campaign_subscriber.id)
            obj_subscriber.status = 4 # Fail
            obj_subscriber.save()
            return False
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    else:
        logger.error('No other method supported, use one of these options :'\
                     'dummy ; plivo')
        return False

    #Update CallRequest Object
    obj_callrequest.request_uuid = result['RequestUUID']
    obj_callrequest.save()

    #lock to limit running process, do so per campaign
    #http://ask.github.com/celery/cookbook/tasks.html

    return True
Esempio n. 3
0
def init_callrequest(callrequest_id, campaign_id):
    """This task outbounds the call

    **Attributes**:

        * ``callrequest_id`` - Callrequest ID
    """
    logger = init_callrequest.get_logger()
    obj_callrequest = Callrequest.objects.get(id=callrequest_id)
    obj_callrequest.status = 7  # Update to Process
    obj_callrequest.save()
    logger.info("TASK :: init_callrequest - status = %s" %
                                        str(obj_callrequest.status))

    try:
        obj_campaign = Campaign.objects.get(id=campaign_id)
    except:
        logger.error("Can't find the campaign : %s" % campaign_id)
        return False

    if obj_callrequest.aleg_gateway:
        id_aleg_gateway = obj_callrequest.aleg_gateway.id
        dialout_phone_number = phonenumber_change_prefix(
                                                obj_callrequest.phone_number,
                                                id_aleg_gateway)
    else:
        dialout_phone_number = obj_callrequest.phone_number
    logger.info("dialout_phone_number : %s" % dialout_phone_number)

    if settings.DIALERDEBUG:
        dialout_phone_number = settings.DIALERDEBUG_PHONENUMBER

    #Retrieve the Gateway for the A-Leg
    gateways = obj_callrequest.aleg_gateway.gateways
    gateway_codecs = obj_callrequest.aleg_gateway.gateway_codecs
    gateway_timeouts = obj_callrequest.aleg_gateway.gateway_timeouts
    gateway_retries = obj_callrequest.aleg_gateway.gateway_retries
    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string
    callmaxduration = obj_campaign.callmaxduration

    #Sanitize gateways
    gateways = gateways.strip()
    if gateways[-1] != '/':
        gateways = gateways + '/'

    if obj_campaign.content_type.app_label == 'survey':
        #Use Survey Statemachine
        answer_url = settings.PLIVO_DEFAULT_SURVEY_ANSWER_URL
    else:
        answer_url = settings.PLIVO_DEFAULT_ANSWER_URL

    originate_dial_string = obj_callrequest.aleg_gateway.originate_dial_string
    if obj_callrequest.user.userprofile.accountcode and \
        obj_callrequest.user.userprofile.accountcode > 0:
        originate_dial_string = originate_dial_string + \
            ',accountcode=' + str(obj_callrequest.user.userprofile.accountcode)

    #Send Call to API
    #http://ask.github.com/celery/userguide/remote-tasks.html

    """
    #this could be needed if we want to call a different API / Twilio
    import httplib, urllib
    params = urllib.urlencode({'From': '900900000', 'To': '1000',})
    headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
    conn = httplib.HTTPConnection("127.0.0.1:8000")
    conn.request("POST", "/api/dialer_cdr/testcall/", params, headers)
    response = conn.getresponse()
    print response.status, response.reason
    data = response.read()
    conn.close()
    """
    if settings.NEWFIES_DIALER_ENGINE.lower() == 'dummy':
        #Use Dummy TestCall
        res = dummy_testcall.delay(callerid=obj_callrequest.callerid,
                                   phone_number=dialout_phone_number,
                                   gateway=gateways)
        result = res.get()
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    elif settings.NEWFIES_DIALER_ENGINE.lower() == 'plivo':
        try:
            #Request Call via Plivo
            from telefonyhelper import call_plivo
            result = call_plivo(
                        callerid=obj_callrequest.callerid,
                        phone_number=dialout_phone_number,
                        Gateways=gateways,
                        GatewayCodecs=gateway_codecs,
                        GatewayTimeouts=gateway_timeouts,
                        GatewayRetries=gateway_retries,
                        ExtraDialString=originate_dial_string,
                        AnswerUrl=answer_url,
                        HangupUrl=settings.PLIVO_DEFAULT_HANGUP_URL,
                        TimeLimit=str(callmaxduration))
        except:
            logger.error('error : call_plivo')
            obj_callrequest.status = 2  # Update to Failure
            obj_callrequest.save()
            if obj_callrequest.campaign_subscriber \
                and obj_callrequest.campaign_subscriber.id:
                obj_subscriber = CampaignSubscriber.objects.get(
                            id=obj_callrequest.campaign_subscriber.id)
                obj_subscriber.status = 4  # Fail

                obj_subscriber.save()
            return False
        logger.info(result)
        logger.error('Received RequestUUID :> ' + str(result['RequestUUID']))

    else:
        logger.error('No other method supported, use one of these options :'\
                     'dummy ; plivo')
        return False

    #Update CampaignSubscriber
    if obj_callrequest.campaign_subscriber \
        and obj_callrequest.campaign_subscriber.id:
        obj_subscriber = CampaignSubscriber.objects.get(
                    id=obj_callrequest.campaign_subscriber.id)
        if obj_subscriber.count_attempt == None \
            or not obj_subscriber.count_attempt >= 0:
            obj_subscriber.count_attempt = 1
        else:
            obj_subscriber.count_attempt = obj_subscriber.count_attempt + 1
        obj_subscriber.last_attempt = datetime.now()
        obj_subscriber.save()

    #Update CallRequest Object
    obj_callrequest.request_uuid = result['RequestUUID']
    obj_callrequest.save()

    #lock to limit running process, do so per campaign
    #http://ask.github.com/celery/cookbook/tasks.html

    return True