def send(self, user, client_id, message): from api.events.models import Event if message[u'type'] not in (u'invite', u'comment'): return False smsee = SMSRegister.get({u'event': message[u'event_id'], u'contact_number': client_id}) if smsee is None: return False event = Event.get(message[u'event_id']) if event is None: return False account = twilio.Account(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) texts = list() if message[u'type'] == u'invite': tz = smsee[u'tz'] or 'America/Toronto' t2 = ('%s shared a plan with you on ' 'Connectsy. ' % event[u'creator']) if event[u'where'] is not None: t2 += 'Where: %s' % event[u'where'] if event[u'when'] is not None and event[u'when'] != 0: t2 += ", " else: t2 += ". " if event[u'when'] is not None and event[u'when'] != 0: t2 += 'When: %s. ' % format_date(event[u'when'], tz=tz) t2 += 'Reply to send a comment, include #in to join.' texts.append(t2) texts.append('What\'s the plan: %(what)s' % {'username': event[u'creator'], 'what': event[u'what']}) elif message[u'type'] == u'comment': texts.append('%(commenter)s commented: %(comment)s' % message) if len(texts[-1]) > 160: texts[-1] = texts[-1][:157]+"..." try: for text in texts: account.request(SMS_OUTPUT_URL, 'POST', { 'To': smsee[u'contact_number'], 'Body': text, 'From': smsee[u'twilio_number'], }) return True except HTTPError, e: print e.read() return False
def test_sms_registration(self): number = '+15555555555' event_id = uuid.uuid1().hex SMSRegister(contact_number=number, twilio_number='+15555555555', event=event_id, expires=timestamp(), user='******').save() smsee = SMSRegister.get({u'event': event_id, u'contact_number': number}) self.assertNotEqual(smsee, None)
def register(event, contacts, tz=None): # late import to User model can use phone number normalizer from api.users.models import User if not u"id" in event: event[u"id"] = str(event[u"_id"]) registered = list() out_of_numbers = list() has_username = list() for contact in contacts: contact[u"number"] = contact[u"number"] user = User.get({u"number": contact[u"number"]}) if user is None: user = User(number=contact[u"number"], display_name=contact[u"name"]) user.save() # Connectsy users don't get SMS if user[u"username"] is not None: has_username.append(user) continue # make sure this user still has numbers available potential_numbers = [n for n in settings.TWILIO_NUMBERS] for reg in SMSRegister.find({u"contact_number": contact[u"number"]}): if from_timestamp(reg[u"expires"]) > datetime.now(pytz.utc): potential_numbers = [n for n in potential_numbers if n != reg[u"twilio_number"]] if len(potential_numbers) == 0: out_of_numbers.append(contact) continue if tz is None: tz = "America/Toronto" if event[u"when"]: expires = event[u"when"] else: print "created", event[u"created"] expires = timestamp(dt=from_timestamp(event[u"created"]) + timedelta(days=2)) registered.append(user) r = SMSRegister( contact_number=contact[u"number"], twilio_number=potential_numbers[0], event=event[u"id"], expires=expires, user=user[u"id"], tz=tz, ) r.save() # register with the notifications system as well note = { u"user": user[u"id"], u"timestamp": timestamp(), u"client_type": "SMS", u"client_id": r[u"contact_number"], } NotificationRegister(**note).save() return registered, out_of_numbers, has_username