telegram-bot-tutorial

What you will learn:

  1. A brief intro to the basic building blocks of Websites (HTML, CSS)
  2. How to import packages to your project
  3. How to connect to Telegram
  4. How to webscrape
  5. How to respond to user input

What you will need to have:
  1. Python installed (minimum python 3). Please click here if you have not done so
  2. An IDE. Any IDE is fine, but preferably VS Code so it will be easier to follow. Please click here if you wish to download VS Code.
  3. A Telegram Account

1. A brief intro to the basic building blocks of Websites

HTML stands for Hyper Text Markup Language and CSS stands for Cascading Style Sheet. These are the building blocks of all websites around the world.


Right click on any website page and click on "Inspect". You will see a window on the side of the page. That is content of the page. The computer takes that chunk of text and converts it into the nice pages you see today. HTML produces the content, while CSS helps style the website to make it look nice and colourful.

In this workshop we will not be writing any HTML or CSS, but it is necessary for us to understand how the website looks like under the hood to be able to webscrape.

2. How to import packages to your project

Download this repository and open up the main.py file using VS Code or any IDE of your preference.
You will see import on the first two lines. We will have to utilise some packages (which are code other people wrote) to help us connect to Telegram. In order to get the package to connect to Telegram, we need to use the command prompt.

Follow these steps to install pyTelegramBotAPI and BeautifulSoup:
Step 1: Go to the search bar and type cmd
Step 2: Click on command prompt
Step 3: Type in pip install pyTelegramBotAPI. (This is an API written by eternnoir that allows us to connect to Telegram using python)
Step 4: Go to main.py on your IDE and fill in line 1 with import telebot
Step 5: Go back to the command prompt and type pip install beautifulsoup4 (This is an API to help us extract out the contents from websites)
Step 6: Go back to main.py on your IDE and fill in line 2 with from bs4 import BeautifulSoup (This helps us import only the necessary functions needed for our process)

Now you have all the tools to start building your own webscraping bot!

3. How to connect to Telegram

Time for us to create a bot! Go into Telegram and search for BotFather

Tap "START" and type /newbot to create your own bot. Follow the instructions to give your bot any name you like. Once you've given the bot a name and username, BotFather will give you an API Token.


Please do not share this token as anyone with it will be able to control your bot!

Copy the API Token and go back to main.py. Paste the API Token in line 5, inside the inverted commas. Make sure that the API Token has the inverted commas before and after it.

Now your bot is connected to Telegram!

4. How to Webscrape

As mentioned in the first section, the content on the websites are mostly in HTML. As such, we can find the target elements and extract the specific information. Lucky for us, we have BeautifulSoup to help us with that. BeautifulSoup can isolate elements in the page will make it easier for us to extract a certain piece of information on the website (e.g. the text of the price of an item on the website)

5. How to respond to user input on Telegram

To reply to a message, we have to include a message handler. You can see these on line 10, 14, and 25. They will handle the responses of the specified user input. Let us use line 10 as an example. You can see that commands=["start"] is within the brackets of the message handler. This means that the following code underneath the message handler will be executed when the user types /start

As mentioned above, the message handlers are always followed with a function (e.g. line 11). They usually start with the word def followed by the name of the function and a variable as a parameter. The name of the function can be any name decided by you. The parameter refers to the message sent by the user. You can extract useful information such as the chat ID of the user (to be used to reply to them) or even the whole text of the message. Take note that any command messages have to be preceeded with a /

To reply to a specific user, use the .send_message function (as seen on line 12). The required arugement is the user chat id, which can be retrieved from the parameter as mentioned in the paragraph above. Use message.chat.id to get the chat ID. For those who are interested, the message information retrieved as a parameter for the function is a dictionary, which allows it to store mutliple data as long as we know how to access them. You can always print() the variable to be able to see the contents in greater detail (which will be useful for debugging).

After putting the chat ID of the user to reply, add a comma , and start typing the message that you would like to send to the user. Do remember that the message will have to be a string data type so you have to enclose your text in inverted commas.

Now add bot.polling way below your code (as seen on line 39) to get the bot to listen on Telegram. Make sure it is at the end of all the code as the bot will not read the code after the polling function is called.

Well Done!

Andddd..... you're all set! Now just right click on the IDE and click Run Python File in Terminal and you can start using your bot! Do note that the file has to be running in the terminal in order for the bot to be live. To shut down the bot, you can use CTRL+C in the terminal. Congratulations on creating your first bot!
If you want to take a look at how it should be working, you can check it out in my telegram-bot-stocks repository!

Additional Resources for Further Learning

If you want to learn more about HTML, click here. If you would like to learn more about Python, click here. Both resources are by W3 Schools. They have great resources for you to learn!

All the best on your development journey!