 Right so hey guys and welcome back to another Python tutorial so it's been a long time since I've actually posted a video so here we are with a nice tutorial so in today's tutorial what we're going to be doing is using an API to basically send text messages from our computer to mobile phone so I've tested this script with UK numbers so you might need to test it with other country codes depending on where you are so to begin with what you're going to have to do is sign up to a service called cinch which is the service that I'm currently using now when you sign up for the account you're going to have £2 credit to be able to use so once you run out of that you might need to upgrade and buy a few more tokens by updating the balance on the account so for the sake of the tutorial I've got enough balance to show you guys how to carry out what we need to do so I'll put the link to signing up in the description so that you guys can create an account for yourselves and get yourselves an access token when you guys log in you'll be redirected to this screen right here which will show you how many credits you have left and all the other stuff in relation to documentation so the bit where you are interested in is the access token as well as the plan ID so to get these details what you want to do is click on SMS and then once that's loaded up just go into APIs now once you go here you will have your service plan ID as well as the API access token now for obviously security reasons I'm not going to reveal my access token you shouldn't either service plan ID should be alright so what we want to do is I'm going to copy these two values across so service plan ID I'm going to copy this one right now and the access token what I've done with it is I've already stored it in a text file so I don't really need to copy it at the moment I've done this step in advance so you guys don't need to store it in a text file I've only done it because I don't want to show it on my screen so what you want to do is open a new Python file it can be a Jupyter notebook or a script file depending on what you're comfortable with so first things first we're going to be importing the request library since we're going to basically make a post request to the API with all the data so we're going to need data such as who the message is from which cinch basically gives us a phone number using which we can send the messages across so they assign us this number in advance we're going to use that who the message is going to be delivered to as well as the other security details like the access token and the service plan ID so to get started with the basics what I'm going to do right now is create a new variable called service plan ID and then paste in the service plan ID that I just copied across from cinch now what you guys can do next is just create another variable called access token whatever I done there get rid of that so create another variable called access token and it's going to be a string again and what you want to do is go back to your dashboard reveal the access token copy and paste that across in here now for security reasons I don't want to expose it so I'm good I've already stored it in a text file so I'm going to create a function that's going to get this access token from my text file without revealing it to you guys so get access token with open access token dot txt read mode as F return F dot read now this will work because my text file literally only has the access token in it so it won't have any issues of multiple line handling and all that stuff I could have just used dot 10 but for the sake of this tutorial just to make it quick I'm just reading it from a text file so what we can now do is assign you guys might have already copied and pasted your access tokens but for me I'm just going to run this function and it's going to grab the access token from my text file without revealing it of course so I've got the access token we've got the service plan ID the next step is to know where the number where the message is going to be sent from now the as I was saying cinch assigns you guys to a number that you can send messages from now you can get details about that from going to the getting started section it will basically have the number it's sending the message from in there now the reason I'm not opening it is because cinch has got my phone number revealed so obviously I don't want that made public so if you guys would go into getting started up here you'll be able to see the messages from from where you the number from where you'll be sending messages basically so you're gonna create a variable called from underscore and then in speech marks just paste that number in now I've already got my number stored on my second screen so let's paste that in right here now once you've pasted that number in what you want to do next is basically create a new variable called to now this variable is going to hold the number to which you want to send the message now in this case I am not going to put a number in but if you guys would put a number in if you are in the UK you need to put the country code first so 4 4 no plus sign so anything followed by the rest of the number if you're in any other part of the country which I haven't tested out at the moment you'd be able to just use the country code with no plus sign and then the rest of the number as well so once you're set up with all these details right here what you want to do next is we want to build up the request that we're going to send to the API so we've got all the data ready we just need to build the requests so to do that we need to be able to authenticate ourselves with the API to make sure that we are valid users so let's create headers which is going to include the authorization that the API will see so in the headers we create a key called authorization have I spelled that right yep authorization and then it's going to be a bearer token so type in bearer and then we're gonna use an f-string here so it's going to type in B E A R E R bearer and then whatever access token was stored up here will get replaced in the string down here because we're using an f-string so that's the authentication handled now we need to also specify the content type as application for slash JSON I've tried without this and the API was sending me errors so make sure you do include the content content type in the headers so that's our headers ready to go now we type we need to write up the payload so the actual data that's going to be posted to the API so we are going to create another dictionary and the first key is going to be from which we have in our from variable right here so this is going to include the number from which we're sending the message the next key is to now this is an array so I'm assuming you could have multiple numbers in here I've only tested it with one number but feel free to add multiple numbers in here to see if we can do a batch request which I think it should be able to so to is going to be assigned to an array and then we're just going to put the two variable right here oh no I've just accidentally closed that so sorry about that I accidentally closed the whole thing but here we are again so we've got the two and then lastly we need the body of the message so the actual content of the message I'm going to say hello world because it's only right right so that's the payload completed as well so that's all the data that's going to be posted to the API now finally let's take these two variables and combine them in our post request so we're going to since we imported requests as are we're going to do our post so requests post and we're going to provide it with all the essential data so first we need to provide the URL to which we're posting this data to so I've already got this stored in my other file we copy and paste it across so it's going to be very similar for you guys as well but if you want the exact URL to where you meant to post it you can go to your dashboard click on getting started and then you'll be able to see the URL to which you meant to post to so this bit will stay pretty much the same for everyone the only thing that changes in the URL is the service plan ID so the service plan ID we already copied and pasted across at the start of the tutorial so we don't need to worry about it since this is an f-string that will replace it right here so we've told the request where we want to post it to now we need to provide headers for authentication so headers equals headers because I've got another variable with all the authentication stuff right here and then lastly we need to include the payload as well so payload equals and then we're going to type in sorry data is the argument in the request so data equals JSON dumps and then we include the payload so we use JSON dumps to make sure that the format of this data is actually in a valid JSON type because the API will expect that now we forgot to import JSON at the start so let's do that as well before we run into any errors so import JSON now let's run the whole thing now at this point what I'm going to do is I'm actually going to type in my number in here and I'm going to blur the screen out so that we can actually run a test to see what the output looks like so this bit's obviously going to be blurred out in a second now let's run it again and as you can see it says the ID that we can use to track the message tasers it says the number to which we're sending to which is obviously going to be blurred the number from where we're sending it this can be obtained from cinch and then the body and all the other stuff as well now at this point if you've got this message that that means the message was sent successfully so you can check the the phones to which you send your message and hopefully you should have the message delivered and that was it for today's tutorial guys hopefully you have enjoyed there's going to be a lot more insightful tutorials coming soon so stay tuned for those in the meanwhile feel free to like the video if you found it useful make sure to comment share subscribe and I'll see you guys in the next one