Telegram bot deployment issue on PythonAnywhere

Hey guys, I’m having trouble with my Telegram bot on PythonAnywhere. It was fine on the free account, but now I’m using a paid one and it’s acting up. I keep getting this error in the logs:

OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'decryption failed or bad record mac')]

I’m using Python 3.7, pyTelegramBotAPI, and Flask. Here’s a simplified version of my code:

import telebot
import flask

bot = telebot.TeleBot('my_token', threaded=False)
app = flask.Flask(__name__)

@app.route('/webhook', methods=['POST'])
def handle_webhook():
    if flask.request.headers.get('content-type') == 'application/json':
        update = telebot.types.Update.de_json(flask.request.get_data().decode('utf-8'))
        bot.process_new_updates([update])
        return ''
    else:
        flask.abort(403)

@bot.message_handler(commands=['start'])
def say_hi(message):
    bot.send_message(message.chat.id, "Hi there!")

bot.set_webhook('https://myusername.pythonanywhere.com/webhook')

Any ideas on how to fix this? It’s driving me nuts!

I encountered a similar problem when deploying my Telegram bot on PythonAnywhere. The SSL error you’re seeing often stems from outdated libraries or configuration issues. Here’s what worked for me:

First, ensure you’re using the latest version of pyTelegramBotAPI. You can upgrade it with:

pip install --upgrade pyTelegramBotAPI

Also, double-check your webhook URL. Make sure it’s using HTTPS and matches exactly what you’ve set in your Telegram bot settings.

If the issue persists, try explicitly specifying the SSL context in your Flask app:

import ssl
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(‘/path/to/cert.pem’, ‘/path/to/key.pem’)
app.run(ssl_context=context)

This approach helped resolve the SSL errors in my case. Let me know if you need further assistance.

I’ve been down this road before with PythonAnywhere and Telegram bots. The SSL error you’re seeing is a real pain. One thing that sorted it for me was adjusting the SSL version in my code. Try adding this near the top of your script:

import ssl
from functools import partial
ssl.wrap_socket = partial(ssl.wrap_socket, ssl_version=ssl.PROTOCOL_TLSv1_2)

This forces the use of TLS 1.2, which PythonAnywhere seems to prefer. Also, make sure your bot token is correct and that you’ve whitelisted the Telegram API IP addresses in your PythonAnywhere settings. If you’re still stuck, their support team is pretty helpful - might be worth reaching out to them directly.

hey miar, had similar issue. try updating ur ssl lib. might help:

pip install --upgrade pyopenssl

if that dont work, check ur pythonanywhere ssl settings. sometimes they need tweaking for bots. good luck!