def sms(): """All inbound sms comes to this function""" # (other functions in this file handle voice) from_number = request.values.get("From") to_number = request.values.get("To") p_from = phonenumbers.parse(from_number) from_user = User.get_user_from_phone_number( str(p_from.country_code), str(p_from.national_number)) if from_user: current_app.logger.info( "Incoming SMS from %s to Staffjoy number %s by user %s" % (from_number, to_number, from_user)) else: current_app.logger.info( "Incoming SMS from %s to Staffjoy number %s (unknown user) " % (from_number, to_number)) # Get a response # CURRENTLY - we don't support incoming sms and just send a generic message. # IN THE FUTURE - we may want to process message, in which case we would send the message body for processing. if from_user: # Greet the user by name message = "Hi %s - email [email protected] for support. Manage which notifications you receive in \"My Account\"." % ( from_user.name or "there") else: message = "You have reached Staffjoy.com - Reply STOP to unsubscribe." send_sms(str(p_from.country_code), str(p_from.national_number), message) return "<Response></Response>" # Twilio null response
def set_phone_number(self, phone_country_code, phone_national_number): """Send a verification pin for a new phone number""" if not self.active: raise Exception( "Cannot confirm a phone number for an inactive user") if self.phone_number: # This prevents race conditions raise Exception( "Remove existing phone number before setting a new one") # Filter to pure digits phone_country_code = ''.join(i for i in phone_country_code if i.isdigit()) phone_national_number = ''.join(i for i in phone_national_number if i.isdigit()) # First, verify that it's a valid phone number format p = phonenumbers.parse("+" + str(phone_country_code) + str(phone_national_number)) if not phonenumbers.is_possible_number(p): raise Exception("Invalid phone number") # check if we registered this number with somebody else. if User.get_user_from_phone_number(phone_country_code, phone_national_number): current_app.logger.info( "Unable to verify user %s (%s) phone number because it already belongs to another user" % (self.id, self.email)) raise Exception("Phone number already belongs to a user") current_app.logger.info( "User %s (%s) is attempting to verify phone number %s" % (self.id, self.email, phonenumbers.format_number(p, phonenumbers.PhoneNumberFormat.E164))) # At this point, we think we can send a message to the number # Generate a verification pin # (comes as a string due to leading zeros) verification_pin = ''.join( random.choice(string.digits) for x in range(self.SMS_VERIFICATION_PIN_LENGTH)) # Set cache (before sms) PhoneVerificationCache.set( self.id, { "verification_pin": verification_pin, "phone_country_code": phone_country_code, "phone_national_number": phone_national_number, }) # Send verification pin message = "Hi %s! Your Staffjoy verification pin is %s" % ( self.first_name, verification_pin) send_sms(phone_country_code, phone_national_number, message)
def getcode(request): if request.method == 'POST': num = randint(100000, 999999) request.session['smscode'] = str(num) request.session.set_expiry(300) phone = request.POST.get('phone') send_sms(phone, {'code': str(num)}, **SMSCONFIG) return JsonResponse({'code': 1, 'msg': 'ok'}) return redirect(reverse('app:index'))
def send_sms(self, message): """Send an SMS to the user""" if not self.active: # Stop communications to inactive users return if not self.phone_country_code and self.phone_national_number: raise Exception("User lacks a verified phone number") current_app.logger.info("Sending sms to user %s : %s" % (self, message)) send_sms(self.phone_country_code, self.phone_national_number, message)
def add_user(carrier, phone_number): '''Add a new user to the database and send a confirmation code.''' if User.query.get(phone_number): return False confirm_code = randint(10000, 99999) user = User(carrier, phone_number, confirm_code) db.session.add(user) db.session.commit() msg = 'Your confirmation number is: ' + str(confirm_code) send_sms(user.carrier, user.phone_number, msg) return True
def send_scores(scores): '''Send scores to registered users.''' users = User.query.filter(confirmed=True) for user in users: send_sms(user.carrier, user.number, 'Broadcast Message') pass