How to run your own local Telegram Bot API server in Python [PTB v13]

Harshil
9 Aug 202107:39

Summary

TLDRThis tutorial video guides viewers on setting up a local Bot API server to bypass Telegram's file size limitations. It demonstrates using an open-source repository and server build instructions to install the server on Linux with g++. The process involves obtaining an API ID and hash, ensuring the bot is logged out from Telegram's servers, and configuring the local server with the correct environment variables. The video concludes with a successful demonstration of the bot running through the local API server, now capable of handling files larger than 50MB.

Takeaways

  • πŸ€– The video is a tutorial on hosting a local Bot API server to bypass the file size limits imposed by Telegram's API.
  • πŸ”— The server can be set up using an open-source repository provided by Telegram.
  • πŸ“š Follow the server build instructions generator for step-by-step installation on your machine.
  • πŸ’» The video demonstrates the process using Linux with WSL (Windows Subsystem for Linux) and g++ compiler.
  • ⏱️ The installation process may take up to 10 minutes depending on network speed and computer performance.
  • πŸ”‘ To run the server, you need an 'api-id' and 'api-hash', which can be obtained from a specific Telegram link after entering your phone number.
  • 🚫 Ensure that the 'api-id' and 'api-hash' are kept secure and not shared, as they can be used to delete your account.
  • πŸ“‹ Before running the local server, the bot must be logged out from the actual Telegram Bot API servers using the Bot.logout() method.
  • πŸ› οΈ After building, navigate to the 'bin' directory to run the API server with the necessary environment variables.
  • πŸ”„ The server can be started with a command, adjusting for any capitalization or syntax errors as needed.
  • πŸ”„ Update your Python Telegram Bot (PTB) code to use the local server by commenting out the default line and adding the server address.
  • πŸ“‘ The local server runs on 'http://0.0.0.0' by default and uses port 8081, allowing for the handling of files larger than 50MB.

Q & A

  • What is the main purpose of hosting your own local Bot API server as described in the video?

    -The main purpose is to bypass the 50 MB upload limit and the 20 MB download limit for bots, allowing for the upload and download of larger files.

  • Where can one find the open source repository mentioned in the video to host their own server?

    -The open source repository can be found on Telegram's provided link, which is mentioned in the video.

  • What are the advantages of having your own local API server according to the video?

    -The advantages include the ability to handle larger files and potentially improved control and customization over your bot's API server.

  • What is the 'server build instructions generator' mentioned in the video?

    -It is an interactive page that helps users install the server on their machine by providing step-by-step instructions based on the user's operating system and preferences.

  • Why does the video recommend using g++ over clang for compiling the server?

    -The video mentions that g++ is chosen because the file size is apparently less than that of clang.

  • How long does the server installation process take according to the video?

    -The installation process can take up to 10 minutes, depending on the user's network speed and computer performance.

  • What are 'api-id' and 'api-hash', and how does one obtain them?

    -The 'api-id' and 'api-hash' are credentials required to run the Bot API server. They can be obtained by visiting a specific link provided in the video, entering your phone number, and receiving these credentials.

  • Why is it important to ensure that the bot has logged out from the actual Telegram Bot API servers before running the local server?

    -It is important to avoid conflicts and ensure that the bot operates exclusively through the local API server instance once it is set up.

  • What command is used to run the local Bot API server after building it, according to the video?

    -The video does not provide the exact command, but it mentions that the server can be run by entering a command in the 'telegram-bot-api/bin' directory with the 'api-id' set as an environment variable.

  • What version of the Python Telegram Bot (PTB) library is required to work with the local API server?

    -The video states that version 13.1 or higher is required, with the presenter running version 13.7.

  • How can one verify that the bot is running through the local API server instead of Telegram's API server?

    -The video suggests using a CommandHandler to check the base_url and process id, and by testing the ability to send files larger than 50MB, which is not possible through Telegram's API server.

Outlines

00:00

πŸ€– Hosting a Local Bot API Server

This paragraph introduces the concept of hosting a local Bot API server to bypass the upload and download limits imposed by Telegram's official servers. The speaker explains that by hosting their own server, users can handle larger files, which is particularly beneficial for bots. The process begins with accessing an open-source repository provided by Telegram and following the installation instructions for a Linux-based system using WSL and g++. The speaker emphasizes the importance of having the correct tools and environment, and provides a brief overview of the steps involved, including compiling the server and obtaining necessary credentials like api-id and api-hash from Telegram.

05:04

πŸ› οΈ Setting Up and Running the Local Bot API Server

