{"id":22663613,"url":"https://github.com/zeph1997/telegram-bot-tutorial","last_synced_at":"2025-10-28T00:17:43.871Z","repository":{"id":40957012,"uuid":"260213976","full_name":"zeph1997/telegram-bot-tutorial","owner":"zeph1997","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-08T11:52:19.000Z","size":1259,"stargazers_count":0,"open_issues_count":9,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-03T23:50:08.297Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zeph1997.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-30T13:06:46.000Z","updated_at":"2021-02-12T15:22:05.000Z","dependencies_parsed_at":"2023-01-25T05:15:46.468Z","dependency_job_id":null,"html_url":"https://github.com/zeph1997/telegram-bot-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeph1997%2Ftelegram-bot-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeph1997%2Ftelegram-bot-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeph1997%2Ftelegram-bot-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeph1997%2Ftelegram-bot-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeph1997","download_url":"https://codeload.github.com/zeph1997/telegram-bot-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246162148,"owners_count":20733357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-09T12:31:16.529Z","updated_at":"2025-10-28T00:17:38.837Z","avatar_url":"https://github.com/zeph1997.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# telegram-bot-tutorial\n\n\u003cb\u003eWhat you will learn:\u003c/b\u003e\n\u003col\u003e\n  \u003cli\u003eA brief intro to the basic building blocks of Websites (HTML, CSS)\u003c/li\u003e\n  \u003cli\u003eHow to import packages to your project\u003c/li\u003e\n  \u003cli\u003eHow to connect to Telegram\u003c/li\u003e\n  \u003cli\u003eHow to webscrape\u003c/li\u003e\n  \u003cli\u003eHow to respond to user input\u003c/li\u003e\n\u003c/ol\u003e\n\u003cbr\u003e\n\u003cb\u003eWhat you will need to have:\u003c/b\u003e\n\u003col\u003e\n  \u003cli\u003ePython installed (minimum python 3). Please click \u003ca href=\"https://www.python.org/downloads/\"\u003ehere\u003c/a\u003e if you have not done so\u003c/li\u003e\n  \u003cli\u003eAn IDE. Any IDE is fine, but preferably VS Code so it will be easier to follow. Please click \u003ca href=\"https://code.visualstudio.com/\"\u003ehere\u003c/a\u003e if you wish to download VS Code.\u003c/li\u003e\n  \u003cli\u003eA Telegram Account\u003c/li\u003e\n\u003c/ol\u003e\n\u003cbr\u003e\n\u003ch2\u003e1. A brief intro to the basic building blocks of Websites\u003c/h2\u003e\nHTML stands for Hyper Text Markup Language and CSS stands for Cascading Style Sheet. These are the building blocks of all websites around the world.\u003cbr\u003e\n\u003cbr\u003e\n\u003cimg src=\"/README/inspect.png\"\u003e\u003cbr\u003e\nRight 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.\u003cbr\u003e\n\u003cbr\u003e\nIn 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.\n\u003cbr\u003e\n\u003ch2\u003e2. How to import packages to your project\u003c/h2\u003e\nDownload this repository and open up the \u003ccode\u003emain.py\u003c/code\u003e file using VS Code or any IDE of your preference.\u003cbr\u003e\nYou will see \u003ccode\u003eimport\u003c/code\u003e 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.\u003cbr\u003e\n\u003cbr\u003e\n\u003cb\u003eFollow these steps to install pyTelegramBotAPI and BeautifulSoup:\u003c/b\u003e\u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 1:\u003c/b\u003e Go to the search bar and type \u003ccode\u003ecmd\u003c/code\u003e \u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 2:\u003c/b\u003e Click on command prompt \u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 3:\u003c/b\u003e Type in \u003ccode\u003epip install pyTelegramBotAPI\u003c/code\u003e. (This is an API written by \u003ca href=\"https://github.com/eternnoir/pyTelegramBotAPI\"\u003eeternnoir\u003c/a\u003e that allows us to connect to Telegram using python)\u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 4:\u003c/b\u003e Go to \u003ccode\u003emain.py\u003c/code\u003e on your IDE and fill in line 1 with \u003ccode\u003eimport telebot\u003c/code\u003e\u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 5:\u003c/b\u003e Go back to the command prompt and type \u003ccode\u003epip install beautifulsoup4\u003c/code\u003e (This is an API to help us extract out the contents from websites)\u003cbr\u003e\n\u0026emsp;\u003cb\u003eStep 6:\u003c/b\u003e Go back to \u003ccode\u003emain.py\u003c/code\u003e on your IDE and fill in line 2 with \u003ccode\u003efrom bs4 import BeautifulSoup\u003c/code\u003e (This helps us import only the necessary functions needed for our process) \u003cbr\u003e\n\u003cbr\u003e\nNow you have all the tools to start building your own webscraping bot!\n\u003cbr\u003e\n\u003ch2\u003e3. How to connect to Telegram\u003c/h2\u003e\nTime for us to create a bot! Go into Telegram and search for \u003ccode\u003eBotFather\u003c/code\u003e\u003cbr\u003e\n\u003cbr\u003e\nTap \"START\" and type \u003ccode\u003e/newbot\u003c/code\u003e 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. \u003cbr\u003e\n\u003cimg src=\"/README/apikey.png\"\u003e\u003cbr\u003e\n\u003cb\u003ePlease do not share this token as anyone with it will be able to control your bot!\u003c/b\u003e\u003cbr\u003e\n\u003cbr\u003e\nCopy the API Token and go back to \u003ccode\u003emain.py\u003c/code\u003e. 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.\u003cbr\u003e\n\u003cbr\u003e\nNow your bot is connected to Telegram!\u003cbr\u003e\n\u003ch2\u003e4. How to Webscrape\u003c/h2\u003e\nAs 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)\u003cbr\u003e\n\u003ch2\u003e5. How to respond to user input on Telegram\u003c/h2\u003e\nTo 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 \u003ccode\u003ecommands=[\"start\"]\u003c/code\u003e 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 \u003ccode\u003e/start\u003c/code\u003e\u003cbr\u003e\n\u003cbr\u003e\nAs mentioned above, the message handlers are always followed with a function (e.g. line 11). They usually start with the word \u003ccode\u003edef\u003c/code\u003e 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 \u003ccode\u003e/\u003c/code\u003e\u003cbr\u003e\n\u003cbr\u003e\nTo reply to a specific user, use the \u003ccode\u003e.send_message\u003c/code\u003e 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 \u003ccode\u003emessage.chat.id\u003c/code\u003e 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 \u003ccode\u003eprint()\u003c/code\u003e the variable to be able to see the contents in greater detail (which will be useful for debugging).\u003cbr\u003e\n\u003cbr\u003e\nAfter putting the chat ID of the user to reply, add a comma \u003ccode\u003e,\u003c/code\u003e 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.\u003cbr\u003e\n\u003cbr\u003e\nNow add \u003ccode\u003ebot.polling\u003c/code\u003e way below your code (as seen on line 39) to get the bot to listen on Telegram. \u003cb\u003eMake sure it is at the end of all the code as the bot will not read the code after the polling function is called.\u003c/b\u003e\u003cbr\u003e\n\u003cbr\u003e\n\u003ch2\u003eWell Done!\u003c/h2\u003e\nAndddd..... you're all set! Now just right click on the IDE and click \u003ccode\u003eRun Python File in Terminal\u003c/code\u003e 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! \u003cbr\u003e\nIf you want to take a look at how it should be working, you can check it out in my \u003ca href=\"https://github.com/zeph1997/telegram-bot-stocks\"\u003etelegram-bot-stocks\u003c/a\u003e repository!\n\n\u003ch2\u003eAdditional Resources for Further Learning\u003c/h2\u003e\nIf you want to learn more about HTML, click \u003ca href=\"https://www.w3schools.com/html/default.asp\"\u003ehere\u003c/a\u003e. If you would like to learn more about Python, click \u003ca href=\"https://www.w3schools.com/python/\"\u003ehere\u003c/a\u003e. Both resources are by W3 Schools. They have great resources for you to learn!\u003cbr\u003e\n\u003cbr\u003e\nAll the best on your development journey!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeph1997%2Ftelegram-bot-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeph1997%2Ftelegram-bot-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeph1997%2Ftelegram-bot-tutorial/lists"}