import loggingimport urllibimport urllib2import jsonlogging.basicConfig(level=logging.INFO)NEXMO_API_KEY = '...'NEXMO_API_SECRET = '...'def send_sms(to, text): params = { 'api_key': NEXMO_API_KEY, 'api_secret': NEXMO_API_SECRET, 'to': to, 'from': 'NEXMO', 'text': text } url = 'https://rest.nexmo.com/sms/json?' + urllib.urlencode(params) request = urllib2.Request(url) request.add_header('Accept', 'application/json') response = urllib2.urlopen(request) if response.code == 200: data = response.read() decoded_response = json.loads(data.decode('utf-8')) messages = decoded_response["messages"] for message in messages: if message["status"] == "0" or message["status"] == 0: # {u'status': u'0', u'network': u'50212', u'remaining-balance': u'18.32500000', u'to': u'PHONE_NUMBER', u'message-price': u'0.03710000', u'message-id': u'0E0...'} logging.info("Send SMS success: {}".format(message)) return True else: logging.error("Send SMS fail: {0}, {1}".format(message["status"], url)) else: logging.error("Send SMS fail: {0}, {1}".format(response.code, url))
❤️ Is this article helpful?
Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.
Do send some 💖 to @d_luaz or share this article.
✨ By Desmond Lua
A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz
👶 Apps I built
Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.