Created
June 8, 2015 15:55
-
-
Save jvanlier/3685f9c748c0af6169d9 to your computer and use it in GitHub Desktop.
Messagebird implementatie hack & fly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import messagebird | |
class SmsException(Exception): pass | |
class SmsConnector: | |
api_key = 'xxx' | |
def __init__(self): | |
self.client = messagebird.Client(self.api_key) | |
def _handle_exception(self, e): | |
errstr = "" | |
for error in e.errors: | |
errstr += "Errorcode: {0}. Error desc: {1}. Param: {2}\n". \ | |
format(error.code, error.description, error.parameter) | |
raise SmsException(errstr) | |
def get_balance(self): | |
"""Check balance at MessageBird. Returns a string""" | |
try: | |
# Fetch the Balance object. | |
balance = self.client.balance() | |
balance_str = "Amount: {0}. Type: {1}. Payment: {2}". \ | |
format(balance.amount, balance.type, balance.payment) | |
return balance_str | |
except messagebird.client.ErrorException as e: | |
self._handle_exception(e) | |
def send_message(self, number_list, message,name): | |
try: | |
message = self.client.message_create( | |
name, | |
number_list, | |
message, | |
{ 'reference' : 'Foobar' } | |
) | |
except messagebird.client.ErrorException as e: | |
self._handle_exception(e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment