def deactivate_subscriptions(self, msg): try: sms = SMS() sub = Subscriber() cur = db_conn.cursor() cur.execute( 'SELECT msisdn FROM subscribers WHERE subscription_status = 0 AND authorized = 1' ) count = cur.rowcount if count > 0: self.logger.info('Found %d subscribers to be deactivated' % count) subscribers_list = cur.fetchall() db_conn.commit() for mysub in subscribers_list: self.logger.debug( 'Send SMS that account is deactivated to %s' % mysub[0]) sms.send(config['smsc'], mysub[0], msg) # disable subscriber try: sub.authorized(mysub[0], 0) except SubscriberException as e: raise SubscriptionException( 'PG_HLR error in deactivating subscription: %s' % e) else: db_conn.commit() self.logger.info('No subscribers need to be deactivate') except psycopg2.DatabaseError as e: raise SubscriptionException( 'PG_HLR error in checking subscriptions to deactivate: %s' % e)
def send_broadcast(self, request, text, btype, location): api_log.info( '%s - [POST] %s/send_broadcast Data: text:"%s" btype:[%s] location:"%s"', request.getHost().host, self.path, text, btype, location) try: sms = SMS() sms.send_broadcast(text, btype, location) data = {'status': 'success', 'error': ''} except SMSException as e: data = {'status': 'failed', 'error': str(e)} api_log.info(data) return data
def send_subscription_fee_notice(self, msg): # get all subscribers try: sub = Subscriber() subscribers_list = sub.get_all() except SubscriberException as e: raise SubscriptionException('%s' % e) sms = SMS() for mysub in subscribers_list: self.logger.debug("Send sms to %s %s" % (mysub[1], msg)) sms.send(config['smsc'], mysub[1], msg)
def send(self, request, source, destination, text): api_log.info( '%s - [POST] %s/send Data: source:"%s" destination:"%s text: %s"', request.getHost().host, self.path, source, destination, text) try: sms = SMS() sms.send(source, destination, text) data = {'status': 'success', 'error': ''} except Exception as e: api_log.info('SMS Exception: %s', e, exc_info=True) data = { 'status': 'failed', 'error': str(e) + ' ' + str(sys.exc_info()[1]) } api_log.info(data) return data
def add(self, msisdn, credit): sub = Subscriber() sms = SMS() try: mysub = sub.get(msisdn) current_balance = sub.get_balance(msisdn) except SubscriberException as e: raise CreditException(e) new_balance = Decimal(str(credit)) + Decimal(str(current_balance)) # update subscriber balance try: cur = db_conn.cursor() cur.execute( 'UPDATE subscribers SET balance=%(new_balance)s WHERE msisdn=%(msisdn)s', { 'new_balance': Decimal(str(new_balance)), 'msisdn': msisdn }) db_conn.commit() sms.send(config['smsc'], msisdn, sms_credit_added % (credit, new_balance)) except psycopg2.DatabaseError as e: db_conn.rollback() raise CreditException( 'PG_HLR error updating subscriber balance: %s' % e) # insert transaction into the credit history try: cur = db_conn.cursor() cur.execute( 'INSERT INTO credit_history(msisdn,previous_balance,current_balance,amount) VALUES(%s,%s,%s,%s)', (msisdn, current_balance, new_balance, credit)) except psycopg2.DatabaseError as e: db_conn.rollback() raise CreditException( 'PG_HLR error inserting invoice in the history: %s' % e) finally: db_conn.commit()
def receive(self, request, source, destination, charset, coding, text): try: sms = SMS() import threading thread = threading.Thread(target=sms.receive, args=(source, destination, text, charset, coding)) thread.daemon = True api_log.info('Starting thread for chat message to %s via %s', destination, request.getClientIP()) thread.start() data = {'status': 'success', 'error': ''} api_log.debug(data) return data except Exception as e: api_log.error("Chat handler exception %s", e, exc_info=True)
def send_subscription_fee_reminder(self, msg): try: subscribers_list = self.get_unpaid_subscriptions() except SubscriptionException as e: raise SubscribtionException( 'ERROR in getting unpaid subscriptions') sms = SMS() sub = Subscriber() for mysub in subscribers_list: package = sub.get_package(mysub[0]) self.logger.debug("Send sms to %s %s" % (mysub[0], msg)) sms.send(config['smsc'], mysub[0], msg) if package > 0: self.logger.info("Deactivate Package for %s", mysub[0]) sub.reset_package(mysub[0]) sms.send(config['smsc'], mysub[0], "Su paquete ha sido desactivado.")
def receive(self, request, source, destination, charset, coding, text, btext='', dr='', dcs=''): if btext == '': btext = text thex = binascii.hexlify(btext) api_log.info( '%s - [POST] %s Data: source:"%s" destination:"%s" charset:"%s"', request.getHost().host, self.path, source, destination, charset) api_log.debug( 'Data: source:"%s" destination:"%s" charset:"%s" coding: "%s" content: %s HexofBin: %s DR: %s DCS: %s', source, destination, charset, coding, text.decode(charset, 'replace'), thex, dr, dcs) sms = SMS() unicode_text = text.decode(charset, 'replace') if use_kannel == 'yes': # Kannel posts to: # post-url = "http://localhost:8085/sms?source=%p&destination=%P&charset=%C&coding=%c&text=%a&btext=%b&dr=%d&dcs=%O" # Kannel sends us GSM0338 but sets charset param to UTF-8 if coding == '0': try: unicode_text = sms.check_decode0338(btext) api_log.info('SMS Decoded from GSM 03.38') api_log.debug('Decoded text:"%s"', unicode_text) except Exception as ex: api_log.debug('Coding(%s), but: %s', coding, str(ex), exc_info=True) data = { 'status': 'failed', 'error': str(ex) + ' ' + str(sys.exc_info()[1]) } # It's probably utf-8 unicode_text = btext.decode('utf-8', 'replace') elif coding == '2' and charset == 'UTF-16BE': try: unicode_text = btext.decode('utf-16be') api_log.info('SMS decoded as UTF-16BE') api_log.debug('Decoded text: "%s"', text) except Exception as e: # Catch Everything, try to not actually LOSE messages! api_log.debug('Exception: %s', e, exc_info=True) # Some phones are sending multi part messages with different charsets. # Kannel concatenates and sends as UTF-16BE coding 2 try: api_log.info('Trying multi part trick') a = btext[:134] b = btext[134:] unicode_text = a.decode('utf-16be') + b.decode('utf8') except Exception as e: api_log.debug('Exception: %s', e, exc_info=True) unicode_text = btext.decode('utf-16be', 'replace') else: unicode_text = btext.decode('utf-8', 'replace') try: sms.receive(source, destination, unicode_text, charset, coding) data = {'status': 'success', 'error': ''} except SMSException as e: data = {'status': 'failed', 'error': str(e)} api_log.info(data) return data