[Music] hi everyone my name is federico tartarini and today we are going to develop a simple python bot this bot is going to receive a message from our user and then is going to reply with the same message that the user has sent to the bot of course from once you've understood how to create a simple bot you will then be able to make the bot much more complex and you can have a look at this python telegram bot library which allows you and gives you a lot of functionalities that you're going to be able to use however in this video we're going to keep it very simple in this tutorial we're going to use google cloud function the alternative is to deploy our google bot on a server the server will be always on and listening and of course we will have some local storage that we can use but at the same time we are going to be billed for all the hours of the month because we of course want to make sure that our bot is always active using google cloud function solve that problem because we are going to be using a web book so every time a user send a message telegram is going to send a post request to the web hook and then the web hook is going to trigger the cloud function the cloud function is going to perform all the actions that we have specified and then is going to be automatically killed by google the other advantage of using google cloud functions is that we're not going to be worrying about maintaining the server so we're not going to have any downtime time we don't have to upgrade or update or patch any packages because google is going to take care of all of that google cloud function our serverless function so we just have to build a bit of python code and then we just have to deploy the code that we are going to deploy is going to be very simple and i've taken most of the code that i'm going to be using in this tutorial from this blog from pablo seminario so of course in order to follow along with me in this video you're going to need a couple of things so you're going to need a google cloud platform account so as you can see here i'm logged in you can log in with your google account and you're going to be needing to activate building which means that you're going to have to enter your credit card details most likely you're not going to be charged by google because if your telegram bot is used by few people they're going to trigger the function a few times per day and therefore you're going to stay inside the free tier however once if you want to use google cloud functions unfortunately you have to provide your credit card details another requirement to follow along is to have python installed on your computer and you can simply install python on your computer by just going on the python website and then just install it so as you can see here install and then you can download python based on the type of machine that you have like here you can install python i have a video also that i'm going to put here a link at the top in a card on how to set up python and install python on pycharm one other requirement that we're going to need for this video is to install the google cloud sdk it's very important that you have it installed on your computer so we're going to be able to use the gcloud commands in order to install it on your computer it's pretty simple you just have to follow these instructions here which i'm going to put a link in the description and basically you just have to select your operative system if you're using powershell i would just suggest you to just copy and paste these into powershell and then this is going to open this setup you just have to follow it along and then tick all these uh check all this thing which are already set by default press finish and you're going to have gcloud installed on your computer the first time that you're going to run a gcloud command from your computer you're going to be redirected to your web browser and google is going to ask you to actually log in into your account using your username and password and then once it's set up everything is going to be fine you don't have to do it again of course since you are developing a bot with telegram you will also need to have a telegram account that's of course a requirement so actually let's start with that we can go in telegram i'm going to use a web telegram of course you can do the same actions from your phone and the first thing that we are going to need to do is to search for both father so we can go here and we're in the search bar and we can look for bot father of course it's already there but i just want to show you how to look for it we find about father and inside here we're going to type a new command so maybe if you have never created a new bot your page here is going to be empty but in my case i've already created a bot before so i'm going to create a new bot so with this command forward slash new bot and both father is going to ask us a couple of questions so my bot is going to be called example youtube video so that's the name of my bot and i'm just copying that because that's what's going to be similar to the user id so we press enter now we need to specify the username so we're going to say example youtube video and then we're going to call it bot please note that you might get an error here saying that the username has already been used so you need to find a different username i actually just realized that i misspell the username i call it you but doesn't matter this is an example i'm going to delete this bot also because i going to show you this api key of course in your case you will have to keep this api key secret because you shouldn't share it with anyone absolutely otherwise everyone that has access to this api key can actually send messages and can take control of your bot in my case i'm going to delete this bot after this video so i don't worry too much i just keep it here in plain text now we can just click here at the top on t dot me for the last example you you video bot so again for the error and then here we have our bot so we have no messages yet we're going to message with the bot later alternatively if you want to find this bot you can just simply copy your username here and you can just search here if you don't find it just put up at in front but basically this is our bot that we have just created same way to get there now we need to open by charm so i'm going to open pycharm and i'm going to open a new project so let me just bring here the new window so we're going to create a new project and we're going to put it into downloads so we're going to call it telegram example so this is going to be our location we are going to create a virtual environment we are going to use base interpreter python 3.8 we are not i'm going to we are not going to tick this thing inherit global site packages because we definitely don't want to do that yes and we are going to create a main dot by welcome script no we are going to just ignore that and then we are going to click on create so this is creating our project on our computer is going to take just a little bit of time i'm going to fast forward this because it's going to be installing and creating the virtual environment of course if you're not using pytharm you could have created a new folder on your computer and you could have created with pip the virtual environment and then you could activated the virtual environment as well i would recommend you to just look at another video line how to get started with a virtual environment if you don't know how to do that or if you're not using python great our project has been created as you can see here at the top i'm going to enter presentation mode so the basically the text on the screen is going to be a little bit bigger and it's going to be a little bit easier for you to see if we press alt one and here we are in our project so we're going to collapse this virtual environment the first thing that we are going to create is a main file main python file so we're going to call it main and inside here we're going to add a bit of code i'm going to copy this code from another project that i previously created and i copied from the online tutorial but i slightly changed it because i just want to show you a couple of things for now you can modify this file based on your liking so we are importing the os package and we are importing the telegram package here we're going to add later on the dependencies so don't worry too much now it's just showing you red we're going to fix that in a second then inside here we are defining a function the name of the function is very important and i will show you in a second why is very important just keep that in mind we'll go over that again and we're calling it telegram underscore bot then we are defining our bot and here we are using this telegram package which comes very handy because we are defining the bot and we are defining it with a token where are we getting this token we're getting this token from the google cloud function so we're going to save a secret inside google cloud function and then we're going to extract this sequence secret using this os dot environ telegram token then we are checking if the request is a post request so as i previously mentioned when the user send a message to telegram telegram is going to send a post request to our url endpoint so that's going to be a post request so it's going to pass this if statement what we are going to do is we are going to extract the message which is this update we're going to get the chat id from that person so we know the person that actually sent the message and we're going to extract that with updated.message.chat id if you want to find out more i would recommend you to just have a look at telegram library on how to unpack this message or alternatively you could even have printed this message and you could have figured it out and then we are also going to send a message so we are replying with the same message so we're going to send back the same message that the user has sent and we can use this function so bota dot send message we need to specify the chat id and the text of the message that we're going to send then we're going to return test or we can return okay just to make sure that everything is okay and this is going to just verify that this function is working the next file that we need to add now is the git ignore file so we can go here just make sure that you click here on telegram example the name of the directory we click here new and then we go to ignore file we go to git ignore file we click here we're going to use a template so we're going to look for python here so we can just scroll down we select python and we select intellij that brings of course i'm going to provide this source code so you don't have to worry too much about it i'm going to share the code with you so you can just simply copy from my github repository i'm going to press on generator and this is going to generate our git ignore file second we need to create a gcloud ignore file again we can use pytharm become sendy because we can go new ignore and we're going to create a gcloud ignore again in this case i'm just going to copy i'm going to use like the generator file here so we're just generating the file and inside here i'm just copying like some a boilerplate and i'm going to explain exactly which file i'm going to exclude them so we are basically excluding the gcloud ignore file itself we're going to exclude the gita repository we're going to exclude git ignore we're going to exclude the readme file that we're going to add in a second and finally we're going to exclude all the files that have already been excluded inside a git in order file so we don't need to write all the files again that have been excluded negative in our file but we can just reference them with this command now you might ask i'm familiar with the getting nor file by why do i need to add a gcloud ignore file well the getting not file is only for git so when you are syncing your project with git the gcloud ignore file is similar to the getting your file but is only for google cloud function google cloud run so you need to add this file if you want to let google know that you don't want to upload some specific file so we go back here at the top telegram example we're going to add a new file this file is not really needed but i'm just going to add it for you so it's going to be the readme file and inside the readme file i'm going to specify all the commands that we're going to use in this example so let me copy and paste it and then we're going to be using this command so we are basically going to create a cloud function run this command in the same line so we're going to just use this command in a second i will explain all the passages so we're going to first create a google cloud function and then we are going to set up the webhook with this command here but again i'm going to explain that the last thing that we need to do is we go here telegram example right click new and then we need to add the last file which the last file is requirements dot text so this again is very important because we need to let python know and the google cloud function know which are the requirements of our project and again python comes handy because as you can see this line is highlighted if i press alt enter it's asking me to install python telegram bot so i can just click on that or i could press enter and this is installing a python telegram bot for us we can also check that by pressing ctrl alt s we can go here inside we can go in the interpreter so we can go python interpreter and we can see that we have python telegram bot installed of course you could have checked that again by activating the virtual environment and with the common pip list you could have checked all the python packages that were installed we're almost there so now we just have to open the terminal so in my case it's going to be alt f12 and we're going to just follow the instruction here in the readme file so we're going to create a google cloud function with this command so the command is going to be gcloud functions deploy telegram bot i told you before that the name inside of the function inside main was very important so it's very important because we're going to add here the name of the function will have to be the same as the name of the function of python so the google cloud function name is better that has the same name as the main function that we need to call inside the main.pi file of course you could change that and i would suggest you to just have a look at the gcloud sdk for more information on how to define what's the name of the function if you want to and how you can change that then we're going to set the environmental variables and here what i mentioned before we could have added a secret directly inside a google cloud console but just to keep it simple inside this example we're going to pass here the telegram token and here we're going to specify the telegram token so i'm going to replace these common this text here with the telegram token just to show you all the steps we're going to define the runtime so in this example we're going to be using python 3.8 and the reason why i'm using python 3.8 is just because it's not exactly the latest version of python at the moment in which i'm recording but it's just well like it works with a lot of different packages and it's also very important that you specify this which is trigger http why do we need to specify this because we want to make sure that our google cloud function is triggered with an http post request we don't have to specify post but we need to specify that it's going to be triggered by an http request and finally we're going to be specifying the project name okay i'm going to change that in a second so let me open my browser again so we're going to get the project id so from here google cloud platform if you don't have a project already created you can click here at the top and create a new project and follow the instructions i've already projects created so i'm going to get this id and i'm going to copy it there so here let me just duplicate this line and put a space so we're going to put our project id and now i'm going to copy the telegram token inside here we just have to copy all this command here go in our terminal paste it just check that everything is okay and we press enter this is going to take a little bit of time and it's going to ask us a couple of questions we might also be asked to log in into gcloud if it's the first time that you're going to be using gcloud commands you're going to have to authenticate with your computer so that is going to be maybe a question that is asked to you so is asking you for allow an authenticated invocation of the new function of course yes we want that telegram is going to be able to send us a request you could change this and ask telegram to have authenticated invocation but this goes way beyond the scope of this video i just want to keep it simple so we press yes and this is not going to be a big problem unless you share publicly what is the url that telegram is invocating i don't think it's going to be a big problem anyway so we can press yes and this might take a little bit of time and we are creating our function so if you go back here you can see inside google cloud function at the moment we just have one function which is a function that i've created before you will see in a few seconds that a new function will appear and it's going to be our telegram bot in the meantime while this process is completing we can go and see the next step for the next step again we're going to be needing the telegram token so just let me copy that we need to set the web book where is going to send the post request so we need two informed pieces of information here so let me duplicate this line again and let me copy the information inside here so we're going to do https forward slash api.telegram bot and inside here we're going to copy our token just make sure there is no space nothing here in between bot and the bot secret id and then web book url you can find it here actually in our message that we just got our fun after our function was successfully uploaded so we can see that the success was successfully uploaded by this message is telling us a couple of information so what's the name of the function the status which is active the version of the function but most importantly he's telling us this url so we can get this url we can copy and we can paste it inside here if we go to this url we should be receiving an okay message and why do we receive an okay message is because we have specified that inside our google cloud inside our python file here so we can see here main.pi and return okay if you will have said any other message we would have seen that message over there so let's go back to the readme now we have specified the right url we just copied this command here and we are setting the url where a telegram is going to be sending our post our messages so we just have to press enter and we should be receiving in a second a success message and you can see that is a success message if the status code is 200 sub description is okay and it says okay results show description web book was set fantastic now we are ready to go so the next step is to go back to our telegram here online webtelegram.org of course you can do the same thing from your phone as i previously say you can click here or you can look for your id or telegram name that you just created you can click here and you can start a conversation with our bot so we're going to send the start message and as you can see we are going to be receiving the same message that we sent so for instance i can send a message say ciao and here we are receiving the same message from the bot let me quickly show you that if i change the main file i'm going to change here this from test i'm going to change it to from okay i'm going to change it to test and in the message i'm going to add here in the text we're going to say from the bot so here we're going to run again this previous command gcloud function deploy telegram bot in this case we are not going to need to again set up the environmental variables so we can cancel all this code here perfect so we can cancel all that and now we can press enter again this is going to deploy our google cloud function so we can see that a new version has been deployed because you can see here version 2. now if i click on this link here i'm going to be redirected to this page and we should receive actually the test message as you can see so we have test so we can see that all our changes have been successfully applied and if you go here to the bot you can see ciao and then if i press enter i should receive a message from the bot of course because i didn't put a space when i actually wrote the string i put plus and then the string from the bottom so we can see that everything is working if you find these content interesting and this video interesting i just wanted to ask you to like this video and please subscribe to my channel if you have any questions or comments please leave them down in the comment section below and i will share of course all the code on github so you can just copy and paste it and you can change it as you like