def send(msg, delay=True, *args, **kwargs): """ Expected kwargs: messaging_token """ phone_number = msg.phone_number if phone_number[0] != "+": phone_number = "+" + phone_number params = urlencode({ "action" : "create" ,"token" : kwargs["messaging_token"] ,"numberToDial" : phone_number ,"msg" : msg.text ,"_send_sms" : "true" }) url = "https://api.tropo.com/1.0/sessions?%s" % params response = urlopen(url).read() msg.save() try: # attempt to bill client from hqbilling.tasks import bill_client_for_sms from hqbilling.models import TropoSMSBillable if delay: bill_client_for_sms.delay(TropoSMSBillable, msg.get_id, **dict(response=response)) else: bill_client_for_sms(TropoSMSBillable, msg.get_id, **dict(response=response)) except Exception as e: logging.debug("TROPO API contacted, errors in billing. Error: %s" % e) return response
def send(msg, delay=True): """ Sends a message via mach's API """ outgoing_sms_text = clean_outgoing_sms_text(msg.text) context = { 'message': outgoing_sms_text, 'phone_number': urllib.quote(msg.phone_number), } url = "%s?%s" % (settings.SMS_GATEWAY_URL, settings.SMS_GATEWAY_PARAMS % context) # just opening the url is enough to send the message # TODO, check response resp = urllib2.urlopen(url).read() msg.save() try: # attempt to bill client from hqbilling.tasks import bill_client_for_sms from hqbilling.models import MachSMSBillable if delay: bill_client_for_sms.delay(MachSMSBillable, msg.get_id, **dict(response=resp)) else: bill_client_for_sms(MachSMSBillable, msg.get_id, **dict(response=resp)) except Exception as e: logging.debug("MACH API contacted, errors in billing. Error: %s" % e) return resp
def create_billable_for_sms(msg, backend_api, delay=True, **kwargs): try: from hqbilling.tasks import bill_client_for_sms from hqbilling.models import API_TO_BILLABLE billable_class = API_TO_BILLABLE.get(backend_api) if delay: bill_client_for_sms.delay(billable_class, msg._id, **kwargs) else: bill_client_for_sms(billable_class, msg._id, **kwargs) except Exception as e: logging.error("%s backend contacted, but errors in creating billable for incoming message. Error: %s" % (backend_api, e))
def create_billable_for_sms(msg, backend_api, delay=True, **kwargs): try: from hqbilling.tasks import bill_client_for_sms from hqbilling.models import API_TO_BILLABLE msg.save() billable_class = API_TO_BILLABLE.get(backend_api) if delay: bill_client_for_sms.delay(billable_class, msg._id, **kwargs) else: bill_client_for_sms(billable_class, msg._id, **kwargs) from corehq.apps.sms.api import store_billable store_billable.delay(msg) except Exception as e: logging.error( "%s backend contacted, but errors in creating billable for incoming message. Error: %s" % (backend_api, e))
def create_from_request(request, delay=True): """ From an inbound request (representing an incoming message), create a message (log) object with the right fields populated. """ sender = request.REQUEST.get(InboundParams.SENDER, "") message = request.REQUEST.get(InboundParams.MESSAGE, "") timestamp = request.REQUEST.get(InboundParams.TIMESTAMP, "") # parse date or default to current utc time actual_timestamp = datetime.strptime(timestamp, DATE_FORMAT) \ if timestamp else datetime.utcnow() # not sure yet if this check is valid is_unicode = request.REQUEST.get(InboundParams.UDHI, "") == "1" if is_unicode: message = message.decode("hex").decode("utf_16_be") # if you don't have an exact match for either of these fields, save nothing domains = domains_for_phone(sender) domain = domains[0] if len(domains) == 1 else "" recipients = users_for_phone(sender) recipient = recipients[0].get_id if len(recipients) == 1 else "" log = SMSLog(couch_recipient=recipient, couch_recipient_doc_type="CouchUser", phone_number=sender, direction=INCOMING, date=actual_timestamp, text=message, domain=domain, backend_api=API_ID) log.save() try: # attempt to bill client from hqbilling.tasks import bill_client_for_sms from hqbilling.models import UnicelSMSBillable if delay: bill_client_for_sms.delay(UnicelSMSBillable, log._id) else: bill_client_for_sms(UnicelSMSBillable, log._id) except Exception as e: logging.debug("UNICEL API contacted, errors in billing. Error: %s" % e) return log
def send(message, delay=True): """ Send an outbound message using the Unicel API """ config = _config() phone_number = clean_phone_number(message.phone_number).replace("+", "") # these are shared regardless of API params = [(OutboundParams.DESTINATION, phone_number), (OutboundParams.USERNAME, config["username"]), (OutboundParams.PASSWORD, config["password"]), (OutboundParams.SENDER, config["sender"])] try: text = str(message.text) # it's ascii params.append((OutboundParams.MESSAGE, text)) except UnicodeEncodeError: params.extend(UNICODE_PARAMS) encoded = message.text.encode("utf_16_be").encode("hex").upper() params.append((OutboundParams.MESSAGE, encoded)) try: data = urlopen('%s?%s' % (OUTBOUND_URLBASE, urlencode(params))).read() except Exception: data = None message.save() try: # attempt to bill client from hqbilling.tasks import bill_client_for_sms from hqbilling.models import UnicelSMSBillable if delay: bill_client_for_sms.delay(UnicelSMSBillable, message.get_id, **dict(response=data)) else: bill_client_for_sms(UnicelSMSBillable, message.get_id, **dict(response=data)) except Exception as e: logging.debug("UNICEL API contacted, errors in billing. Error: %s" % e) return data