Telegram Bot with Python – How to Make Telegram Bot with Python
In this article we will write a Telegramm bot using Python language. Before we go to technical department, let’s examine the concepts.
Telegram : telegram is an instant messaging service that offers multi-platform support. Telegram clients are available for both mobile (Android, iOS, Windows Phone, Ubuntu touch) and desktop systems (Windows, OS X, Linux).[1]]
Bot:
it is a common term used in the sense of “robot” in the world. Robots that can perform many computer operations semi-automatically are used in all areas of Information Technology.[2]]
We talked about concepts as a foundation, so let’s talk about what we’re going to do now.:
Aim: to send dynamic data to our phone at regular intervals. For example: to receive the weather every day as a message, to receive the announcements on the school page every week as a message, etc.
Requirements:
python 3.X, BeatifulSoup 4, Telegraph account and basic python knowledge(including package installation).
How to Make Python Telegram Bot:
In my example, “http://guzelsozlerblog.com/gunun-sozu I will draw the words of the day according to the topics on the page and send these words as a message to my phone via telegramma. I will try to explain it step by step in my best possible way, so the article may be long. Don’t lose your motivation when you see the long article, and you will see it as easy when you look at it on the surface.
Step 1: open an empty Python file (for example: sozBotu.py( see). First, let’s access the web site where we want to extract data and try to get the entire page. We’il use the requests Library for this.
1 2 3 4 | import requests url="http://guzelsozlerblog.com/gunun-sozu" response=requests.get(url) print(response.text) |
Step 2: We will filter the parts we want to get from the web page output we have obtained. In my example, the information he wanted to retrieve was among the “div id=”PageContent” tags. For this reason, only these tags and contents need. To do this, we use the BeatifulSoup library. In this section we will see that there is an array of returned data.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import requests from bs4 import BeautifulSoup url="http://guzelsozlerblog.com/gunun-sozu" response=requests.get(url) print(response.text) AnalizEdilecekIcerik=response.text icerigiAl=BeautifulSoup(AnalizEdilecekIcerik,"lxml") filtre=icerigiAl.find_all(id="PageContent") print(filtre) print(filtre[0]) |
Step 3: we will create the algorithm to print the data we have taken as desired.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import requests from bs4 import BeautifulSoup url="http://guzelsozlerblog.com/gunun-sozu" response=requests.get(url) <span style="color:#ff0000;">response.encoding= "iso-8859-9"</span> AnalizEdilecekIcerik=response.text icerigiAl=BeautifulSoup(AnalizEdilecekIcerik,"lxml") sozler=icerigiAl.select("div#PageContent") for i in range(0,len(sozler)): print(sozler[i].contents[0].text +": "+sozler[i].contents[2]+"\n") |
Step 4: Now, we will make the settings we need to do in the Telegramm application.
4.1. Let’s call the pre-set user named bottather. To do this from the desktop computer https://telegram.me/BotFather you can use the connection.
4.2. Follow the instructions below. you can change expressions like gununsozubot and Gununsozot by yourself. As our bot father said, You have to pay attention: the second name has to end with “bot”. Save the one-line token code your father gave you.
4.3. This time give the father the command “/mybots”. Your bot — your bot will be listed. Since I had a bot before it was also visible, it will only be listed as a bot since you are doing it for the first time. Choose your boat.
4.4. Activate the bot with the “Start” button. A boat that transmits the words of the Day Every day. you can type ” /setdescription “via BotFather.