Spring Boot: How to send sms using Twilio API (2019)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi welcome to amigos code in this video I'm going to show you exactly how to use Tulio and spring booth to send SMSs so basically we're gonna do something like this so basically you can see that we can send SMS is using code and Twilio allows us to do that so in this video I'm going to show you exactly how to use their API to build an application where you're gonna be able to expose an end point and have clients sending a request where they want to send an SMS in the actual payload clients have to specify the destination number as well as the message without further ado let's dive into it alright so let's go ahead and integrate video with spring book so Twilio basically allows you to send SMS 'as video whatsapp and basically you can see right here so they give you the ability to send SMS is programmable voices video Twitter API for whatsapp and all the things but basically when sometimes you get these automated sms's then most likely Twilio is being used to send those sms's and in this video you're going to learn how to build an app that does that so go ahead and navigate to twitter.com and basically we can get a free api that we can use for testing purposes so go ahead to twitter.com and then go ahead to sign up and then go ahead and pretty much just enter your name and last name so I'm going to say amigos and then code and then test and the email is amigos code and then test at gmail.com go ahead and pick a password and then get started and there we go so now we have to verify our phone number actually our identity by using the phone number and make sure that you use your real phone number because otherwise it won't work so we need to verify our identity to be able to send and receive messages using the test API so I'm going to go ahead and add my phone number and you can see that you can pick your country code as well as the actual phone number so I'm going to go ahead and stick with UK and then the phone number will be there we go so obviously you can't see my phone number but go ahead and add your phone number and right here you can see that I'm actually recording my screen so you see that we will get an SMS very soon so now I'm gonna click on this checkbox and then verify there we go so now you can see that I did get an SMS so I'm gonna open that up and there we go so you can see my verification code someone into that zero four and then six five Submit and there you go so now we have to answer you know these questions so do you write code go ahead and say yes or you can skip directly to the dashboard so that's I'm gonna do I'm gonna skip so basically don't gather you know information about you basically and there we go so just give a second it's loading and tada so now we are inside of the Twilio dashboard and if I remove that you can see that we have a trial balance of fifteen dollars and fifty cents and then we have the account set plus the auth token and this is what we're going to use to connect to Twitter's API in a second so we also need to get a trial number and this is the number that it is used as the sender so go ahead and get this trial number and it's going to run them channel number and you can see the number right here and if you don't like this number go ahead and search for a different number so basically this number right here so you can see that it says this number can receive in wrinkles and make outgoing and also can send and receive text messages from mobile numbers so go ahead and choose this number and now we have to fill address information so go ahead and fill yours and I said so now we have our phone number right here so you can see that this is the phone number and you can see that they also are telling us that trial accounts are allowed only one Twilio number to purchase additional numbers please upgrade your account so this is more than enough for testing purposes now I'm going to go ahead and press done and this is everything that we have to do in order to configure Twilio so now we need to write a spring bull application that uses this trial number the account s ID and then the auth token so basically using these three pieces of information we can then write some code that will trigger some sms's so go ahead and navigate to a spring initializer and this website allows you to bootstrap your application so we're going to generate a project we'd maven so right here maven and then Java but you can go ahead and be cuddling groovy and then the spring book version make sure it's anything above 2.0 so go ahead and try and pick the same version if you see otherwise just stick with a higher version because it will also work now go ahead and leave this information right here so the project metadata as it is and click on the switch to the full version link and basically right here I'm gonna be changing the Java version to 11 so I'm using jar 11 but if you're using Java 8 this will also work so now we need to do is speak some dependencies so right here you can see that there is a huge list of dependencies but the one for this video will be the web so this allows us to write full stack web applications with Tomcat and spring MVC so now go ahead and generate project and if you want to learn more about spring book go ahead and check my channel for a spring boot tutorial so I'm gonna open this demo zip folder and it's in my desktop so I'm gonna unzip this and there we go I'm gonna delete this folder and there we go so now we have this demo application right here so let me actually call it demo and then Twilio and for this video I'm going to be using IntelliJ which is by far the best IDE and if you've never used IntelliJ go ahead and check also my video on how to download install and configure IntelliJ for Java developers so go ahead and open and also so if you are using any other IDE this will also work so go ahead and open up the actual folder so inside of this demo Twilio there is a file called Palm D'Or XML so this is what we need so I'm going to open that and then open a project and there we go so you can see that maven is doing this thing to resolve the dependencies and that's done so now you can see that we have this folder called demo if you open that up you see that we have this Palm D'Or XML and basically this is the dependency that we've picked from the spring initialize right here so spring boot start a web and also by default it comes with this start to test for writing integration tests so now let's go ahead and add the Twilio dependency into our project so go ahead and open up any web browser and go ahead to Twilio comm dogs libraries and then java and right here you can see that they have this Java helper library and this is a Java client that allows us to automate sms's so right here you can see that they have the maven dependency so if I called so you can see that we can grab this dependency right here so the version for me is 7.30 4.0 and make sure to pick the same version as me to make sure you complete this tutorial without any problems so go ahead and grab the actual dependency go back to IntelliJ and then after the spring boot started test-based are in just give me a second enable order imports and now IntelliJ is resolving these dependencies and there we go so now we have the actual Twilio dependency and we can use some of the classes provided by their api now let's go ahead and write our application that allows clients to send a post request to our server and then pretty much send an SMS to a given phone number so let's go ahead and open up source and then inside of source open up main and then inside of main open up resources so right here go ahead and delete this applications dot property and let's go ahead and create a new file and by the way I'm using presentation assistant and this allows us to see all the keyboard shortcuts that I'm actually typing because when I use IntelliJ I don't don't use the actual mouse quite a lot so I know all the keyboard shortcuts and this allows me to be very fast and efficient when writing code and I do recommend you to learn the keyboard shortcuts for long term benefit so let's go ahead and create a file called application dot llamo and basically we could have used the applications of property by I prefer the e llamo syntax so now inside of this applications or mo let's go ahead and define Twilio property files so first we need to type Trulia so this will be the actual root and then right here we're gonna have the account and then underscore SID we also will have the auth token so off and then token and then we will also have the send or actually let's go back to Twilio so go back to the actual console and then you can see that we have this trial number so let's call it trial number and in fact just let me grab that here come and see go back to IntelliJ this will be trial and then underscore number and now I'm gonna paste that in let me go ahead and do the same for account CID so I'm gonna copy that go back to IntelliJ paste that in with a space there and then we need the auth token so go back to Twilio and then copy go back to IntelliJ and then paste that in so this is everything that we need to have for configuration now let's go ahead and create a class that reads these properties and allows us to inject them in other classes and use these properties so go ahead and open up Java and inside of Java you have come to example demo and then inside you have this demo application so right here so this is the main method and this is how spring both applications get started so now go ahead and create a new class and let's go ahead and create a new class so this will be a class and call it Twilio configuration and then okay now let's go ahead and annotate this class without configuration so springboard knows that this needs to be instantiated as a bean for dependency injection we also need to say add and then configuration properties and this guy takes a prefix and the prefix is Twilio so I'm gonna explain this in a second what we doing so basically this tweety right here is this one right here so this one so now what we need to do is to create three fields that Springbok will use to get these properties right here into our class so these have to be private and we can't simply say final because this class needs some setters for setting the values from the config file so let's go ahead and save private and then string account and that's it so right here because the seed property so this guy right here is underscore we have to have camelcase right account and then SID so then we also have private and then string off and then token and we also have private string and then trial number there we go all right so now let's go ahead and create an empty constructor so public and then Twilio configuration so this will be empty constructor and let's go ahead and create some getters and setters so I'm gonna press control enter and then get and setters select all of those and there we go so basically this class needs to have setters so these setters so that these values right here so the application Damaso accounts it so this value will be passed inside of this field right here by using this setter the same for the auth token as well as the trial number so now let's go ahead and use this configuration file and initialize our connection to the Twitter API so let's go ahead and create a new class and we're going to call this as Twilio initializer and then enter and this will also be at configuration so add and then configuration and what we need is to inject the Twitter configuration class so private final and then string actually so not string but Twilio configuration and then I'm gonna press option and then enter add to constructor and or to wire so basically this is using dependency injection so if you not sure about the pence injection go ahead and check my video on dependency injection where I explain the benefits of using it so now let's go ahead and write after we pretty much just inject the Tweedy configuration let's go ahead and say Twilio and right here we're going to say in it so this is how we initialize the connection to Twilio api so go ahead and say in it and inside you can see that it takes a username and a password and both must not be no so if I go back to to Twilio docks so right here so in this helper if I scroll down you can see that this is how we initialize so we have to say Twilio in it and then the username is the account said and then the password is the auth token so let's go ahead and do that now so go back to IntelliJ and now the accounts it will come from the property file so Twilio configuration dot account said and then the password will be Twilio configuration don't get off token so also let's go ahead and add a logger so private final and then static and then this will be logger and make sure that you use the org dot SLA f4j so I'm going to pick that one and then logger equals two and then logger factory and then dot and then get logger and then twiddle class or actually truly initialize it'll class and right here simply go ahead and have a log message so logger dot in an info and then let's simply say that Twilio initialized and then dot dot with account SID and then past the account said so go ahead and pretty much to say Twilio and in configuration don't get accounts it and basically I'm only doing this so that you can see that we are getting the correct value by in essence you'd never log your API credentials and this is pretty much all we need to do for this class and I've just spotted that I've got a typo there so this should be initializer and there we go so now let's go ahead and open up demo application and try and run the app to make sure that we are connecting through Twitter's API successfully so go ahead and play so run them application give it a second and there we go so now we should see our log message and fair enough so Twilio right here initialize with account said and this is the account set right here so ending in B E and if I go to Twilio and then if I show you again my account SID so this is the one so we successfully connected to Twitter's API now let's go ahead and write the logic to use the API to send some sms's so what I'm going to do is go back to IntelliJ and I'm gonna stop the application for now close this and let's go ahead and premature define an interface that will be used to send SMS s so go ahead and say SMS and then sender and change this from a class to an interface and this will have one method void send SMS and this will take an SMS and then request then let's call this SMS request so basically we don't have this class so let's go ahead and create this class so I'm gonna press option and then enter create class and then okay and there we go so now if someone wants to send a request so now so if someone wants to send an SMS they have to provide the phone number so private final and then string phone and the number and let's also have the actual message so string and then message so this will be the destination phone number and this is the actual message so let me go ahead and add these two constructor and also had some getters so control enter and then getters and there we go and also we can go ahead and add two strings so to shrink and then okay and now if I go back to our interface so right here so you can see that the arrow went way and we have this one interface with this send SMS that allows any implementation to send an SMS so let's go ahead and create the Twilio implementation of this and the reason why I do have an interface is because you can then switch providers without changing code and basically all you have to do really is just change one line and then you can switch between implementations and I'm gonna show you this in a second what I mean so go ahead and create another class and this will be twiddle SMS and then sender and then this guy implements SMS and then sender there we go so now we have to implement the method send SMS and there we go so basically this class right here so we have to allocate with ADD and then service so that spring initializes this class and now we need to have a way to send SMS s so to send an SMS with Twilio API we have to use this message and make sure that it's this one right here so Twilio the rest api v mm dot account and then dot and then creator so the creator method takes couple arguments but the one that we are interested in is the second one where we pass the two phone number and from from number and then the actual body which is the message so let's go ahead and define these so new and then phone number and then the phone number will come from SMS quest dot get phone number we also have to get the from number so new and then phone number and this we're gonna fill this in a second and then we also have the actual body so SMS request don't and then get message so now we have this message creator and if I Shrunk this to a variable so we have message creator like that and let me also extract this way variable so to that as well the from number and then the actual body so the message and if I put this in one line like so and now we need to grab the from phone number and remember inside of our configuration we have the trial phone number so let's go ahead and use that so private final and then Twilio configuration and we're going to inject this guy again so also wired and now we can say Twilio configuration dot and then get trial number so now we have this creator right here and what we need to do is simply say creator dot and then create and this pretty much sends the actual SMS so now obviously you could do some validation right here so basically we could go ahead and say you know if and then is phone number valid and then say SMS request get a phone number so if that's the case what we're going to do is send this otherwise we can pretty much just throw for example a custom HTTP response or throw an illegal argument exception so let's go ahead and throw an illegal argument exception so throw and the new illegal argument exception and then pass the message so phone number and then plus and then SMS request dot get phone number plus and then end that like that space is not a valid number just like that so if I pretty much just put this on a new line so you can see exactly what we're doing and there we go so now we need to create this guy right here so create this method that returns a boolean and for now let's simply go ahead and say that this returns true right so I'm gonna leave a to do right here for you to implement this and you could use for example Google's own library to validate phone numbers so to do implement phone number validate and there we go so also let me go ahead and have a log line right here so private static and then final loader and then logger equals through logger factory dot get logger and then let's also go ahead and say that logger don't info sent SMS and then plus and then what we're going to do is actually pass the SMS request so obviously we need to pass like that and there we go so now we have this service called Twilio SMS sender that is capable of sending SMSs so now what we need to do is actually create another service so this service so let's simply call it service and this will be that and then service and this will actually inject the Thule SMS service so inject Twilio SMS watch the SMS sender and I'm going to add two constructor Auto wire and now let's go ahead and say public avoid send and then SMS and then simply pass the SMS request and then simply invoke this will you SMS sender so Twilio SMS sender dot and then send SMS and then pass the actual request and there we go so now the last thing that we have to 3/8 is the actual HTTP resource or the actual controller so I'm going to go ahead and create a class and then call this as the controller and right here we have to annotate with at and then rest controller and then at request and then mapping and this will be API and then version 1 and then SMS and right here at let's have an endpoint that clients can hit and send SMS s so public and then void and then send SMS and we're going to receive the SMS request like that and then we need to pretty much just invoke our service so private oops private and then final and then service and then this will be both while again and what we need to do is simply invoke the send message so service dot and then send SMS SMS request and there we go so now there are a couple of things that we have to do and that is to make sure that this method is used as a post request we have to annotate with at and then post mapping and then we have to say that this SMS request we will get it from the request and then body and now I need to open up SMS request and then annotate these two fields inside of the constructor we add and then Jason property and basically these are the properties that the client will send so phone number and then add jason property and then this will be message and there we go so I'm gonna show you exactly how this works in a second so if I go back you can see that we get the request body and we can also validate the actual payload so I'm gonna go back to SMS request and I'm going to say that this phone number has to be not blank so ie if someone sends a blank value we're not allowing blank phone numbers to be sent from the controller so now this will be the same not blank and there we go so now to activate these go back to the actual controller and then before requests mapping say add and then valid there we go and now this make sure that when we get this SMS request both phone number and SMS the must not be blank otherwise it will blow so this is pretty much everything that we have to do and if I open up the Twilio SMS sender right here so right here you see that we have this truly SMS Center and it implements this SMS Center so let's go ahead and tag this guy as Twilio so to do and when we inject this SMS Center so inside of this service right here you have to use at and then qualifier and basically this will be the actual Twilio implementation and right here instead of saying Twilio SMS sender simply say SMS and then sender and let me rename this to SMS sender all of that as well and now it looks a lot better because now we are simply using this interface right here and we can choose between implementations so in our case we have this truly SMS Center for example if you had some other provider all you had to do was to create this same class right here extend SMS sender and then name it and then name it right here and if you wanted to change the actual provider you don't have to change any code so all you have to do is change to your new and L provider right so we have Twilio and we're gonna stick with 2004 this video and pretty much this is everything that we have to do in order to send SMS s now the fun now the most exciting bit is to test it so let's go ahead and run the application and you can see everything is up and running we have Twilio initialized correctly and then we have that tomcat started on poor eighty and if you remember correctly in our controller this is the actual path so API v1 and then SMS so for this video I'm gonna be using this rest client called post man so if I open that up and collapse that and then as well this and let me get my phone on the right hand side so you can see exactly what we're doing there we go so now let's go ahead and send a a post request so basically this client allows you to perform like and get requests proposed put delete and all kind of other things so we are using this because we don't have for example an Android app or an iOS app or even a web app that connects to our back-end so basically this is a client that allows us to do that so I'm gonna send a post request to localhost and then for 8080 and then for slash API and then V 1 and then SMS so localhost 8080 so you saw the port right here so this is the port and then the path is API v1 SMS so if I close this and then go to body and then roll and right here let's go ahead and send a JSON to our service so this is what our server is expecting and then we need the actual payload so let's go ahead and say this will be phone number and make sure that you are sending to your phone number right so I'm gonna add my phone number and then the actual message and this will be hello world with spring boot and Twilio and that's it so now if i unlock my phone and then let's go ahead and send so let's send SMS and there we go so you can see that we got a two hundred starters code and you can see that we've got a new notification with the actual message so if I UPS so if I go that and you can see that we have the SMS and the phone number right so remember the from phone number is if I go to Twilio and right here you can see that this phone number is the same one as the one I've just received so now if I go ahead and open up the SMS so I'm gonna click on the actual notification and there we go so you can see that it's sent from your Twilio trial account hello world with spring and Twilio and this is awesome so we can even send emojis so go ahead and say copy and then paste emoji so there is a link right here so if you search for this and click on the very first link and pretty much just get any emoji so I'm gonna get this one copy go back to postman paste that in and in fact I didn't copy it correctly so I'm gonna grab that copy go back to postman paste and then I'm gonna say wait and an emoji and then send there we go and you can see that we got a second message with our emoji and in fact let me just go ahead and change this so you can see exactly again so hello how are you and then let me have this kiss right here so copy that so this emoji and then go back to postman paste that in and then if i unlock my phone so you can see exactly what we're doing and then send there we go you see that we go a third message right there with the different emoji and a different message and this is pretty much how you use Swilly api with spring boot to send SMSs so obviously you can do a lot more than just sms's with to do api but I just thought that this is a very cool API that you should be using in your applications in case you need to send SMSs alright so if you enjoy this video go ahead and click on the thumbs up because this really helps me and he makes me happy to be honest when I see many likes and basically if you want to download the source code go ahead and check on the link below where I've got ad I have repository with the entire source code for this video also don't forget to subscribe to my channel to get more videos like this also follow me on my Instagram where the community is growing and if you have any questions or any other videos that you would like me to do similar to this one go ahead and drop me a message otherwise join me in the next one see ya
Info
Channel: Amigoscode
Views: 54,632
Rating: undefined out of 5
Keywords: spring boot 2.0, spring boot, java, twilio, api, how to send sms using java, how to send sms using twilio, Web Development (Interest), spring, pivotal Web Application (Industry), Java (Programming Language), Spring Framework, Software Developer (Project Role), Java (Software), Weblogic, cloud foundry, microservice, microservices, tomcat, jetty, undertow, rest, mvc, springio, dependency injection
Id: OuBttmaPlhM
Channel Id: undefined
Length: 38min 20sec (2300 seconds)
Published: Mon Jan 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.