How to send an SMS using Node JS | VERY EASY!!!!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody Welcome to coding 101 in today's video I'm going to be showing you how to send sms's directly to your phone using JavaScript in node.js it's a very very simplistic tutorial nothing too complicated I remember when I was trying to do this and I watched a lot of videos and everything was super complicated but I'm going to just show you the simplistic way to do it almost every applications especially those ones that are client based and when I say client based I mean that you have to communicate with the client and the client test the client has to communicate with the administration the administration or the back end as the communicate with the client so you will find that this SMS feature is very very important so for example say you were to create an e-commerce website there's a need for you to send sms's to the customer to probably show them where their package is and all that kind of stuff almost every single applications that I've ever created has needed this type of SMS functionality but it's been so difficult trying to find the right code to try to implement this feature well you're in luck because in today's video I'm going to be showing you how to do it how to send sms's directly to your phone literally you're going be doing it and the SMS is going to be arriving into your phone so before we get started I do want to encourage you to subscribe to the channel make sure you click the Subscribe button and after you're done clicking the Subscribe button make sure you click the like button and make sure you comment uh underneath this video If you enjoyed this tutorial or you would like me to do any other tutorial beside this one all right let's get into our project before we get into our project I would like to explain the package that I'm going to be using to in order to send these sms's because obviously we cannot do it with just regular JavaScript we have to use an external library and this library is very extensive because it covers a multiple uh streams of communication channels if I could say that there's a lot of things that you can do using this package and many of you might be familiar with this package it's called twilio so twilio is a very very powerful platform that you can use to send sms's you can use to even send WhatsApp as well so if you're interested in me doing a tutorial on how to send WhatsApp messages using trilio please make sure that you comment in the comment section be happy to do that for you so it's a package and it's called twilio it's the one that you're going to use and the good thing about trilio is that it's not only integratable with JavaScript but you can also integrate it with many different platforms out there just in case you're not interested in JavaScript so you can send sms's you can send emails as well you can also send WhatsApp messages as well so it is a very very powerful tool that we're going to be using today now in order for us to use this tool you do need to sign up you need to go to the trilia website go to twilio.com and you need to sign up to this platform I have already done this they also have a trial version for you to try out so all you just need to do is to sign up and as you can see it's no credit card required and after you've signed up after you've signed up to this platform you're going to need their authentication token and you're also going to need the account as ID from them so those are the two important things that you need to do so I'm not going to go through that phase because I don't want this video to be long but all in all you can see that there are many things that you can do with this 3D website as indicated on the screen as SMS marketing call tracking web chats many different things but we're going to be focused on SMS today without wasting much further of your time I would like to get into our code the first thing that we of course need to do is to create a node.js project so we're going to just say npm init dash dash yes so that we don't have to bother with those questions that npm is going to give us and just like that we've created our very own npm project and we can just go into our visual studio and we're going to create a file called changes like that we've managed to do that and I think for this one we can just simply either we can just like go into it but I like to create a server because I don't know it just gives me that that balance obviously I can just run this package without run this file without the need for a server but I think a server is always important because it just shows that something is happening but once again you don't actually need to run a server for this but I think for me I'm going to need to do that so in order to run a server of course we need to install Express we also need to install twilio the twilio platform as well so I'm going to just say come over here and say npm install twilio first and then Express it's gonna pause the video and allow for those packages to install into our project and after our packages have done installing we can just simply create a simple Express server nothing too complicated we're just going to say const Express it calls to require once again you don't necessarily need to create an Express server check we're going to run this at 45 000 log listening five thousand right that's good and what I'm going to do over here is that I'm going to also import art video so I'm just going to say twilio call to require and then two video we're going to create a function here I'm going to call it SMS and inside of this function we're going to create or configure our twilio now like I said once you log into the twilio website they're going to give you two types of code so 2do as an API of course so they need some sort of authentication for you to use their service so they give you an authentication token and they give you an SID so as an SID is a form of identification so you have to extract these information from the Sid and the op token as well so if you are having trouble doing this just like shoot me a comment in the comment section and I will provide a link that will give you a step-by-step instruction on how to access these two very important variables so now I already have these variables in place so I'm going to be integrating them with my SMS function so of course the first thing that you need to do is to configure your twilio transporter or whatever you call it your Twitter your API with those Sid information so I'm just going to say Tradio like this and inside of here as you can see on the function definition you could see that the first string that is going to accept is the Sid and then after that it's going to accept your authentication token as well so you're going to put those in there I am actually going to declare mine on an environment variable because I don't want people to have access to them and actually use my twilio bundles or whatever you call them so I'm going to come over here and I'm going to create a dot in EMV dot EnV file and this is where I'm going to put in my trilio Sid information and I'm also going to put my twilio off Authentication token all right or actually you need to put codes you just put the you just put the the combination of letters in order to put them in a codes dot EMV does not identify between strings and all that kind of stuff it just stores a variable so the other thing that of course I'm going to store in here is my phone number as well all right and then after filling my DOT EnV file I come back to my code and now all I have to do is put them in here wait I first need to import dot EnV in my thingy over here so I just need to say dot EnV y what's the required Dr EnV like that so if your dot EnV does not work as it's supposed to I think you may have to try installing it in your command line you may have to just come over here and just say npm installed yes I think I've installed it University or it's built in um so it's probably why for me I don't think that was necessary but yeah you can style it as a maybe a depth dependency then you can come over here you can just simply say process .m Dot and you're going to take in those um things that you put in your environment variable first is the Sid you're also going to say Point V all right and then once you're done doing that you've initialized your client which is going to be responsible for spending these sms's what you simply do is just you just you can just return this and I think you may have to see a sink of weight over here because now you're returning a promise you just have to say async over here and then you just say client messages dot create [Music] you can just put over here the body of the message say hey this is a message right you're going to say from so twilio is going to actually provide you with the default phone number that is going to be the one that is going to be used to send those sms's so if you're not from America this number is actually an American number I don't know if there's a possibility but there's some countries where you can actually use treatio to get that number that is used to send the SMS and over here I'm going to put the phone number that is going to receive the SMS so I'm going to say processed on it yeah V and then I'll say phone number and then after that because this is a promise I'm just going to say then message let's see whatever is returned message like that if there's an error sorry dot catch there's an error we'll just simply console log there to be very much interested in that Julio and then after we're done doing this yeah I think that's about it we should be able to come over here and call this method that we see over here I'm not really seeing the need of this async I'm going to get rid of it for now so I'm just gonna and now we just go to our command line and we run our node application but before we do that there's one other thing that I think I might have forgotten and is to configure our DOT EMV so we're gonna have to come here and say config that after doing that we just scan our code make sure everything is good as you can see over here supposed to be phone number like this number cool I just want this message to be a little bit explicit of course it's going to show if we it's going to explicitly show if the message has gone through but I just want to create another console message so maybe we can come over here and just say message sent yeah and after here we could do the same for this as well this is just so that you can see you don't actually need to do this it's just for you to see that the message has been sent properly can create another parameter over here I can just say um message not sent maybe not not sense like that and this is how we will know that the message is gone and the message has not gone all right cool so that is our application so I'm having my fingers crossed that everything goes well and if we come over here and we say node app.js fingers crossed that it works and as you can see it says that the message has been sent successfully I am going to provide a screenshot of the message from that exact phone number on the screen so you can see it but other than that yeah we've managed to send an SMS through our JavaScript in node.js so thank you so much for watching this is the end of our video please make sure that you have subscribed to this Channel and make sure that you've liked this video if you've enjoyed it and make sure to leave a comment in the comment section if you've enjoyed it or if you have not enjoyed it it's still fine with me but thank you very much once again for watching I will see you next time in the next video bye
Info
Channel: Koding 101
Views: 24,494
Rating: undefined out of 5
Keywords:
Id: l4DiU93LyhM
Channel Id: undefined
Length: 13min 10sec (790 seconds)
Published: Wed Oct 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.