Forums

Telegram bot python-telegram-bot

Firstly, many thanks to giles for the blog post to use telepot for Telegram. It is a simple but yet great place to start!

Now that I have a bot up and running using Telepot + Django, I wanted to port it over to python-telegram-bot to take advantage of the updates and more. Most importantly, I want to use the secret_token that telegram supports along with webhooks

With this, I tried looking high and low for a tutorial to follow. However, some of the tutorials seems to reference an older version of the PTB package and hard to follow. Not sure if any kind souls can point me to something easier to follow for a novice? Thank you!

Glad you liked the tutorial! Unfortunately I don't know of any good tutorials for python-telegram-bot with webhooks, but perhaps someone else here in the forums will?

Thanks giles! I think I will stick to telepot for now since it fulfils most of the features I need and is simple to use.

I keep hitting error when I run my code on pythonanywhere when my local development environment had no issue.

  1. My bot will react to a command e.g. /command
  2. Based on the command, it will check an API to pull an URL.
  3. The bot will save to a local static path.
  4. Return the static URL with a random token to ensure telegram pulls the right version of the file.

I tried troubleshooting by changing it to a static URL without any token behind (e.g. xxxxx.jpg) but it still does not work. Not sure if I am missing anything.

The errors returned by Telegram:

telepot.exception.TelegramError: ('Bad Request: failed to get HTTP URL content', 400, {'ok': False, 'error_code': 400, 'description': 'Bad Request: failed to get HTTP URL content'}) telepot.exception.TelegramError: ('Bad Request: wrong file identifier/HTTP URL specified', 400, {'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong file identifier/HTTP URL specified'})

Server.log

SIGPIPE: writing to a closed pipe/socket/fd (probably the client disconnected) on request /[path reddacted]/[imagefilename].jpg?a=[token] (ip [redacted]) !!! uwsgi_response_write_headers_do(): Broken pipe [core/writer.c line 248] during GET /[path reddacted]/[imagefilename].jpg?a=[token] (10.0.0.20)

Edit: I found this line: [Telegram IP] - - [09/Jul/2024:08:55:43 +0000] "GET /[path to image]/[image name].jpg HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36" "[Telegram IP] " response-time=6.824

Seems like telegram servers closed the connection or something. Not sure how to work around this.I don't mind passing the URL from the API directly to Telegram but there will be a lot more wait to call the API.

That's a request that telegram made to your web app, but your web app was not able to handle it. If you are on a free account, you only have one worker. So if that request is triggered by a request to your web app, the initial request will still be holding the worker and that worker will not be available to handle the request from telegram.

Thank you glenn for the explanation! I tried for hours and could not figure out why (the previous version served the URL off the API to telegram). To workaround this, I managed to switch and send a file instead of the url and it seems to work well so far. Fingers crossed!

Glad to hear that you have a solution!

pip3 install python-telegram-bot requests --user

Is there something we can help with related to that command?