Esempio n. 1
0
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    # Handle any messages the phone sent
    sent_messages_str = self.request.get('messages', '[]')
    sent_messages = json.loads(sent_messages_str)

    try:
      messages = db.get(sent_messages)
      def deliver(m):
        m.status = 'delivered'
        return m
      map(deliver, messages)
      db.put(messages)
    except db.BadKeyError:
      # TODO: Do something
      pass

    # Send them any new messages
    messages = SmsMessage.all().filter('status =', 'queued').filter('direction =', 'outgoing').fetch(100)

    def modify(m):
      m.status = 'sent'
      return { 'id': str(m.key()), 'message': m.message, 'phone': m.phone_number }
    send_messages = map(modify, messages)

    db.put(messages)

    self.response.headers['Content-Type'] = 'text/json'
    self.response.out.write(json.dumps({'result': 'ok', 'messages': send_messages}))
Esempio n. 2
0
    def post(self):
        sms_sid = self.request.get('SmsSid', '')
        sms_status = self.request.get('SmsStatus', '')

        if sms_sid == '' or sms_status == '':
            logging.error('No SmsSid or SmsStatus!')
            return

        sms_message = SmsMessage.all().filter('twilio_sid =', sms_sid).get()
        if not sms_message:
            logging.error('No SmsMessage for the given SmsSid')
            return

        if sms_status == 'sent':
            sms_message.status = 'delivered'
        elif sms_status == 'failed':
            logging.error('SMS failed to send: %s' % sms_sid)
            sms_message.status = 'failed'
        else:
            logging.warn('Unknown status: %s' % sms_status)
            return

        sms_message.put()