示例#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/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)
示例#4
0
def getPhone(createIfNeeded=False):
    user = users.get_current_user()
    phones = (Phone.all().filter('user = ', user).fetch(1))
    if phones:
        phone = phones[0]
    else:
        if createIfNeeded:
            phone = Phone(user=user)
        else:
            phone = None
    return phone
示例#5
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'}))