 Welcome back to the channel. In today's video I'm going to show you how to use Twilio which is basically a provider for SMSs, WhatsApp and all the different messaging platforms in an API. Now we'll be using this to work in a WordPress plugin that will allow us to send information whenever a particular event is triggered. So we'll have it as a standalone plugin to work with WordPress or we'll actually use it inside our WooCommerce to trigger particular events such as a change in the order notes or a change in the order status for a particular client. Now it's a bit costly when you come to Twilio as a platform unless you're sending from the US. It would be advisable for you to check out the pricing so that you would see that it is something that's comfortable for your particular country. Let's click on the details to see the costs for the US. So for the US it's this cheap but if I was to change this to my home country you will see that it becomes quite costly at this point. So I'm paying over 6 cents yet in the US we are paying a 7th over cent. So after seeing the pricing here so I'm going to go down to the docs I'll go to the API reference and then you're going to see here that you have the option of starting to see the different services they use and then choose the API you want to work with. So what I want to do is work with SM messages and I'm going to just click here on that overview and you'll see that we have this whole documentation that gets us started off and we can do particular things. So in here we have an option of working with a free Twilio account so that's what I'm going to do. I'm going to click for one. I'll sign up to open it and of course I'll fill in the particular details I'll just use one of the password generated on my computer and then I'll accept that and click a free trial to run. Of course I'll have to verify my account by confirming my email and once I've verified my email they're going to ask me to verify my phone number so I'm going to use a demo phone number in here and that will give me a validation code and once that is done then I'm ready to fill in the little details that I needed to check out my account. So I'm going to use SMS and then I'm going to use a lot of notifications and I'll build with code and of course my preferred language is going to be PHP that's what we use as a backend for WordPress and then I'm going to run this on my hosting service and I'll click submit and now we have a dashboard to work with and of course in our free account we're going to be given some $15 to use so that saves me a lot of money so what I'm going to need in this particular case is this account SID and of course the token that will be given so all I need to do is just click show and I'll be able to get both these IDs now what we're going to do is close a few of these tabs we need to go back to the API docs so I'm going to go to the docs here and go to the API docs I'm going to go back to the overview I want to look at the API overview just to see and understand what it does so in here we have a base URL that we'll always use and you can see it applied in different ways and we can do something in Cal give it our details and it will be able to give us a service now I want to be able to send my SMS so this is a section that I'm in and so I'm going to click this to post and this takes us to the right resource to help us send our SMS so now you'll see that we have to post we can either use a method.json's file and then pass it to our account SID or we can use something like this code that we have here so that we always have something that's current and running that's triggered by an event of course using WordPress hooks so what I'm going to do here is you will see that we have a vendor slash autoload.php file that's being required and we are told we need to use Composer to be able to do this if you want to check if you have Composer on your computer I would recommend you go to your terminal type Composer and then type dash dash version when you click enter you'll be able to get the version number of your Composer right here and if you don't have Composer I would advise you to actually just go look at this documentation that's being highlighted here and then you'll be able to see how you can install Composer on your own machine now depending on your operating system you'll find a way of installing Composer right here but on a Mac I would advise you to use something like homebrew to make it an easy process for you so after ascertaining that we have our Composer what I'm going to do is just go into my plugins folder right here so I'm inside my website I'm inside its WP content and then inside the plugins folder and what I'm going to do right here is open up a new folder and call it Twilio dash messaging and of course we'll take a press that and I'm going to just come here change directory and drop into Twilio itself so now we are working inside this folder and the next thing we need to do right here is we're going to use the SDK version click this SDKs so that I can see the one for PHP when I click on the PHP you'll see that all we need to do is just run this line that requires Composer and then it gives us the name of the SDK now if you don't know what Composer is Composer is a packaging manager sort of like what you use NPM to handle your packages of Node JavaScript so the first thing we're going to do in our plugin is run Composer in it we're going to generate for us a Composer.json file we want to give it this particular package name we'll press enter description will say sending sms with Twilio and then the author will be fine with that and then minimum stability will be dev and then for the package we're just going to say it is a project and then the license I can give it an MIT version and then I'll say I'd like to define my dependencies or require them interactively and I'm going to choose yes so I'll press enter and then they're going to ask me to search for a package. Now the package that we need here is Twilio slash SDK and when I click enter I'm told to leave this so that I can get the latest version if I wanted I would have chosen one particular version but I'm going to go for the latest I'll press enter and then back and notice saying that we are using 6.18 version of this SDK and then we'll just say no to this and then you're going to see that we now get this particular JSON that's going to appear in our file at the end of the day so do I want to confirm generation yes I'll press enter would like to install the dependencies yes so I'll hit enter and then select the rest for me so we have everything done I'm going to go inside my folder and I'll check out this and you'll see that we have this new composer.json file and we have this folder that has a vendor folder in it and you have composer and you have all these classes of course I'm going to drop this in my editor so that I can look at the files more independently so I'll hold this and drop it here and you'll see that we have this showing up right here we need to look at the composer folder we have this but we have not yet installed our Twilio SDK right here so what I'm going to do is just open this up in my terminal or the integrated terminal VS code and I'm going to type composer I for composer install and I'll hit enter and you'll start seeing that this is initializing the installation and we have a little folder that's opened up here which is called Twilio and in here it's going to have its SDK and it's going to start having all the different files that are required for this particular package to be installed and right now everything has been done and we are good to go so I'm going to go back to the docs here and you see that you can actually start running PHP in here and you can create an SMS and you can be able to send everything so first and foremost I'm going to go back into my folder here and I'm going to open up a new file which I will call take it press Twilio messaging and I'm going to add on the extension of .php and that will allow us to start writing our PHP right here in this folder so I'll open .php here and of course we now need to add our starting comments for our plugin that we're having here so I'm going to call this take it press Twilio messaging and then of course I'll change the plugin link and I'll add the github version of where I'm going to store this slash and then I'll add on take it press Twilio messaging then of course I'll add my plugin author name I'll just take it press and then of course just say this plugin sends SMS using Twilio version will be that and then I'll give it a license and I'll say it's an MIT version or I can say it's a GPO to later and then of course I'll use that same thing for my text domain, save this and then I will be having a plugin inside my admin so I'm going to just go to my browser go into my dashboard go into the plugin section and you will now see that we have this Twilio messaging plugin here I'll activate it and of course that's now going to work but we don't have any code in here so I'll go back to the documentation that we do have and you're going to see that we have this particular code that we need to require the autoload file then we shall be able to use the Twilio rest account client that will allow us to send SMS we need to pass in our account SID need to pass in our auth token and then we can use this client from this particular class and then we shall be able to use the particular methods coming from the client class and then we shall be able to create SMS so I'm going to just copy everything that we have here and I'm going to drop it under here but of course I need to add some basic security here by saying defined and we're going to be looking for the abs path global and we'll say if the abs path is ok then we'll work if it's not added then we're going to die so I'll say unauthorized access and of course add a comment and say this is basic security and save this so in here what this code is doing is that it requires the vendor autoload file which is under this folder and this is the file we are looking for so it basically gets composer working in here now after getting composer working we require the Twilio folder SDK to give us the rests then we get this client.php and it uses all these files of PHP and gets everything into one single class and then that gives us that class which class we are going to reference here as a client and then we shall be able to pass in the ID which is our SID and then the token from our API so what I'm going to do is just go back to the browser I'll go into the communication I'm going to copy our SID I'll come here and change it pass this in I'll come back here and then I'll get the auth token and also pass it in here just store it as a variable and then that's going to ensure that this is working now remember we've stored this particular class in this variable and this class has a number of methods of functions attached to it and we have the messages and we have the create also change to that now all we need to do is just pass in our number here now remember I have a demo number that I'm going to pass in here so for this part we need to get a number from our Twilio console we're going to go back to the browser and then we're going to get a trial number from Twilio so I'll click get a trial number and then I'm going to choose this number and this is the number that I need to use I'll click done and then I'm just going to go back into our code add that number there and I'm going to just leave this body of text and say hey it's WordPress and this is the message we're going to be hoping to get from our phone when this message is sent it wouldn't be prudent for us to allow this to run aimlessly so I'm going to have some WordPress actions going on here so I'm going to use an add action so I'll use add action right here and I'm going to use the publish post action and in here we're going to write our on function which I'll call take a press publish post Twilio and of course we need to wrap this inside single codes because that's the format we're using and then I'll add a semicolon here so I'm going to copy this and add a function now of course the publish post action gives us the post id in here so I'll pass in the post id that's what we're getting and then we're going to use that in here so what I'm going to do right now is just copy whatever we have here cut it and then I'm going to paste it under this function of course I'll have to tab this in so that it's easier to read and then what we're going to do right here is say you know what it's WordPress we're going to also chain on the post id post id is created and add a space here so that this is a better message and so this will only be triggered when the post is made I'm going to come back to add main area and then I'm going to go to our posts of course we have an error it says defined we made an error here and we just need to add an e here save this come back reload this and of course we're going to go to our posts here and I'm going to click add new I've introduced my phone so that we can be able to see the change or the message that does come when we trigger our code remember this action of post publish or publish the post should be able to run our code once we click this publish button here so let me add a new title and I'll say new plugin test and that will be our title we can add content here if we need to and then what we need is to click this publish button and on clicking it the post has been published and then all we need to do is now see that on our phone we have a new message that has come in so I'm going to open it up and you'll be able to see that it tells us that a page 1976 has been created and if we look right here you're going to see that our post ID is 1976 so that works very well now we can move this from just WordPress and add it into our WooCommerce and if you want to try that out I have a video here that showed you how to send an SMS to a customer using a change in the order status or in the notes change and you can implement that using this Twilio API that's a little bit different from the API that we used here so what I'm going to do is just copy it from the raw version so that I just have only the code and we are going to implement our API in this so I'm going to copy the code that we have here and I am going to just close this off so that we can apply it so I'll come back here and I'm going to just close this off so that we can apply it so I'll come back here and I'm going to paste the code in here and this code is just targeting when we have WooCommerce order status changing or whenever we add new notes and then with that it picks of course the order ID and the different information and then it runs through this particular method which we have called take a press send SMS to customer we have a phone that we send to we have a default message and the shop name now in here is where we run our message right here and then that message gets loaded into this custom URL and we send the message now all we are going to do is just get whatever we have here of course copy it and then we are going to comment out this code so that it doesn't run anymore and then we are going to come back into this particular place and we are going to change everything that we have here so let me remove the URL the arguments and the response and the error let's this here and what we want to do to copy from here we just need to get the phone number that's going to be passed we had saved this as a variable so I'll change it in the location copy this message and I'm going to paste it here in our body I'll save that and then all I'm going to do now is remove this data that is existing here the only thing that I'm going to do differently is add the shop name at the end so I'm going to change this one with just PHP and then of course we need quotes in here and I'll put a dash so paste this out save this we are now going to go and make an order from our wukamas and see if we'll get a message on our phone so I'm going to go back inside here I'll go back a step I'll go into my wukamas and I have a number of orders that are happening and I'm going to change this order from on hold and change it into processing and then I'll hit update here we'll see that we have an error here and it's telling us this number that we're trying to send to is actually not right because it doesn't have a plus sign so I'm just going to go back to our code here and I'm going to just chain on that plus sign so I'll add on this plus and then and then change that so I'm going to save this come back here let's change our order to refund it and I'll hit update and now you'll see that we have a new message that's telling us that we have something coming from our shop we have everything as it should be and then we can also add a new note here and say this is a note that's going to be sent so we can send notes to customer and we'll say new note for the SMS and again let's just watch our phone to see if we're going to get the same thing coming so I'll click add the new note is added to our notes and you'll see that even on the phone we actually have sent from Trilio and then we have the new note SMS for YouTube so that's how we can incorporate the Trilio API inside our particular SMS or WordPress plugins so that we can be able to send out messages to the different clients please use this accordingly to your plugin, to the way you want to design it use the API let me know how you're doing in the comment section if you like the video please give it a thumbs up don't forget to subscribe and share this with your friends otherwise enjoy your coding