def subscribe(sms_number): if not sms_number: return 'You sent nothing yo.' sms_number = filter(str.isdigit, str(sms_number)) subscriber, created = Subscriber.objects.get_or_create( sms_number=sms_number) texter = Texter() if not created: if subscriber.expired: # yay! a renewal subscriber.renew() subscriber.save() thought = get_todays_thought() try: texter.send_text(subscriber, thought.thought_text, thought.post_id) except TwilioRestException as e: subscriber.active = False subscriber.save() logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str( e.msg) + ')' except DuplicateTextException: # no prob, they already got todays message pass return 'Welcome back! Check your phone!' elif not subscriber.active: # technically they could be blacklisted, but i can't do anything about that return 'Did you reply STOP? Reply START and try again.' else: return 'You\'re already subscribed, yo.' try: message = "Cool! Welcome to ShowerTexts.com! You'll start receiving Shower Texts daily. " \ "Reply STOP at any time if you get sick of them. " \ "Your first one will follow..." texter.send_text(subscriber, message, 'initial') except TwilioRestException as e: logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str(e.msg) + ')' except DuplicateTextException: logging.warning('Duplicate welcome text.') thought = get_todays_thought() try: texter.send_text(subscriber, thought.thought_text, thought.post_id) except TwilioRestException as e: logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str(e.msg) + ')' except DuplicateTextException: logging.error('Duplicate initial thought. Shouldn\'t happen - odd.') return 'Cool! Check your phone!'
def send_todays_texts(self): ret = [] thought = get_todays_thought() for subscriber in Subscriber.objects.filter(active=True): row = {'to': subscriber, 'action': 'showertext'} message = thought.thought_text post_id = thought.post_id if subscriber.expired: row['action'] = 'expiration' subscriber.active = False subscriber.save() message = 'HOUSE KEEPING! I\'m clearing out old numbers to make room for more. If you like these, ' \ 'please resubscribe for free! http://www.showertexts.com' post_id = 'EXP-' + str(datetime.date.today()) try: self.send_text(subscriber, message, post_id) row['result'] = 'Success' except DuplicateTextException: row['result'] = 'Duplicate' except TwilioRestException as ex: row['result'] = 'Exception: ' + str(ex) logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(ex)) ret.append(row) return ret
def subscribe(sms_number): if not sms_number: return 'You sent nothing yo.' sms_number = filter(str.isdigit, str(sms_number)) subscriber, created = Subscriber.objects.get_or_create(sms_number=sms_number) texter = Texter() if not created: if subscriber.expired: # yay! a renewal subscriber.renew() subscriber.save() thought = get_todays_thought() try: texter.send_text(subscriber, thought.thought_text, thought.post_id) except TwilioRestException as e: subscriber.active = False subscriber.save() logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str(e.msg) + ')' except DuplicateTextException: # no prob, they already got todays message pass return 'Welcome back! Check your phone!' elif not subscriber.active: # technically they could be blacklisted, but i can't do anything about that return 'Did you reply STOP? Reply START and try again.' else: return 'You\'re already subscribed, yo.' try: message = "Cool! Welcome to ShowerTexts.com! You'll start receiving Shower Texts daily. " \ "Reply STOP at any time if you get sick of them. " \ "Your first one will follow..." texter.send_text(subscriber, message, 'initial') except TwilioRestException as e: logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str(e.msg) + ')' except DuplicateTextException: logging.warning('Duplicate welcome text.') thought = get_todays_thought() try: texter.send_text(subscriber, thought.thought_text, thought.post_id) except TwilioRestException as e: logging.error('Exception sending number to: ' + subscriber.sms_number + ' - ' + str(e)) return 'I couldn\'t send a text to that number! (' + str(e.msg) + ')' except DuplicateTextException: logging.error('Duplicate initial thought. Shouldn\'t happen - odd.') return 'Cool! Check your phone!'
def today(request): """ Returns todays selected post in plain text. Used for Alexa endpoint. """ ret = cache.get('todays_thought_text') if not ret: thought = get_todays_thought() ret = thought.thought_text cache.set('todays_thought_text', ret, 60 * 15) logging.warning('Uncached: ' + ret) else: logging.warning('Cached: ' + ret) return HttpResponse(ret, 'text/plain')
def alternate(request): """ Allows an admin to select an alternate post from today's front page. """ trigger_pass = request.GET.get('p', None) alt_id = request.GET.get('s', None) if alt_id: choose_alternate(alt_id) return HttpResponseRedirect('/today') return render( request, 'alternates.html', { 'today': get_todays_thought().thought_text, 'submissions': get_submissions(), 'trigger': trigger_pass, })