Forums

requests.exceptions.SSLError: HTTPSConnectionPool(host='api.telegram.org', port=443) on paid account

I have hacker account. Bot works fine until I want to send message according to the callback

@bot.message_handler(func=lambda call: True) 
    def choose(call):
         if call.data == "test":
            bot.send_message(chat_id=call.message.chat.id, text='test2')

What is the full traceback? How the request looks like? Are there more details in the response?

.

secret = "code"
bot = telebot.TeleBot('token', threaded=False)
bot.remove_webhook()
time.sleep(1)
bot.set_webhook("domain/{}".format(secret))
app = Flask(__name__)

@app.route('/{}'.format(secret), methods=["POST"])
def webhook():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return "ok", 200

def type_markup():
    markup = InlineKeyboardMarkup()
    markup.row_width = 2
    markup.add(InlineKeyboardButton("type1", callback_data="tp1"),
               InlineKeyboardButton("type2", callback_data="tp2"))
    return markup

@bot.message_handler(commands=['start'])
def startCommand(message):
    bot.send_message(message.chat.id, 'Choose', parse_mode='Markdown', reply_markup=type_markup())
    db.insert_user_db(user_id=message.chat.id, user_name=message.chat.username)

def option_markup():
    markup = InlineKeyboardMarkup()
    markup.row_width = 2
    markup.add(InlineKeyboardButton("option1", callback_data="opt1"),
               InlineKeyboardButton("option2", callback_data="opt2"))
    return markup

@bot.callback_query_handler(func=lambda call: True)
def choose_type(call):
    if call.data == "type1":
        bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=option_markup())

@bot.message_handler(func=lambda call: True) # for callback_query_handler also doesn't work
def choose(call):
    if call.data == "opt1":
        bot.send_message(chat_id=call.message.chat.id, text='option1 chosen') #here SSL error appears

Could you show the traceback message?

.

