def send_notification_task(to, amount, balance, business_name): logging.basicConfig(filename='SMS.log', level=logging.DEBUG) gateway = AfricasTalkingGateway(settings.username, settings.apikey) message = " %s " % (amount, balance, business_name) try: recipients = gateway.sendMessage(to, message) for recipient in recipients: logging.info('number=%s;status=%s;messageId=%s;cost=%s' % (recipient['number'], recipient['status'], recipient['messageId'], recipient['cost'])) except AfricasTalkingGatewayException, e: logging.warning('SMS failed to send %s' % str(e))
def welcomeMessage(to): logging.basicConfig(filename='SMS.log', level=logging.DEBUG) gateway = AfricasTalkingGateway(settings.username, settings.apikey) message = "Welcome to target limited, to purchase send TARGET *amount*business_id# to 14014" try: recipients = gateway.sendMessage(to, message) for recipient in recipients: logging.info('number=%s;status=%s;messageId=%s;cost=%s' % (recipient['number'], recipient['status'], recipient['messageId'], recipient['cost'])) except AfricasTalkingGatewayException, e: logging.warning('Database setup completed %s' % str(e))
def info_send(sender_id, to, message, enqueue_=0): logger = get_logger(__name__).bind( ) # Specify the numbers that you want to send to in a comma-separated list # Please ensure you include the country code (+254 for Kenya in this case) # Create a new instance of our awesome gateway class gateway = AfricasTalkingGateway(settings.AFRICAS_TALKING_USERNAME, settings.AFRICAS_TALKING_API_KEY, "sandbox") # Any gateway errors will be captured by our custom Exception class below, # so wrap the call in a try-catch block response = [] try: # Thats it, hit send and we'll take care of the rest. from_ = sender_id to = ", ".join(to) recipients = gateway.sendMessage(to, message, from_=from_, enqueue_=enqueue_) for recipient in recipients: # Note that only the Status "Success" means the message was sent result = { 'to': recipient['number'], 'status': recipient['status'], 'messageId': recipient['messageId'], 'cost': recipient['cost'] } response.append(result) except AfricasTalkingGatewayException as e: error_msg = 'Encountered an error while sending: %s' % str(e) # result = { # 'to': recipient['number'], # 'status': error_msg, # 'messageId': recipient['messageId'], # } response.append(error_msg) logger.warning('error', error_msg=error_msg) return response
def send_sms(self, phone_number, message): # Specify your login credentials username = self._config_parser.get('africas_talking', 'username') apikey = self._config_parser.get('africas_talking', 'apikey') # Specify the numbers that you want to send to in a comma-separated list # Please ensure you include the country code (+254 for Kenya) to = phone_number # And of course we want our recipients to know what we really do # message = "I'm a lumberjack and it's ok, I sleep all night and I work all day" # Create a new instance of our awesome gateway class gateway = AfricasTalkingGateway(username, apikey) # ************************************************************************************* # NOTE: If connecting to the sandbox: # # 1. Use "sandbox" as the username # 2. Use the apiKey generated from your sandbox application # https://account.africastalking.com/apps/sandbox/settings/key # 3. Add the "sandbox" flag to the constructor # # gateway = AfricasTalkingGateway(username, apiKey, "sandbox"); # ************************************************************************************** # Any gateway errors will be captured by our custom Exception class below, # so wrap the call in a try-catch block try: # Thats it, hit send and we'll take care of the rest. results = gateway.sendMessage(to_=to, message_=message, from_=None) for recipient in results: # status is either "Success" or "error message" print('number=%s;status=%s;messageId=%s;cost=%s' % (recipient['number'], recipient['status'], recipient['messageId'], recipient['cost'])) except AfricasTalkingGatewayException as e: return ('Encountered an error while sending: %s' % str(e)) pass