class Nexmo(object): """ Gateway for sending text messages and making phone calls using Nexmo_. You need your Nexmo account credentials, and a registered app account dashboard. ``NEXMO_API_KEY`` Should be set to your account's API key from the Dashboard. ``NEXMO_API_SECRET`` Should be set to your account's authorization token. ``NEXMO_CALLER_ID`` The name of the service to text through. .. _Nexmo: https://www.nexmo.com/ """ def __init__(self): self.client = Client(getattr(settings, 'NEXMO_API_KEY'), getattr(settings, 'NEXMO_API_SECRET')) # def make_call(self, device, token): def send_sms(self, device, token): body = ugettext('Your authentication token is %s') % token opts = {} opts['to'] = device.number.as_e164, opts['from'] = getattr(settings, 'NEXMO_CALLER_ID') or 'DjangoApp' opts['body'] = body self.client.send_message(opts)
def send_sms(sender: str, to: str, message: str, client: nexmo.Client) -> dict: """Sends an SMS. ``sender`` and ``to`` must be in proper long form. """ # Nexmo is apparently picky about + being in the sender. sender = sender.strip("+") resp = client.send_message({"from": sender, "to": to, "text": message}) # Nexmo client incorrectly treats failed messages as successful error_text = resp["messages"][0].get("error-text") if error_text: raise nexmo.ClientError(error_text) return resp
def send_sms(sender: str, to: str, message: str, client: nexmo.Client) -> dict: """Sends an SMS. ``sender`` and ``to`` must be in proper long form. """ # This has to be removed at some point. Sleep a little to avoid hitting rate limits. time.sleep(0.3) # Nexmo is apparently picky about + being in the sender. sender = sender.strip("+") logging.info(f"Sending from {sender} to {to} message length {len(message)}") resp = client.send_message({"from": sender, "to": to, "text": message}) # Nexmo client incorrectly treats failed messages as successful error_text = resp["messages"][0].get("error-text") if error_text: raise nexmo.ClientError(error_text) return resp