2022-09-08 09:12:44,848: Exception on /******-****-****-****-******** [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.9/http/client.py", line 1345, in getresponse
    response.begin()
  File "/usr/local/lib/python3.9/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.9/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/local/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2633)
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/home/***/.local/lib/python3.9/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot111:BB/sendMessage?chat_id=21111111&text=%D0%92%D1%8B%D0%B1%D0%B5%D1%80%D0%B8%D1%82%D0%B5+%D1%82%D0%B8%D0%BF&reply_markup=%7B%22inline_keyboard%22%3A+%5B%5B%7B%22text%22%3A+%22%5Cu0412%5Cu0430%5Cu043b%5Cu044e%5Cu0442%5Cu0430%22%2C+%22callback_data%22%3A+%22currency%22%7D%2C+%7B%22text%22%3A+%22%5Cu0411%5Cu0435%5Cu043d%5Cu0437%5Cu0438%5Cu043d%22%2C+%22callback_data%22%3A+%22oil%22%7D%5D%5D%7D&parse_mode=Markdown (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2633)')))
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/***/mysite/flask_app.py", line 53, in webhook
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 449, in process_new_updates
    self.process_new_messages(new_messages)
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 481, in process_new_messages
    self._notify_command_handlers(self.message_handlers, new_messages, 'message')
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 4064, in _notify_command_handlers
    self._exec_task(message_handler['function'], message, pass_bot=message_handler['pass_bot'], task_type='handler')
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 780, in _exec_task
    raise e
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 773, in _exec_task
    task(*args, **kwargs)
  File "/home/***/mysite/flask_app.py", line 67, in startCommand
    bot.send_message(message.chat.id, 'Выберите тип', parse_mode='Markdown', reply_markup=type_markup())
  File "/home/***/.local/lib/python3.9/site-packages/telebot/__init__.py", line 1006, in send_message
    apihelper.send_message(
  File "/home/***/.local/lib/python3.9/site-packages/telebot/apihelper.py", line 267, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/home/***/.local/lib/python3.9/site-packages/telebot/apihelper.py", line 146, in _make_request
    result = _get_req_session().request(
  File "/home/***/.local/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/***/.local/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/home/***/.local/lib/python3.9/site-packages/requests/adapters.py", line 563, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot111:BB/sendMessage?chat_id=21111111&text=%D0%92%D1%8B%D0%B1%D0%B5%D1%80%D0%B8%D1%82%D0%B5+%D1%82%D0%B8%D0%BF&reply_markup=%7B%22inline_keyboard%22%3A+%5B%5B%7B%22text%22%3A+%22%5Cu0412%5Cu0430%5Cu043b%5Cu044e%5Cu0442%5Cu0430%22%2C+%22callback_data%22%3A+%22currency%22%7D%2C+%7B%22text%22%3A+%22%5Cu0411%5Cu0435%5Cu043d%5Cu0437%5Cu0438%5Cu043d%22%2C+%22callback_data%22%3A+%22oil%22%7D%5D%5D%7D&parse_mode=Markdown (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:2633)')))

That may be related to the bug mentioned here: https://stackoverflow.com/questions/64960430/python-requests-with-proxy-results-in-sslerror-wrong-version-number/66656188#66656188

I downgraded my urllib3 to 1.23 and then to 1.25.11 in both cases didn't work.

Does the traceback still mention the path /usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py?

yes still same error

*Traceback (most recent call last):
  File "/home/usr/.local/lib/python3.9/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/home/usr/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "/home/usr/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 446, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url*

That's not the same error. The error about SSL versions appears to be fixed. Now it looks like there is a different issue, but you have not provided enough of the traceback to identify it.

full traceback

2022-09-09 20:22:08,878: OSError: write error
2022-09-09 20:22:08,903: Exception on /******-****-****-****-******** [POST]
Traceback (most recent call last):
  File "/home/adminx/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "/home/adminx/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 426, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/home/adminx/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 421, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/lib/python3.9/http/client.py", line 1345, in getresponse
    response.begin()
  File "/usr/local/lib/python3.9/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.9/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/local/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/usr/local/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/local/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ssl.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2633)
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/home/adminx/.local/lib/python3.9/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen(
  File "/home/adminx/.local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "/home/adminx/.local/lib/python3.9/site-packages/urllib3/util/retry.py", line 446, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot11111111111:BBDSHSDSJSJSDJSDJSDJDSJDS/sendMessage?chat_id=257458458&text=%D0%92%D1%8B%D0%B1%D0%B5%D1%80%D0%B8%D1%82%D0%B5+%D1%82%D0%B8%D0%BF&reply_markup=%7B%22inline_keyboard%22%3A+%5B%5B%7B%22text%22%3A+%22%5Cu0412%5Cu0430%5Cu043b%5Cu044e%5Cu0442%5Cu0430%22%2C+%22callback_data%22%3A+%22currency%22%7D%2C+%7B%22text%22%3A+%22%5Cu0411%5Cu0435%5Cu043d%5Cu0437%5Cu0438%5Cu043d%22%2C+%22callback_data%22%3A+%22oil%22%7D%5D%5D%7D&parse_mode=Markdown (Caused by SSLError(SSLError(1, '[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2633)')))
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/adminx/mysite/flask_app.py", line 53, in webhook
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 449, in process_new_updates
    self.process_new_messages(new_messages)
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 481, in process_new_messages
    self._notify_command_handlers(self.message_handlers, new_messages, 'message')
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 4064, in _notify_command_handlers
    self._exec_task(message_handler['function'], message, pass_bot=message_handler['pass_bot'], task_type='handler')
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 780, in _exec_task
    raise e
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 773, in _exec_task
    task(*args, **kwargs)
  File "/home/adminx/mysite/flask_app.py", line 67, in startCommand
    bot.send_message(message.chat.id, 'Выберите тип', parse_mode='Markdown', reply_markup=type_markup())
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/__init__.py", line 1006, in send_message
    apihelper.send_message(
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/apihelper.py", line 267, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/home/adminx/.local/lib/python3.9/site-packages/telebot/apihelper.py", line 146, in _make_request
    result = _get_req_session().request(
  File "/home/adminx/.local/lib/python3.9/site-packages/requests/sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/adminx/.local/lib/python3.9/site-packages/requests/sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
  File "/home/adminx/.local/lib/python3.9/site-packages/requests/adapters.py", line 563, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot11111111111:BBDSHSDSJSJSDJSDJSDJDSJDS/sendMessage?chat_id=257458458&text=%D0%92%D1%8B%D0%B1%D0%B5%D1%80%D0%B8%D1%82%D0%B5+%D1%82%D0%B8%D0%BF&reply_markup=%7B%22inline_keyboard%22%3A+%5B%5B%7B%22text%22%3A+%22%5Cu0412%5Cu0430%5Cu043b%5Cu044e%5Cu0442%5Cu0430%22%2C+%22callback_data%22%3A+%22currency%22%7D%2C+%7B%22text%22%3A+%22%5Cu0411%5Cu0435%5Cu043d%5Cu0437%5Cu0438%5Cu043d%22%2C+%22callback_data%22%3A+%22oil%22%7D%5D%5D%7D&parse_mode=Markdown (Caused by SSLError(SSLError(1, '[SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2633)')))

It may be a version issue between requests and OpenSSL. Try installing different versions of requests and see if you can find one that works.

problem with your SSL certificate. Can you handle this problem I need to solve it I found this https://stackoverflow.com/questions/72523361/trying-to-send-a-post-request-to-a-flask-app-ssl-error-occurs

That is not the problem that you are experiencing. Did you try installing different versions of requests?

I tried several versions, even from 2016-2017. Didn't help. I'm actually don't use requests library itself. The only request I use is in my POST webhook method from flask

from flask import Flask, request

@app.route('/{}'.format(secret), methods=["POST"])
def webhook():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return "ok", 200

And if change this method to this

@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
   update = request.get_json()
   if "message" in update:
    text = update["message"]["text"]
    chat_id = update["message"]["chat"]["id"]
    bot.send_message(chat_id, "From the web: you said '{}'".format(text))
    return "OK", 200

it posts messages. The problem is in the flasks request in process_new_updates

You may also try to update system image. See https://help.pythonanywhere.com/pages/ChangingSystemImage

I have latest updates of everything. I can reply or send message like this.

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    bot.reply_to(message, message.text)
    bot.send_message(message.chat.id, 'Hello!')

But I can't do it via callback_query_handler. SSL error in POST request appears

You have "glastonbury" and the latest is "haggis"

I updated to haggis and did other updates. Same error appears

Are you able to run it correctly form the other machine?

locally all works fine. guys you have this problem not solved from 2019 https://www.pythonanywhere.com/forums/topic/14315/

According to the discussion in this ticket on python-telegram-bot repo it may mean "that a user blocked the bot and the bot can't send messages to that user."

he has different type of error. My bot can't be blocked because I'm sending message to myself. And its fully open, other messages are coming.