1. Python 3

  2. Python-telegram-bot

  3. Public API Random Dog

The first step to get started 

First, a token must be created for the robot before writing commands in the Python program. To create a token, we must have access to Telegram AIP.

 

Learn how to build a telegram robot using Python

Create a new bot in BotFather

Before building the robot, the registration process must be done. After registration, we will have access to the token and by using it, AIR Telegram can be obtained. Create a new bot by sending the / newbot command in BotFather.

Follow the relevant steps to get the relevant username and token. You can use it to access your robot:

 

 

The token obtained should look like the following code:

************* 704418931: AAEtcZ **************

Library installation

Using the following command, a library is prepared for this purpose:

 

Write a program

Now we are trying to build the first robot using the Python program . After executing all the codes, the image of a dog should be displayed on the monitor if we have followed all the steps correctly.

The importance of the library

First, list all relevant libraries using the following command:

 

 

Now access the API and URL of the photo using the thumbnail:

 

 

 

 

Obtain jason data using the URL below.

https://random.dog/woof.json

The result should look like the following code:

 

 

 

 

Now get the image URL:

 

 

 

 

Now cover the code using the get_url () function:

 

 

 

 

Send image

To send a message or image, we need an item and a parameter. For photo parameters:

  • the image URL
  • the recipient’s ID

By calling get_url () function. You can find the URL of the photo:

 

 

recipient’s ID can also be obtained using the following command:

 

 

Now that the image URL and the recipient’s ID have been obtained, it is time to send the relevant image with the following command:

 

 

The relevant code must be in a function called bop, and the resulting code must be in accordance with the following code.

 

 

 

 

The main program

Now create a function called main to run the program. Do not forget that the generated token must be replaced with the previous one:

 

 

 

 

Finally, the following code should be displayed:

 

 

 

Run the program

The construction of the Telegram robot in Python is now complete. Now its efficiency must be checked. Save its name as main.py and run the following command:

 

 

 

 

By writing the address https://telegram.me/YOUR_BOT_USERNAME. Access your telegram robot and send the / bop command. Now, if a picture of a dog is created on the monitor, all the steps have been completed successfully.

 

Run the program

Fix errors

Using this method and the codes mentioned in the above step, you have been able to create a bot that will send you funny photos of dogs whenever you want. With it, not only will you have a picture of funny dogs, but also videos and various gifts from them are available to you.

Now we are trying to be able to write a text with a few other changes along with the submitted image.

We will use regex to solve this problem. In order to be able to distinguish between existing photos, videos and gifs, we consider the final part of the relevant oral:

https://random.dog/*****.JPG

Specify the relevant files or better named:

 

 

Now we use regex to extract the corresponding horoscope from the existing oral.

 

 

Using the above code, a function called get_image_url () is created. Now we can get the relevant file with the following command.

 

 

 

 

Change the relevant code

Now in the last step, place url = get_url () in the bop () function with url = get_image_url (). The final code should look like this:

 

 

 

 

Now everything will be fine. Now you have built a telegram robot using the Python program.

The importance of building a telegram robot with Python

Telegram Messenger has undoubtedly been able to gain the status of a feature in recent years due to its extraordinary advantages and has received special attention compared to other programs of its kind.

For this reason, the creators of this program are trying to increase its efficiency and use by upgrading some of its features, and enjoy special popularity. The presence of the Telegram bot or robot option in Telegram also makes it sufficiently separate from other programs.

The method followed in the previous section helps us to build and benefit from the Telegram robot using the Python program.

Bots are an automated user account that allows the user to do interesting things, for example, when you use this program to communicate with your friends, you can easily share various videos and photos with your friends.

 

The importance of building a telegram robot with Python

 

Of course, without using them, there are other ways to share all kinds of photos and videos.

As a Telegram bot, you have to do the following steps:

  • Open the YouTube website in a web browser and go to the search section
  •  Find the video you want to share with your friends
  • Select the share via option and encounter different sharing methods
  • Now go back to Telegram and share the relevant link.

Do not use the Telegram robot

Although this method is normal for all of us and we are accustomed to do the above steps to publish photos and videos in the telegram in many cases, but in some cases the mentioned steps may not even be successful.

For example, when the internet speed is low, then you can not connect to the filter breaker to enter YouTube. Now, easily use the Telegram bot to publish the desired messages in the Telegram space. In this case, just follow the steps below:

  1.  When communicating with people when you are in the Telegram application.
  2.  You can share the word @vid with the video you selected
  3.  By clicking the submit button, the relevant video will be published in Telegram chat

Bot Telegram

There is no doubt that the second method is much easier, and you can transfer the relevant messages in less time by using the Telegram bot. To create a Telegram bot using Python, you must first have a special account in Telegram.

Once you have installed Telegram and created a separate account, look for the word botfather in the search field. After you find the chat in it and send the command newbot / first, the next steps will be automatically provided to you under this section. Now that you have completed the necessary steps in this section, you will have the following:

  •  Your special token

  •  URL for Telegram API

  •  Link for Telegram bats

These are the starting points for using the Telegram bot. At this stage, the Telegram bot has been created, but we still do not have the opportunity to benefit from it.

 

Do not use the Telegram robot

Python program

Python is one of the simple yet powerful programming languages ​​that has enabled us to improve the power and efficiency of our software by designing special code. Today from the Python program for:

  • Data science,
  • Machine learning
  • Automation of systems
  • Web Development

Programming interfaces are widely used.

Another way to build a telegram robot with Python

A cyberspace must first be created

 

 

 

 

Now activate virtualenv using the following command

 

 

 

 

Our library needs the following

We can install them using the following command:

 

 

 

 

Search for the project by yourself now:

 

 

 

 

In the credentials.py section we will need three variables:

 

 

 

 

Now enter the following commands in app.py:

 

 

 

 

The telegram URL required for / {token is called using the following command to respond to messages sent to the bot:

 

 

 

 

Now that everything is ready, it is enough to create an interesting web page with the following command:

 

 

 

 

After the previous command, the final version of app.py should look like this:

 

 

 

We use the requirements.txt file to help Heroku detect which libraries are being used.

To generate the relevant file, consider the following command:

 

 

 

 

Now use Procfile to determine where the relevant software will start. Create a Procfile using the following command:

 

 

 

 

By adding the .gitignore option you will no longer need to update it.

To build a deploy, follow these steps:

 

 

If you encounter a problem in the waiting for login section, use the following command:

 

 

 

 

Now Deploy the software:

 

 

 

 

If all the above steps are correct, you should see the following on the monitor:

 

 

 

 

Now you have the relevant Telegram bot and you can make your Telegram bot more practical by adding some items such as typing option.

 

Bot Telegram

 

The option to write along with the submitted photos is obtained through the following commands.

Obtain the following code using the respond () function:

 

 

 

 

By calling the respond () function, you can easily separate the corresponding logic from the others and create an AI bot this way:

 

 

 

Inside ai.py:

 

 

 

 

Now enter it in app.py:

 

 

 

 

Then call the previous command inside the respond () code:

 

 

 

 

Now that you have mastered the various steps of building a Telegram robot with Python, it is better to easily transfer your messages by having special bots when using Telegram.