コード例 #1
0
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    phone = Phone.normalize_number(self.request.get('phone'))
    message = self.request.get('message')

    if not (message and phone):
      result = { 'result': 'error',
                 'message': 'missing phone and/or message' }
      self.response.out.write(json.dumps(result))
      return

    sms_message = SmsMessage(phone_number=phone, 
                         message=message, 
                         direction='incoming',
                         status='unclaimed')
    objects = [ sms_message ]

    phone_entity = Phone.all().filter('number =', phone).get()
    if phone_entity:
      post = Post.fromText(message)
      post.unique_id = Post.gen_unique_key()
      post.user = phone_entity.user
      objects.append(post)

      sms_message.status = 'queued'
		
    db.put(objects)

    #self.response.out.write(message)
    self.response.out.write(json.dumps({'result': 'ok'}))
コード例 #2
0
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    phone = Phone.normalize_number(self.request.get('phone'))
    message = self.request.get('message')

    if not (message and phone):
      result = { 'result': 'error',
                 'message': 'missing phone and/or message' }
      self.response.out.write(json.dumps(result))
      return

    sms_message = SmsMessage(phone_number=phone, 
                         message=message, 
                         direction='incoming',
                         status='unclaimed')
    objects = [ sms_message ]

    phone_entity = Phone.all().filter('number =', phone).get()

    if not phone_entity:
      db.put(objects)
      self.response.out.write(json.dumps({'result': 'ok'}))
      return

    post = Post.fromText(message)
    post.unique_id = Post.gen_unique_key()
    post.user = phone_entity.user
    objects.append(post)
    sms_message.status = 'queued'

    db.put(objects)

    imok_user = ImokUser.all().filter('account =', phone_entity.user).get()
    email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

    for email in email_query:
      template_data = {
        'message': post.message,
        'link': post.permalink(self.request.host_url),
        'unsubscribe_link': email.permalink(self.request.host_url),
        'user': imok_user
        }
      body = template.render(s.template_path('email.txt'), template_data)
      mail.send_mail(sender='*****@*****.**',
                     to=email.emailAddress,
                     subject="I'm OK",
                     body=body)

    sms_message.status = 'processed'

    response_sms = SmsMessage(phone_number=phone,
                              message="I'm OK: Message received. %d contact(s) notified." % email_query.count(),
                              direction="outgoing",
                              status="queued")

    db.put([ response_sms, sms_message ])

    #self.response.out.write(message)
    self.response.out.write(json.dumps({'result': 'ok'}))
コード例 #3
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}))
コード例 #4
0
    def post(self):
        self.response.headers['Content-Type'] = 'text/plain'

        util = twilio.Utils(s.TWILIO_ACCOUNT_ID, s.TWILIO_AUTH_TOKEN)
        if not util.validateRequest(
                self.request.url, self.request.POST,
                self.request.headers['X-Twilio-Signature']):
            logging.error("Invalid request. Probably not Twilio.")

        sms_sid = self.request.get('SmsSid')
        phoneNumber = Phone.normalize_number(self.request.get('From'))
        text = self.request.get('Body')

        if not (text and phoneNumber):
            logging.error('WTF. No phoneNumber or message text.')
            return

        smsMessage = SmsMessage(phone_number=phoneNumber,
                                message=text,
                                direction='incoming',
                                twilio_sid=sms_sid,
                                status='unclaimed')

        phone_entity = Phone.all().filter('number =', phoneNumber).get()

        if not phone_entity:
            db.put([smsMessage])
            #self.response.out.write(json.dumps({'result': 'ok'}))
            return

        user = phone_entity.user
        self.savePostAndPush(text, phoneNumber, user, smsMessage)
コード例 #5
0
def sendSms(phone, message):
    sms_message = SmsMessage(phone_number=phone.number,
                             message=message,
                             provider=s.SMS_PROVIDER,
                             direction='outgoing',
                             status='queued')
    sms.sendSms(sms_message)