The second paragraph delves into the technical setup of the local Bot API server. The speaker discusses the initial errors encountered and the corrections made, such as adjusting capitalization in the commands. Once the server is successfully running, the focus shifts to integrating it with the Python Telegram Bot (PTB) library, requiring version 13.1 or higher. The speaker guides through the necessary code modifications, including commenting out certain lines and updating the server address to point to the local instance. The local server's functionality is confirmed by checking the base_url and process id, and demonstrating the ability to send files larger than 50MB, which would not be possible with Telegram's standard API server. The paragraph concludes with a successful demonstration and a thank you note to the viewers.

Mindmap

Keywords

πŸ’‘Local Bot API Server

A local Bot API server is a self-hosted server that allows users to bypass the upload and download limits imposed by third-party platforms, such as Telegram's 50 MB upload limit. In the video, the host demonstrates how to set up such a server to facilitate the handling of larger files, which is central to the video's theme of overcoming file size restrictions.

πŸ’‘Upload Limit

The upload limit refers to the maximum file size that can be sent to a server or platform. In the context of the video, the host mentions a 50 MB upload limit as a reason for hosting a local Bot API server, highlighting the need for larger file transfers beyond what is allowed by default on platforms like Telegram.

πŸ’‘Download Limit

Similar to the upload limit, the download limit is the maximum file size that can be received from a server or platform. The script mentions a 20 MB download limit for bots, which is a key motivation for hosting a local server to bypass these restrictions and handle larger files.

πŸ’‘Open Source Repository

An open source repository is a public codebase where developers can collaborate and contribute to a project. In the video, the host references an open source repository provided by Telegram for hosting a local Bot API server, emphasizing the community aspect and the availability of resources for such projects.

πŸ’‘Server Build Instructions Generator

The server build instructions generator is a tool mentioned in the script that helps users to install a server on their machine. It is an interactive page that guides users through the process of setting up a local Bot API server, which is crucial for the video's tutorial on self-hosting.

πŸ’‘Operating Systems

Operating systems are the software that manages computer hardware and software resources. In the video, the host chooses Linux, specifically WSL (Windows Subsystem for Linux), to demonstrate the installation of the local Bot API server, showcasing the compatibility of the process with different operating systems.

πŸ’‘g++

g++ is a compiler used for programming languages like C++. The host mentions using g++ over clang due to file size considerations, indicating a preference for certain tools based on specific project requirements in the context of building the server.

πŸ’‘API ID and API Hash

API ID and API Hash are unique identifiers and authentication keys required to interact with a server's API. The video script instructs viewers on obtaining these credentials from Telegram and emphasizes their importance for running the local Bot API server securely.

πŸ’‘Bot.logout()

The Bot.logout() method is a function used to log out a bot from the Telegram Bot API servers. In the script, the host uses this method to ensure the bot is logged out before running the local server, illustrating a necessary step in the transition from using the default API to a custom one.

πŸ’‘Environment Variable

Environment variables are settings in the operating system that affect the way running processes behave. In the video, the host sets the API ID as an environment variable for the local Bot API server, demonstrating a common practice for configuring applications with required parameters.

πŸ’‘CommandHandler

A CommandHandler in the context of the video is a piece of code that processes commands sent to the bot. The host uses a CommandHandler to verify that the bot is running through the local API server, showing an example of how to test and confirm the functionality of the self-hosted server.

Highlights

Introduction to hosting a local Bot API server to bypass the 50 MB upload and 20 MB download limits for bots.

Using an open-source repository provided by Telegram for hosting the server.

Installation process explained via server build instructions generator.

Choosing the right operating system and compiler for the installation.

Copying and running a shell script to install the server, which may take up to 10 minutes depending on system specs and network speed.

Obtaining api-id and api-hash from Telegram for server authentication.

The importance of keeping api-id and api-hash secure to prevent unauthorized account access.

Ensuring the bot has logged out from the official Telegram servers before running the local API server.

Demonstration of using the Bot.logout() method to log out the bot from the official servers.

Running the local Bot API server after building, including changing to the bin directory.

Using environment variables for api-id during the server run command.

Troubleshooting common errors during the server setup, such as capitalization mistakes.

Verifying the successful setup of the local server with no output indicating proper operation.

Updating the Python code to use the local server address instead of the default Telegram API.

Requirement of PTB version 13.1+ for the local server to work properly.

Demonstration of the bot running through the local API server with a CommandHandler.

Capability to send files larger than 50MB using the local API server.

Final confirmation of the bot's operation through the local server with a successful file upload test.

Conclusion and appreciation for watching the tutorial on setting up a local Bot API server.

Transcripts

play00:00

Hi welcome to this video

play00:03

Today I'll be showing you how to host your own local Bot API server

play00:07

We usually do this

play00:09

to try and get and get around the 50 MB upload limit.

