How to build telegram bot with Ruby on Rails and Postgres. From 0 to real product.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Pretty cool! thanks for sharing! Awesome to see a tutorial for a bot that can be deployed in heroku

👍︎︎ 2 👤︎︎ u/Ferdelva 📅︎︎ May 08 2020 🗫︎ replies
Captions
Hello, my name is Valentin and you're watching Bots factory. Bots Factory is a show where we build test and release chatbots together. In previous episodes we built chatbot from scratch with Ruby language and Heroku cloud platform I hope you tried yourself at home and didn't face any problems. I put the link to the video in the description box. Today we have a challenging task, it might take an hour or two. Fasten your seat belts, today we're going to build a chatbot from scratch with Ruby on Rails framework, Postgres database and custom logic inside. The idea behind the bot is simple we want to build a telegram bot catalogue a place where you can add, remove and find information about telegram bots. Our plan is simple, first, we need to create an empty rails application, second one we will integrate our chatbot inside this rails application and we add logic to safe users, to save bots, to remove bots, save description for the bot and search by descriptions, so let's start. We begin with empty rails application. we need to run our terminal and create a new application. I will do it in my GitHub folder, rails, you and we will name it, our bot, like "testbot", it takes some time to install all necessary dependencies, cool, We are going into our "testbot" and we have a rails app inside, let's start it to see how it works does it work correctly or not let's take this one, cool our rails app working correctly, so check We finished the first step. Next, we need to integrate our bot inside rails app so let's open our folder, our "testbot" we are going to integrate it inside let's create a new folder like telegram and create a new file inside and find it let's name our file bot.rb inside telegram folder so we have a new file inside telegram folder. Let's write a new bot first thing, we need to integrate Rails environment inside our bot "require" all environment inside our bot, also we can start new terminal and test it out "ruby telegram/bot.rb" ok looks good, there are no errors inside our bot next we need to add telegram library inside our gem file like we did in previous episode so open gem file and below. gem telegram bot ruby run bundle install, okay we good, to run your bot inside we require telegram library so required telegram bot okay good now we can start writing our simple bot run token we will set talking above like talking a bit later do bot and cool for talking I will use my token from testing bot this one okay below we need to run a bot we need to listen for new messages and you could have a new message let's print it out on our output and we will exit the bot let's start okay we didn't set up correctly talking let's rename it cool we'll have something like we have something inside so our bot integration works correctly so what's next we need to create a new logic do create new users but first we need to set up our database for Postgres by default rails working with SQLite and it's not so super suitable for production environment because it is a file actually and we would like to set Postgres as a database but first we need to create a database itself let's create a new database create the database we will just name it like test bot test both death and exit I have Postgres in my local machine so let's find how do have a new database inside testbot_dev yes we have one so we can connect to our database let's do it I will use let's create a new connection to Postgres and name it dev by default we have all the local machine and testbot dev test and connect okay we have empty next we need to point our database to point our rails app to the database let's do it with settings configuration database we need to change adapter from sqlite3 to postgres to postgres here and set correct database name testbot dev also don't forget to change VM file and switch from SQLite to progress run bundle install and try to run your app rails s correct our app responds correctly we have some logs inside that means that we have an empty website based on Rails framework and it has Postgres as a data base so we set everything next step for us is a create a model for users rails framework works with objects and we need to set two different objects first one for user and second one for bot let's start with user for user I would like to save telegram ID and first name that's not for me let's generate new model call it user and we have we need to have two different actually three different fields first one it's name as a string second one we need a telegram ID it is integer and the third one we need next step or just simple step I will explain later why we need this one right now we prepared and you migration for users so we will go we are going to create a new table for users with name telegram ID and step let's run our migration and open database again refresh and as you can see we have a new table for users next we need to create the same like for BOTS let's generate rails generate model but with user name as a string next we need to have description as a string and next we need to have a reference to user because I would like to know what user save what what so you will allow to the delete bots only if you create it so user reference okay cool let's run migration and again oh we have a problem here not reference but references and open again your database okay we have table for our bots user name description user ID like reference to user and create it updated these columns created automatically for any rails model by default so next step we need to point that one user has many BOTS and we can set it with the model models user has many BOTS oh like this one but like and let's start our rails app again just make sure and validate that we created our models correctly ok it's fine it's super cool now we are ready to fill up our logic inside we are going for three different command to add a new bot to remove bot and a delete and search for bot let's write down our logic so we will accept commands from message from text from message that means that we need to set a logic inside this block and we need to understand what we're going to do based on text in messages for the at this point switch case statement is enough for us case message texts and when we have add next we going to write for the delete and search at this point we are ready to fill up our logic for every single case at first step let's just send a message that we are going to add you both were going to delete bot and we are going to search for the new bot so we are going to reply to our customer our user so while reading of the chatid it is message chat and ID and we are going to send a message like send me bot username and the same for delete but choose bot let's try pick bot to delete and for search let's ask for a search request send me your requests okay we ready to go let's run our bot so we're ready to go let's run our bot add okay it is working delete okay working and search cool it is working right now so next step we would like to add and a new bot to our database but first we need to find and save data about our user so how comes that we will use user model and we will save user if it doesn't exist in our database that means that we are going to find do we have a user in our database by telegram ID if user exists user find by if not exist we will create a new one with telegram ID like message chat ID and name do have a name right yes we have a name like message from the first message from user name or first name and actually, here we can set from ad let's run our bot again we will stop it so and if we if we don't have we will create a new one and versus if we have one we are going to find by telegram id let's run what and send a message any message it doesn't matter what doesn't okay we have a problem and the method exists for nil class so yeah it is we shouldn't find but just user exists and telegram ID I just forgot about that that's enough that's wrong again and exists and again let's send a message to our bot and open our database cool we have a new user by me so it's me it was created right now so next step we are going to set a step for this user if we're going to add this step we need to remember somewhere that this user are going to add a new bot or delete new bot or make a search because on the next step after we are going to add something we need to understand that we are waiting for what username for example let's end it here so when we have an add going to set for user step add and save it and the same for the delete search actions but okay cool let's try it out restart our bot and send a new message and open our database refresh it and we can have a step add for this particular user let's try delete refresh page yes we have delete and search refresh again search cool logic works at this point we need to set a new logic for every single action that means that if we have user next step at we are going to save this user this text we got from user as a user name let's do it in case of case message text but not message text we will switch between user different steps so my laptop is died and we need to charge it so in case of add when add again delete and search add delete search when we have an add we are going to create a new bot bot create with username from message and for particular user actually user BOTS because we have a lot of bots right any user can have bots who okay we are going to create a new bot here next step I'm going to ask for a description so as you can see we need to add a new switch case for description so we set like if for user next step we are waiting for description and save it and it will have a description we're going to update the last user bot with this description use your BOTS last last created user bot last description from message text and we need to clean it up click clean up our user next step because we are not waiting for anything for from him let's try this out and of course we need to set like bot on new bot last you bot description you bot save let's try this out so we're going to add new bot my to email bot it is a real what you can find a video about my bots in my channel so look at my videos okay and next we have not set a message like we're waiting for your description but we will write down like this is the description in his address cool now we have to open our website database and we have a new bot here everything works correctly but we need to add couple of message to make sure that our user understand what we are waiting for from here for example we will stop the bot and add send me bot description and again when we save everything we are going to send a message thank you I saved everything I saved your bot let's add another one and check that all messages were sent let's add my track bot so we got a new message send me about description for example I'm tracking your activity cool let's open our database and we have a new one everything works correctly so right now we need to add a new logic for delete step if we're going to delete our bot I would think that we need to allow people delete bot they created before so would not allow people to delete different random bots from our database so that means that we if we are going to delete about I need to ask and I asked to pick about to delete that means that we need to send a menu with all bots user created before we need to get all bots which were created by this particular user how we can use it using BOTS map you bot for example it is a variable it doesn't matter how you name it and we need to collect all bots usernames to an array and set it as a keyboard you bought next user name and set it as a keyboard so we are going to create a new keyboard with command reply keyboard markup and here we need to set it a reply and reply mark up let's try this out we need to restart our bot and type delete pick about to delete so it doesn't work it was it was different keyboard reply marck up and restart but again so delete who have a keyboard with different bots this user particular created so next step we are going to wait what what what well going to delete and we need to check that do we have this bot on user name for this particular user so first we in to check what user name call include message text if we have bot in our database we are going to get it so bot find by a username message text in Destroy and else we we need to send a message like we can't find your bot and here we need to send a message like all good we destroy your bot and and also we need to clean up next step for this user so let's try it out we add new logic great so the net to email what all good we destroy your bot let's open our database refresh and yeah we don't have this what but if for example we send it right now delete and we don't have to email bot and if we try to send it we can't find this bot in our database so everything is good the last step we're going to integrate our search method inside and we are going to search by substring on all descriptions how we can do that so let's first add but again to make a search and description I for first all messages to email cool let's update database yes we have a keyboard let's search it we need to do it here so we need to make a search in our database and try to find something meaningful for this particular request how we can use it so first we need to find something in our database for BOTS now we have specific query which helps you to find anything inside strings and set strings description like message text it will be an array of bots and we are going to reply every single message for particular bot so if we have bots so this means that size of the array it's not a zero if it's not zero we need to send message for every single bots do not bot but let's try a s like search bot we like for example bot a user name and description why it's all noisy my my laptop so here when to send like search result search results so if we have some bots else we need to send a message like we can't find anything sorry we can't find you what we can't find em all for you and also we need to clean up our next step here okay let's just start our bot and try search option send me your request let's send email Search so we find one bot to email bot with profits all messages we mail we find it because our request found that fort we mail but we have email inside description if we sent a request for example for track let's enter search sorry discard track we find my track bot if we search for like something simple for I we find two different bots that's it as you can see today we create a new telegram bot which helps you to save data about bots we can edit delete and search for it and also we're smart enough and don't delete to you bot which not associate with particular user I hope you liked this video and make sure that you subscribe to my channel like this button and see you next week ciao we just created a bot a cool that is really really cool I hope you guys enjoy it a lot
Info
Channel: Valentin
Views: 2,453
Rating: undefined out of 5
Keywords: telegram bot, how-to, development, ruby, telegram, bot, bots, Bots Factory, BotsFactory, ruby on rails, RoR, How to build telegram bot, how to create telegram bot, how to make telegram bot, how to create telegram bot with ruby on rails
Id: X7kHe4tExKQ
Channel Id: undefined
Length: 36min 33sec (2193 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.