def send_to_number(self,
                       to,
                       from_,
                       body,
                       to_country=None,
                       from_country=None):
        """Send properly-formatted numbers to FreeSwitch.

        Internally, our canonical format is E.164 without the leading plus (due
        to OpenBTS's inability to handle the + symbol).
        """
        if to_country:
            to = number_utilities.convert_to_e164(to, to_country)
        if from_country:
            from_ = number_utilities.convert_to_e164(from_, from_country)
        to = number_utilities.strip_number(to)
        from_ = number_utilities.strip_number(from_)
        return self._send_raw_to_freeswitch_cli(
            str("python VBTS_Send_SMS %s|%s|%s" % (to, from_, body)))
def fsapi(session, stream, env, args):
    """Handles data from the FS API.

    Args:
      string of the form <service_type>|<call_or_sms>|<destination_number>
    """
    service_type, call_or_sms, destination_number = args.split('|')
    # Sanitize the destination number.
    destination_number = number_utilities.strip_number(destination_number)
    res = str(billing.get_service_tariff(
        service_type, call_or_sms, destination_number=destination_number))
    consoleLog('info', "Returned FSAPI: " + res + "\n")
    stream.write(res)
def chat(message, args):
    """Handles data from the chatplan.

    Args:
      string of the form <service_type>|<call_or_sms>|<destination_number>
    """
    service_type, call_or_sms, destination_number = args.split('|')
    # Sanitize the destination number.
    destination_number = number_utilities.strip_number(destination_number)
    res = str(billing.get_service_tariff(
        service_type, call_or_sms, destination_number=destination_number))
    consoleLog('info', "Returned Chat: " + res + "\n")
    message.chat_execute('set', 'service_type=%s' % res)
def fsapi(session, stream, env, args):
    """Handles data from the FS API.

    Args:
      string of the form <account_balance>|<service_type>|<destination_number>
    """
    balance, service_type, destination_number = args.split('|')
    # Sanitize the destination number.
    destination_number = number_utilities.strip_number(destination_number)
    res = str(
        billing.get_seconds_available(int(balance), service_type,
                                      destination_number))
    consoleLog('info', "Returned FSAPI: " + res + "\n")
    stream.write(res)
def chat(message, args):
    """Handles data from the chatplan.

    Args:
      string of the form <account_balance>|<service_type>|<destination_number>
    """
    balance, service_type, destination_number = args.split('|')
    # Sanitize the destination number.
    destination_number = number_utilities.strip_number(destination_number)
    res = str(
        billing.get_seconds_available(int(balance), service_type,
                                      destination_number))
    consoleLog('info', "Returned Chat: " + res + "\n")
    message.chat_execute('set', 'service_type=%s' % res)
Exemplo n.º 6
0
    def post(self, request, format=None):
        needed_fields = ["imsi", "trans", "balance", "dest"]
        if not all(i in request.POST for i in needed_fields):
            return Response("Missing Args", status=status.HTTP_400_BAD_REQUEST)

        imsi = request.data['imsi']
        promo_type, service_type = extract_types(request.data['trans'])
        balance = int(request.data['balance'])
        dest = request.data['dest']

        destination_number = number_utilities.strip_number(dest)

        # This is the number of seconds available
        # based on the subs current balance
        if 'globe' in service_type:
            # replace first word with globe keyword
            sec_avail = int(
                billing.get_seconds_available(
                    int(balance), 'outside_' + service_type.split('_')[1],
                    destination_number))

        else:
            sec_avail = int(
                billing.get_seconds_available(int(balance), service_type,
                                              destination_number))

        try:
            max_call_duration = Config.objects.get(
                key='max_call_duration').value
            if int(max_call_duration) <= 0:
                raise ValueError
        except (Config.DoesNotExist, ValueError):
            # cap call at 1-day duration limit
            max_call_duration = 24 * 60 * 60

        # if unli, no tariff
        if promo_type == 'U':
            sec_avail = max_call_duration

        # if bulk, depends on remaining quota
        elif promo_type == 'B':
            query = service_type + '__gt'
            promo = PromoSubscription.objects.filter(
                contact__imsi__exact=imsi,
                promo__promo_type=promo_type,
                **{
                    query: 0
                }).order_by('date_expiration')
            if promo:
                key = 'promo[0].%s' % service_type
                sec_avail = eval(key) * 60

        # if discounted, get tariff from earliest subscribed discounted promo
        elif promo_type in ['D', 'G']:
            query = service_type + '__gt'
            promo = PromoSubscription.objects.filter(
                contact__imsi__exact=imsi,
                promo__promo_type=promo_type,
                **{
                    query: 0
                }).order_by('date_expiration')

            if promo:
                key = 'promo[0].%s' % service_type
                disc_rate = eval(key)
                whole, deci = divmod(balance, disc_rate)
                sec_avail = int(whole) * 60

        # If afforded/available seconds is greater than limit,
        # then we use the call duration limit
        if sec_avail > int(max_call_duration):
            ret = str(max_call_duration)
        # Else, use what's originally available
        else:
            ret = str(sec_avail)

        return Response(ret, status=status.HTTP_200_OK)
Exemplo n.º 7
0
    def post(self, request, format=None):
        needed_fields = ["imsi", "trans", "dest"]
        if not all(i in request.POST for i in needed_fields):
            return Response("Missing Args", status=status.HTTP_400_BAD_REQUEST)

        promo_type, service_type = extract_types(request.data['trans'])
        imsi = request.data['imsi']
        dest = request.data['dest']

        # Assume first that regular tariffs will be applied
        # then overwrite later if promos apply.
        # Kinda weird, but this avoid 'ret' being unset if Disc
        # promo is suddenly purged by celery while traversing FS dialplan
        if 'sms' in service_type:
            call_or_sms = 'sms'
        else:
            call_or_sms = 'call'
        destination_number = number_utilities.strip_number(dest)

        # Kludge!
        if 'globe' in service_type:
            # replace first word with globe keyword
            ret = str(
                billing.get_service_tariff(
                    'outside_' + service_type.split('_')[1], call_or_sms,
                    destination_number))
        else:
            ret = str(
                billing.get_service_tariff(service_type, call_or_sms,
                                           destination_number))

        # if unli or bulk, no tariff
        if promo_type == 'U' or promo_type == 'B':
            ret = '0'

        # if discounted, get tariff from earliest subscribed discounted promo
        elif promo_type in ['D']:
            query = service_type + '__gt'
            promo = PromoSubscription.objects.filter(
                contact__imsi__exact=imsi,
                promo__promo_type=promo_type,
                **{
                    query: 0
                }).order_by('date_expiration')

            if promo:
                key = 'promo[0].%s' % service_type
                ret = str(eval(key))
                # else, regular tariffs from above will apply

        elif promo_type in ['G']:
            query = service_type + '__gt'
            promo = PromoSubscription.objects.filter(
                contact__imsi__exact=imsi,
                promo__promo_type=promo_type,
                **{
                    query: 0
                }).order_by('date_expiration')
            member = GroupMembers.objects.filter(
                group__owner__imsi__exact=imsi, user__callerid__exact=dest)

            if promo and member:
                key = 'promo[0].%s' % service_type
                ret = str(eval(key))
                # else, regular tariffs from above will apply

        return Response(ret, status=status.HTTP_200_OK)