I used botfather to create a telegram bot. I used the generated bot token in my python code. I also used python-telegram-bot package to handle my codes. The problem was the file size limitation because of the bot-telegram-api and its http-interface provided by telegram. The solution I found is this: Using a Local Bot API Server
First question:
Now, I don't know how to create a bot to use my local API Server to prevent file size limitation! I've created an application in my.telegram.org and used the given api_hash and api_id in my local API Server. Does anyone know how should I create a bot without using botfather?! Or should I change some settings in botfather to use my local API Server instead of telegram's original server?
Second question:
Can I still use the python-telegram-bot package?!
I tried to log-out my bot and set local_mode=True and base_url parameters. But, when I run the code, I get connection time out error. I also containerized my codes for convenience.
Here are my codes:
from telegram.ext import Application, CommandHandler
def main():
builder = Application.builder()
builder.token(VALUES['BOT_TOKEN'])
builder.local_mode(local_mode=True)
builder.base_url('http://bot-api:8081/bot')
app = builder.build()
app.add_handler(CommandHandler('help', help_command))
app.run_polling(poll_interval=10)
# app.bot.logOut()
# print('bot logged out!!!')
# exit(0)
I did run the local API server like this:
telegram-bot-api --api-id=++++ --api-hash=++++ --local -v 3
This is my code error:
bot-1 | bot is running
bot-1 | Traceback (most recent call last):
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_transports/default.py", line 69, in map_httpcore_exceptions
bot-1 | yield
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_transports/default.py", line 373, in handle_async_request
bot-1 | resp = await self._pool.handle_async_request(req)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 216, in handle_async_request
bot-1 | raise exc from None
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 196, in handle_async_request
bot-1 | response = await connection.handle_async_request(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/connection.py", line 101, in handle_async_request
bot-1 | return await self._connection.handle_async_request(request)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/http11.py", line 143, in handle_async_request
bot-1 | raise exc
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/http11.py", line 113, in handle_async_request
bot-1 | ) = await self._receive_response_headers(**kwargs)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/http11.py", line 186, in _receive_response_headers
bot-1 | event = await self._receive_event(timeout=timeout)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_async/http11.py", line 224, in _receive_event
bot-1 | data = await self._network_stream.read(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 32, in read
bot-1 | with map_exceptions(exc_map):
bot-1 | File "/usr/local/lib/python3.11/contextlib.py", line 155, in __exit__
bot-1 | self.gen.throw(typ, value, traceback)
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
bot-1 | raise to_exc(exc) from exc
bot-1 | httpcore.ReadTimeout
bot-1 |
bot-1 | The above exception was the direct cause of the following exception:
bot-1 |
bot-1 | Traceback (most recent call last):
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/request/_httpxrequest.py", line 276, in do_request
bot-1 | res = await self._client.request(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1574, in request
bot-1 | return await self.send(request, auth=auth, follow_redirects=follow_redirects)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/sentry_sdk/integrations/httpx.py", line 137, in send
bot-1 | rv = await real_send(self, request, **kwargs)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1661, in send
bot-1 | response = await self._send_handling_auth(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1689, in _send_handling_auth
bot-1 | response = await self._send_handling_redirects(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1726, in _send_handling_redirects
bot-1 | response = await self._send_single_request(request)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_client.py", line 1763, in _send_single_request
bot-1 | response = await transport.handle_async_request(request)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_transports/default.py", line 372, in handle_async_request
bot-1 | with map_httpcore_exceptions():
bot-1 | File "/usr/local/lib/python3.11/contextlib.py", line 155, in __exit__
bot-1 | self.gen.throw(typ, value, traceback)
bot-1 | File "/usr/local/lib/python3.11/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
bot-1 | raise mapped_exc(message) from exc
bot-1 | httpx.ReadTimeout
bot-1 |
bot-1 | The above exception was the direct cause of the following exception:
bot-1 |
bot-1 | Traceback (most recent call last):
bot-1 | File "/code/main.py", line 208, in <module>
bot-1 | main()
bot-1 | File "/code/main.py", line 204, in main
bot-1 | app.run_polling(poll_interval=10)
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_application.py", line 836, in run_polling
bot-1 | return self.__run(
bot-1 | ^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_application.py", line 1036, in __run
bot-1 | raise exc
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_application.py", line 1025, in __run
bot-1 | loop.run_until_complete(self.initialize())
bot-1 | File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
bot-1 | return future.result()
bot-1 | ^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_application.py", line 495, in initialize
bot-1 | await self.bot.initialize()
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_extbot.py", line 293, in initialize
bot-1 | await super().initialize()
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/_bot.py", line 767, in initialize
bot-1 | await self.get_me()
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_extbot.py", line 1873, in get_me
bot-1 | return await super().get_me(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/_bot.py", line 541, in decorator
bot-1 | result = await func(self, *args, **kwargs)
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/_bot.py", line 902, in get_me
bot-1 | result = await self._post(
bot-1 | ^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/_bot.py", line 629, in _post
bot-1 | return await self._do_post(
bot-1 | ^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/ext/_extbot.py", line 347, in _do_post
bot-1 | return await super()._do_post(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/_bot.py", line 657, in _do_post
bot-1 | return await request.post(
bot-1 | ^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/request/_baserequest.py", line 200, in post
bot-1 | result = await self._request_wrapper(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/request/_baserequest.py", line 340, in _request_wrapper
bot-1 | raise exc
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/request/_baserequest.py", line 330, in _request_wrapper
bot-1 | code, payload = await self.do_request(
bot-1 | ^^^^^^^^^^^^^^^^^^^^^^
bot-1 | File "/usr/local/lib/python3.11/site-packages/telegram/request/_httpxrequest.py", line 293, in do_request
bot-1 | raise TimedOut from err
bot-1 | telegram.error.TimedOut: Timed out
bot-1 | Sentry is attempting to send 2 pending events
bot-1 | Waiting up to 2 seconds
bot-1 | Press Ctrl-C to quit
bot-1 exited with code 0
You create the bot with Botfather/use the existing one. The token doesn't even change. Swithing to your local api server just requires calling the logOut
method once, as explained here. Note that PTB also has a dedicated wiki page on using local api servers.
Disclaimer: I'm currently the maintainer of python-telegram-bot
.