def post(self): q_id = int(self.request.get('q_id')) q = QueueEntry.get_by_id(q_id) twilio_settings = TwilioSettings.all().get() schedule = SMSSchedule.all().get() tclient = TwilioRestClient(twilio_settings.account_sid, twilio_settings.auth_token) if q and q.status == 'P': employees = Employee.all() for employee in employees: entry = ReportingEntry.all().filter('reporting_date = ', q.report_date).filter('mobile_number =', employee.mobile_number).get() if not entry: entry = ReportingEntry(name = employee.name, mobile_number = employee.mobile_number, email = employee.email, reporting_date = q.report_date, reporting_location = "NOT REPORTED", reporting_time = None, employee = employee) entry.put() if schedule.status=='Active': tclient.sms.messages.create(to = employee.mobile_number, from_ = twilio_settings.twilio_number, body = schedule.message[:160]) smslog = SMSLog(message = schedule.message[:160], from_number = twilio_settings.twilio_number, to_number = employee.mobile_number, direction = 'outgoing-scheduled', employee_name = employee.name) smslog.put() q.status = 'S' q.save()
def get(self): fromNumber = self.request.get('From') to = self.request.get('To') body = self.request.get('Body') if fromNumber != '': fromNumber = fromNumber[-10:] smslog_incoming = SMSLog(message = body, from_number = fromNumber, to_number = to, direction = 'incoming', employee_name = '** NOT AN EMPLOYEE') employee = Employee.all().filter('mobile_number =', fromNumber).get() utc = timezone.UTC() est = timezone.Eastern_tzinfo() now = datetime.now() in_est = now.replace(tzinfo=utc).astimezone(est) schedule = SMSSchedule.all().get() if employee: smslog_incoming.employee_name = employee.name smslog_incoming.put() entry = ReportingEntry.all().filter('reporting_date = ', in_est.date()).filter('mobile_number =', employee.mobile_number).get() if not entry: entry = ReportingEntry(name = employee.name, mobile_number = employee.mobile_number, email = employee.email, reporting_date = in_est.date(), reporting_location = body, employee = employee) entry.put() else: entry.reporting_location = body entry.save() logging.debug(" From: %s"%fromNumber) if schedule and schedule.auto_reply_enabled=='Y' and schedule.auto_reply!=None and schedule.auto_reply!='': r = twiml.Response() r.sms(schedule.auto_reply[:160]) smslog = SMSLog(message = schedule.auto_reply[:160], from_number = to, to_number = fromNumber, direction = 'outgoing-reply', employee_name = employee.name) smslog.put() self.response.out.write(r)