How to run your own local Telegram Bot API server in Python [PTB v13]
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
🤖 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.
🛠️ 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
💡Upload Limit
💡Download Limit
💡Open Source Repository
💡Server Build Instructions Generator
💡Operating Systems
💡g++
💡API ID and API Hash
💡Bot.logout()
💡Environment Variable
💡CommandHandler
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
Hi welcome to this video
Today I'll be showing you how to host your own local Bot API server
We usually do this
to try and get and get around the 50 MB upload limit.
or the 20MB download limit
which is actually really less for bots.
anyway so if you want to
upload/download larger files
you can host your own server.
You can do this by going to this open source repository here
which Telegram has put here (really kind)
and I'll show you how to install this on your machine so that you can also host your own server
and get all these [features].
Here you can see the advantages of having your own local API server
And now I'll actually show you how to do this-
So if you click on [server build instructions generator]
You have to first install this [server]. So we can use this interactive page
to try and install it.
So here you have operating systems
I'm gonna use Linux cause I'm using WSL
Ubuntu
So these are some options, you don't have to
necessarily select them
I'm using g++ cause file size is apparently less than clang
So yeah I'm sticking with g++
So what I did is just copy this into a file
and just run it (shell script)
Depending upon your system, this can take upto 10 minutes.
and it depends on your network speed + computer performance
So after this is done (I have it over here)
I have my Telegram bot API thing which I have built
on my own using these instructions...
and if I actually go to that directory
you can see that all the files are here.
and if you want to run the actual Bot API server
what we need according to the instructions given
you need your api-id and api-hash.
which you obtain by going to this link
go here, enter your phone number
and you'll get two things, api hash and api id.
So you wanna copy those because you'll need them very soon.
and make sure not to give to anyone because it [can be used to delete your account]
So you just need to run the server
but before running the server, you need to make sure that your bot has logged out.
from the actual Telegram Bot API servers.
So you do this by using the Bot.logout() method
And you must use this before running your own [local api server] instance
So if I just run this [script]
just to call this logout() method,
you can see it printed True because it actually logged out from the server
and that's why I got this error.
So now I can comment that out again...
And now I actually have to run my local Bot API server. So to do this (after building)
you can first change the directory to: bin (I messed that up lol)
(messed this up...)
(still didn't realise my mistake...)
(finally!) Ok, just hold up, fixing this..
(awkward OBS moment)
Yea okay so
now that we're in the directory [telegram-bot-api/bin]
we have to just run the API server
So you do that by [entering this command]
Now we need the api-id, which I've put as an environment variable
(typing..)
Yea that's it, that's all we need.
So I run this, and of course there's an error...
(hmmm...)
(found the mistake)
let me just do this again [cut]
Okay so as you can see I got it running, just had to change some capitalization and stuff...
and now we have the server running, cause there's no output
So we now we head to our code [PTB code]
By the way, we need version 13.1+ for this to work
So I'm currently running v13.7
And now what we need is we need to comment this line out
And we have to put in our server address.
So in my case, the server address as you can see
this is the wiki page in case you ever forget what to do
(reading it out) You have to pass this in your Updater or Bot.
So since I'm running locally I don't have a domain
So I'm just using http:0.0.0.0
And by default it runs on port 8081
So when you do this and you run you should be able to
run your own bot API server and I'll show you it.
Okay so as you can see I just ran my [python] file
ran the script
and now I have a CommandHandler to check if I'm actually running
through my local API server [which goes here]. it prints my base_url along with the process id
And now I'm gonna check
let's check.. [entered /check]
here we go so now we know that the
bot is actually running through a local API server and not through Telegram's API server which is great
because now we can actually send large files [greater than 50MB]
You can see it goes [in this function]
And here we go! We have larger than a 50MB file
which means our server works!
Thank you for watching and I'll see you next time.
関連動画をさらに表示
Asp.Net Core Web API Client/Server Application | Visual Studio 2019
Server Side Rendering with Vue.js 3
2 | How to Install a Local Server for PHP | 2023 | Learn PHP Full Course for Beginners
Laravel deployment with GitHub Actions
Konfigurasi DHCP Server dan DHCP Client pada Router Mikrotik RB 951Ui-2HnD
How to install Private Server | Genshin Impact (2024)
5.0 / 5 (0 votes)