def handle_incoming(self, request, reply_using=None): request_dict = request.POST if request.method == 'POST' else request.GET # Check whether we've gotten a SendDateTime if 'SendDateTime' not in request_dict: return HttpResponse('') # Check whether we've already received this message if SMS.objects.filter(gateway_ref=request_dict['MessageID']).exists(): return HttpResponse('OK') # Parse and process message year, month, day, hour, minute, second, ms = list(map(int, findall(r'(\d+)', request_dict['SendDateTime']))) sms_dict = { 'sent': datetime(year, month, day, hour, minute, second), 'content': request_dict['MsgeContent'], 'sender': check_cell_phone_number(request_dict['SenderGSMNR']), 'to': request_dict['ShortCode'], 'operator': int(request_dict['Operator']), 'gateway_ref': request_dict['MessageID'], 'backend': self.get_slug(), } sms = SMS(**sms_dict) response = self.process_incoming(request, sms) # If necessary, send response SMS if response is not None: signature = get_account(reply_using)['reply_signature'] success = send([sms.sender], response, signature, using=reply_using) # Sending failed, queue SMS if not success: send_queued(sms.sender, response, signature, reply_using) return HttpResponse(response) return HttpResponse('OK')
def handle_incoming(self, request, reply_using=None): request_dict = request.POST if request.method == 'POST' else request.GET # Check whether we've got an id if 'id' not in request_dict: return HttpResponse('') # Check whether we've already received this message if SMS.objects.filter(gateway_ref=request_dict['id']).exists(): return HttpResponse('ACK/Jasmin') # Parse and process message sms_dict = { 'sent': datetime.now(), 'content': request_dict['content'], 'sender': check_cell_phone_number(request_dict['from']), 'to': request_dict['to'], 'gateway_ref': request_dict['id'], 'backend': self.get_slug(), } sms = SMS(**sms_dict) response = self.process_incoming(request, sms) # If necessary, send response SMS if response is not None: signature = get_account(reply_using)['reply_signature'] success = send([sms.sender], response, signature, using=reply_using) # Sending failed, queue SMS if not success: send_queued(sms.sender, response, signature, reply_using) return HttpResponse('ACK/Jasmin')
def process_smses(smsk, sms_id, account_slug): smsobj = SMS.objects.get(pk=sms_id) smsbackend = SMSBackend() racc = get_account(account_slug) response = smsbackend.process_incoming(None, smsobj) if response is not None: signature = racc['reply_signature'] # If an SMS account can receive but not send SMSes, # it can specify a preferred reply account reply_account = racc.get('reply_account', account_slug) success = send([smsobj.sender], response, signature, reply_account) if not success: send_queued(smsobj.sender, response, signature, reply_account) task_logger.debug('End processing incoming SMS key: %s', smsk)
def handle_incoming(self, request, reply_using=None): request_dict = request.POST if request.method == 'POST' else request.GET # Check whether we've gotten a SendDateTime if not 'SendDateTime' in request_dict: return HttpResponse('') # Check whether we've already received this message if SMS.objects.filter(gateway_ref=request_dict['MessageID']).exists(): return HttpResponse('OK') # Parse and process message year, month, day, hour, minute, second, ms = map( int, re.findall(r'(\d+)', request_dict['SendDateTime'])) sms_dict = { 'sent': datetime.datetime(year, month, day, hour, minute, second), 'content': request_dict['MsgeContent'], 'sender': check_cell_phone_number(request_dict['SenderGSMNR']), 'to': request_dict['ShortCode'], 'operator': int(request_dict['Operator']), 'gateway_ref': request_dict['MessageID'], 'backend': self.get_slug(), } sms = SMS(**sms_dict) response = self.process_incoming(request, sms) # If necessary, send response SMS if response is not None: signature = smsgateway.get_account(reply_using)['reply_signature'] success = smsgateway.send([sms.sender], response, signature, using=reply_using) # Sending failed, queue SMS if not success: smsgateway.send_queued(sms.sender, response, signature, reply_using) return HttpResponse(response) return HttpResponse('OK')
def recv_smses(account_slug='redistore'): def _(key): return '%s%s' % (racc['key_prefix'], key) count = 0 racc = get_account(account_slug) rpool = redis.ConnectionPool(host=racc['host'], port=racc['port'], db=racc['dbn']) rconn = redis.Redis(connection_pool=rpool) smsbackend = SMSBackend() logger.info("Processing incoming SMSes for %s", account_slug) while True: smsk = rconn.rpoplpush(_('inq'), _('mvne:inq')) if not rconn.llen(_('mvne:inq')): break count += 1 logger.debug("Processing incoming SMS key: %s", smsk) smsd = rconn.hgetall(smsk) if not smsd: logger.error("SMS key %r is empty", smsk) continue smsd['sent'] = datetime.datetime.strptime(smsd['sent'], inq_ts_fmt) smsd['backend'] = account_slug smsobj = SMS(**smsd) response = smsbackend.process_incoming(None, smsobj) if response is not None: signature = racc['reply_signature'] success = send([smsobj.sender], response, signature, account_slug) if not success: send_queued(smsobj.sender, response, signature, account_slug) if rconn.lrem(_('mvne:inq'), smsk, 1) == 0: logger.error("SMS key %r doesn't exist in %r", smsk, _('mvne:inq')) if not rconn.delete(smsk): logger.error("SMS Hash %r doesn't exist" % smsk) logger.debug("End processing incoming SMS key: %s", smsk) logger.info("End processing incoming SMSes for %s (%d processed)", account_slug, count)