def handle(self, msg): """ Handles messages that match the forwarding rules in this app. """ msg_parts = msg.text.split() rules = self._forwarding_rules() if not msg_parts: return False keyword = msg_parts[0].lower() if keyword not in rules: self.debug(u"{0} keyword not found in rules".format(keyword)) return False rule = rules[keyword] contact = msg.connection.contact if not contact or not rule.source.contacts.filter(pk=contact.pk).exists(): msg.respond(self.not_registered) return True now = get_now() msg_text = [rule.message, u" ".join(msg_parts[1:])] msg_text = [m for m in msg_text if m] msg_text = u" ".join(msg_text) full_msg = u"From {name} ({number}): {body}".format( name=contact.name, number=msg.connection.identity, body=msg_text ) broadcast = Broadcast.objects.create( date_created=now, date=now, schedule_frequency="one-time", body=full_msg, forward=rule ) broadcast.groups.add(rule.dest) msg.respond(self.thank_you)
def send_queued_messages(): """ Send messages which have been queued for delivery. """ messages = BroadcastMessage.objects.filter(status="queued")[:50] logger.info("Found {0} message(s) to send".format(messages.count())) for message in messages: connection = message.recipient.default_connection try: msg = send(message.broadcast.body, connection)[0] except Exception, e: msg = None logger.exception(e) if msg: logger.debug("Message sent successfully!") message.status = "sent" message.date_sent = get_now() else: logger.debug("Message failed to send.") message.status = "error" message.save()