The send_otp_via_sms() function sends the OTP code via SMS.īarring there are no validation or other issues, a message is flashed to the user with a success category and then a redirect is made to the validate endpoint. The argument is a predefined message format and set of instructions to speak the code in the Twilio Markup Language (TwiML), which at its core is an XML document with special tags defined by Twilio. Then a call is placed to the provided phone number. The split_code() function takes the OTP code and adds spaces between each digit in the code so that Twilio pronounces each number instead of pronouncing the whole word. In the send_otp_via_voice_call() function, the OTP code is passed as a string to the split_code() function. Once the OTP code has been successfully generated, a call is made to the send_otp_code() function which either routes the notification through a voice call or SMS depending on the channel that was specified by the user in the submitted form. This function handles making a HTTP POST request to the GenerateOTP API to generate the OTP code. The formatted phone number is then stored in the Flask user session, so that it can be recalled later when the OTP is validated.Ī call is then made to the make_otp_request() function. We expect the phone number in the incoming request to be in the canonical E.164 format, so we have to remove the leading plus “+” sign as the GenerateOTP doesn’t accept it as a valid character in the initiator_id field. In our case, we can use the phone number as the initiator_id. To generate the OTP code, the GenerateOTP API needs an initiator_id which serves as an identifier for the particular user the OTP belongs to. It’s always a best practice to do server side validation even if validation is still carried out by the client. In the generate() function, we now carry out the necessary checks to ensure the phone number and the channel fields are present in the request when the form is submitted. env file and add paste your Twilio credentials, along with the Twilio phone number associated with your account: At the root of the project’s directory, create a. We are going to need these values to authenticate with the Twilio service. On your Twilio Console, copy your Account SID and Auth Token. If you are using a trial Twilio account you will need to verify your own phone number with Twilio before it can receive calls or SMS messages from the Twilio number. You can add a phone number to your account in the Buy a Number page if you don’t already have one. This is the number from which the OTPs are going to be sent. The action attribute of the form is also set to an empty string.Įnter in your browser’s address bar to see this template.Īfter you sign up for an account on Twilio, head over to your Twilio Console and set up a Twilio phone number that can make voice calls as well as send SMS messages. This HTML file also extends the base.html layout and adds a form that allows the user input the OTP code that was received so that it can be validated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |