https://github.com/zeph1997/telegram-bot-tutorial
https://github.com/zeph1997/telegram-bot-tutorial
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/zeph1997/telegram-bot-tutorial
- Owner: zeph1997
- Created: 2020-04-30T13:06:46.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T11:52:19.000Z (over 2 years ago)
- Last Synced: 2025-02-03T23:50:08.297Z (4 months ago)
- Language: Python
- Size: 1.2 MB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# telegram-bot-tutorial
What you will learn:
- A brief intro to the basic building blocks of Websites (HTML, CSS)
- How to import packages to your project
- How to connect to Telegram
- How to webscrape
- How to respond to user input
What you will need to have:
- Python installed (minimum python 3). Please click here if you have not done so
- 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.
- 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 themain.py
file using VS Code or any IDE of your preference.
You will seeimport
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 typecmd
Step 2: Click on command prompt
Step 3: Type inpip install pyTelegramBotAPI
. (This is an API written by eternnoir that allows us to connect to Telegram using python)
Step 4: Go tomain.py
on your IDE and fill in line 1 withimport telebot
Step 5: Go back to the command prompt and typepip install beautifulsoup4
(This is an API to help us extract out the contents from websites)
Step 6: Go back tomain.py
on your IDE and fill in line 2 withfrom 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 forBotFather
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 tomain.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 thatcommands=["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 worddef
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. Usemessage.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 alwaysprint()
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 addbot.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 clickRun 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!