Forums

500 INTERNAL SERVER ERROR with telegram webhooks

I am using web server flask (python 3.6) in order to catch POST request from telegram server (webhooks), however, when telegram sends me POST request, my webapplication responses: 500 INTERNAL SERVER ERROR This error does not occur every time, but sometimes

 from flask import Flask, request
import telebot
from telebot import types
import time
import config

secret = "b217604b-f60b-40b8-9b9d-565da0330650"

bot = telebot.TeleBot(config.token_bot, threaded=False)

bot.remove_webhook()
time.sleep(1)
bot.set_webhook(url="https://MYNAME.pythonanywhere.com/{}".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

@bot.message_handler(commands=['help'])
def startCommand(message):
    bot.send_message(message.chat.id, 'Hey *' + message.chat.first_name + '*!' , parse_mode='Markdown', reply_markup=types.ReplyKeyboardRemove())
    print(message.chat.id)

This is my error log:

 2018-01-13 16:04:50,367: Traceback (most recent call last):
2018-01-13 16:04:50,367:   File "/home/goga201/.local/lib/python3.6/site-packages/requests/adapters.py", line 370, in send
2018-01-13 16:04:50,367:     timeout=timeout
2018-01-13 16:04:50,367:   File "/home/goga201/.local/lib/python3.6/site-packages/requests/packages/urllib3/connectionpool.py", line 574, in urlopen
2018-01-13 16:04:50,367:     raise SSLError(e)
2018-01-13 16:04:50,367: requests.packages.urllib3.exceptions.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2178)
2018-01-13 16:04:50,368: 
2018-01-13 16:04:50,368: During handling of the above exception, another exception occurred:
2018-01-13 16:04:50,368: 
2018-01-13 16:04:50,368: Traceback (most recent call last):
2018-01-13 16:04:50,368:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
2018-01-13 16:04:50,369:     response = self.full_dispatch_request()
2018-01-13 16:04:50,369:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
2018-01-13 16:04:50,369:     rv = self.handle_user_exception(e)
2018-01-13 16:04:50,369:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
2018-01-13 16:04:50,369:     reraise(exc_type, exc_value, tb)
2018-01-13 16:04:50,370:   File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
2018-01-13 16:04:50,370:     raise value
2018-01-13 16:04:50,370:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
2018-01-13 16:04:50,370:     rv = self.dispatch_request()
2018-01-13 16:04:50,370:   File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
2018-01-13 16:04:50,371:     return self.view_functions[rule.endpoint](**req.view_args)
2018-01-13 16:04:50,371:   File "/home/goga201/mysite/flask_app.py", line 20, in webhook
2018-01-13 16:04:50,371:     bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
2018-01-13 16:04:50,371:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/__init__.py", line 197, in process_new_updates
2018-01-13 16:04:50,371:     self.process_new_messages(new_messages)
2018-01-13 16:04:50,371:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/__init__.py", line 218, in process_new_messages
2018-01-13 16:04:50,372:     self._notify_command_handlers(self.message_handlers, new_messages)
2018-01-13 16:04:50,372:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/__init__.py", line 1283, in _notify_command_handlers
2018-01-13 16:04:50,372:     self._exec_task(message_handler['function'], message)
2018-01-13 16:04:50,372:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/__init__.py", line 339, in _exec_task
2018-01-13 16:04:50,372:     task(*args, **kwargs)
2018-01-13 16:04:50,372:   File "/home/goga201/mysite/flask_app.py", line 47, in state1
2018-01-13 16:04:50,373:     processStatement(config.States.S_1.value, message.chat.id, message.text)
2018-01-13 16:04:50,373:   File "/home/goga201/mysite/flask_app.py", line 75, in processStatement
2018-01-13 16:04:50,373:     bot.send_message(user, "Не могу найти по ответе значение в бд" + statement + " " + message)
2018-01-13 16:04:50,373:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/__init__.py", line 465, in send_message
2018-01-13 16:04:50,373:     reply_markup, parse_mode, disable_notification))
2018-01-13 16:04:50,373:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/apihelper.py", line 135, in send_message
2018-01-13 16:04:50,374:     return _make_request(token, method_url, params=payload, method='post')
2018-01-13 16:04:50,374:   File "/home/goga201/.local/lib/python3.6/site-packages/telebot/apihelper.py", line 54, in _make_request
2018-01-13 16:04:50,374:     timeout=(connect_timeout, read_timeout), proxies=proxy)
2018-01-13 16:04:50,374:   File "/home/goga201/.local/lib/python3.6/site-packages/requests/sessions.py", line 465, in request
2018-01-13 16:04:50,374:     resp = self.send(prep, **send_kwargs)
2018-01-13 16:04:50,375:   File "/home/goga201/.local/lib/python3.6/site-packages/requests/sessions.py", line 573, in send
2018-01-13 16:04:50,375:     r = adapter.send(request, **kwargs)
2018-01-13 16:04:50,375:   File "/home/goga201/.local/lib/python3.6/site-packages/requests/adapters.py", line 431, in send
2018-01-13 16:04:50,375:     raise SSLError(e, request=request)
2018-01-13 16:04:50,375: requests.exceptions.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2178)
2018-01-13 16:04:50,471: Resetting dropped connection: api.telegram.org

That looks similar to a known issue with Telepot -- check out this GitHub post by the owner -- apparently the first suggested workaround in the list has worked for other people.