コード例 #6
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()
コード例 #7
0
  def post(self):
    self.response.headers['Content-Type'] = 'text/json'

    phone = Phone.normalize_number(self.request.get('phone'))
    message = self.request.get('message')

    if not (message and phone):
      result = { 'result': 'error',
                 'message': 'missing phone and/or message' }
      self.response.out.write(json.dumps(result))
      return

    sms_message = SmsMessage(phone_number=phone, 
                         message=message, 
                         direction='incoming',
                         status='unclaimed')
    objects = [ sms_message ]

    phone_entity = Phone.all().filter('number =', phone).get()

    if not phone_entity:
      db.put(objects)
      self.response.out.write(json.dumps({'result': 'ok'}))
      return

    post = Post.fromText(message)
    post.unique_id = Post.gen_unique_key()
    post.user = phone_entity.user
    objects.append(post)
    sms_message.status = 'queued'

    db.put(objects)

    imok_user = ImokUser.all().filter('account =', phone_entity.user).get()
    email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

    for email in email_query:
      template_data = {
        'message': post.message,
        'link': post.permalink(self.request.host_url),
        'unsubscribe_link': email.permalink(self.request.host_url),
        'user': imok_user
        }
      body = template.render(s.template_path('email.txt'), template_data)
      mail.send_mail(sender=s.MAILER_EMAIL,
                     to=email.emailAddress,
                     subject="IMOk status",
                     body=body)

    sms_message.status = 'processed'

#     Response_sms = SmsMessage(phone_number=phone,
#                               message="IMOk: Message received, %d contact(s) notified." % email_query.count(),
#                               direction="outgoing",
#                               status="queued")

    sendSms(phone=phone_entity, message="IMOk: Message received, %d contact(s) notified." % email_query.count())
#    db.put([ response_sms, sms_message ])
    db.put([sms_message])
    
    #self.response.out.write(message)
    self.response.out.write(json.dumps({'result': 'ok'}))
コード例 #8
0
    def savePostAndPush(self,
                        text,
                        phoneNumber,
                        user,
                        smsMessage=None,
                        bogusTimestamp=None):
        post = Post.fromText(text)
        if bogusTimestamp != None:
            post.datetime = bogusTimestamp
        post.unique_id = Post.gen_unique_key()
        post.user = user
        objects = [post]
        if smsMessage != None:
            smsMessage.status = 'queued'
            objects.append(smsMessage)
        db.put(objects)

        ######################################################################
        # send email

        # FIX: this block of code does not belong in sms_twilio

        imok_user = ImokUser.getProfileForUser(user)
        email_query = RegisteredEmail.all().filter('userName ='******'emailAddress')

        debugOutput = []
        for email in email_query:
            template_data = {
                'message': post.message,
                'link': post.permalink(self.request.host_url),
                'unsubscribe_link': email.permalink(self.request.host_url),
                'user': imok_user
            }
            body = template.render(s.template_path('email.txt'), template_data)
            debugOutput.append(body)
            mail.send_mail(sender=s.MAILER_EMAIL,
                           to=email.emailAddress,
                           subject="IMOk status",
                           body=body)

        twitter_post(imok_user, post.message)

        ######################################################################
        # post to person finder
        if 0:
            debugText = personFinder.postToPersonFinder(post)
            debugOutput.append(debugText)

        ######################################################################
        # send confirmation SMS
        if smsMessage != None:
            smsMessage.status = 'processed'
            smsMessage.put()

            response_sms = SmsMessage(
                phone_number=phoneNumber,
                message="I'm OK: Message received, %d contact(s) notified." %
                email_query.count(),
                direction="outgoing",
                status="queued")
            #response_sms.put()
            sendSms(response_sms)

        #self.response.out.write(message)
        #self.response.out.write(json.dumps({'result': 'ok'}))
        return debugOutput