play00:13

or the 20MB download limit

play00:16

which is actually really less for bots.

play00:19

anyway so if you want to

play00:22

upload/download larger files

play00:27

you can host your own server.

play00:28

You can do this by going to this open source repository here

play00:35

which Telegram has put here (really kind)

play00:40

and I'll show you how to install this on your machine so that you can also host your own server

play00:47

and get all these [features].

play00:48

Here you can see the advantages of having your own local API server

play00:54

And now I'll actually show you how to do this-

play00:57

So if you click on [server build instructions generator]

play01:02

You have to first install this [server]. So we can use this interactive page

play01:08

to try and install it.

play01:10

So here you have operating systems

play01:14

I'm gonna use Linux cause I'm using WSL

play01:18

Ubuntu

play01:20

So these are some options, you don't have to

play01:26

necessarily select them

play01:28

I'm using g++ cause file size is apparently less than clang

play01:34

So yeah I'm sticking with g++

play01:37

So what I did is just copy this into a file

play01:43

and just run it (shell script)

play01:45

Depending upon your system, this can take upto 10 minutes.

play01:56

and it depends on your network speed + computer performance

play02:02

So after this is done (I have it over here)

play02:06

I have my Telegram bot API thing which I have built

play02:12

on my own using these instructions...

play02:16

and if I actually go to that directory

play02:20

you can see that all the files are here.

play02:23

and if you want to run the actual Bot API server

play02:27

what we need according to the instructions given

play02:31

you need your api-id and api-hash.

play02:33

which you obtain by going to this link

play02:38

go here, enter your phone number

play02:44

and you'll get two things, api hash and api id.

play02:47

So you wanna copy those because you'll need them very soon.

play02:51

and make sure not to give to anyone because it [can be used to delete your account]

play02:55

So you just need to run the server

play03:02

but before running the server, you need to make sure that your bot has logged out.

play03:08

from the actual Telegram Bot API servers.

play03:14

So you do this by using the Bot.logout() method

play03:18

And you must use this before running your own [local api server] instance

play03:22

So if I just run this [script]

play03:27

just to call this logout() method,

play03:29

you can see it printed True because it actually logged out from the server

play03:33

and that's why I got this error.

play03:36

So now I can comment that out again...

play03:42

And now I actually have to run my local Bot API server. So to do this (after building)

play03:52

you can first change the directory to: bin (I messed that up lol)

play03:56

(messed this up...)

play04:05

(still didn't realise my mistake...)

play04:09

(finally!) Ok, just hold up, fixing this..

play04:19

(awkward OBS moment)

play04:23

Yea okay so

play04:25

now that we're in the directory [telegram-bot-api/bin]

play04:28

we have to just run the API server

play04:31

So you do that by [entering this command]

play04:34

Now we need the api-id, which I've put as an environment variable

play04:43

(typing..)

play05:03

Yea that's it, that's all we need.

play05:07

So I run this, and of course there's an error...

play05:10

(hmmm...)

play05:17

(found the mistake)

play05:20

let me just do this again [cut]

play05:24

Okay so as you can see I got it running, just had to change some capitalization and stuff...

play05:28

and now we have the server running, cause there's no output

play05:34

So we now we head to our code [PTB code]

play05:38

By the way, we need version 13.1+ for this to work

play05:43

So I'm currently running v13.7

play05:46

And now what we need is we need to comment this line out

play05:55

And we have to put in our server address.

play05:59

So in my case, the server address as you can see

play06:03

this is the wiki page in case you ever forget what to do

play06:07

(reading it out) You have to pass this in your Updater or Bot.

play06:13

So since I'm running locally I don't have a domain

play06:17

So I'm just using http:0.0.0.0

play06:22

And by default it runs on port 8081

play06:27

So when you do this and you run you should be able to

play06:31

run your own bot API server and I'll show you it.

play06:36

Okay so as you can see I just ran my [python] file

play06:40

ran the script

play06:45

and now I have a CommandHandler to check if I'm actually running

play06:50

through my local API server [which goes here]. it prints my base_url along with the process id

play06:56

And now I'm gonna check

play07:02

let's check.. [entered /check]

play07:07

here we go so now we know that the

play07:10

bot is actually running through a local API server and not through Telegram's API server which is great

play07:16

because now we can actually send large files [greater than 50MB]

play07:20

You can see it goes [in this function]

play07:28

And here we go! We have larger than a 50MB file

play07:33

which means our server works!

play07:35

Thank you for watching and I'll see you next time.

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Bot APILocal ServerFile UploadsLarge FilesTelegram BotsAPI HostingOpen SourceLinux SetupWSL UbuntuAPI IDAPI Hash