【问题标题】:Problems with starting Telegram bot启动电报机器人的问题
【发布时间】:2019-04-03 13:48:35
【问题描述】:

我正在尝试在 PythonAnywhere 上部署我的电报机器人。免费帐户运行良好,但付费帐户存在一些问题。我在错误日志中收到“OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'decryption failed or bad record mac')]”消息(因此机器人工作错误) .我该如何解决?
我正在使用 python3.7 + pyTelegramBotAPI + flask

错误重现的代码示例:

# coding=utf-8
import telebot
import flask
import time


token = 'bot_token'
bot = telebot.TeleBot(token, threaded=False)

WEBHOOK_HOST = '*userName*.pythonanywhere.com'
WEBHOOK_URL_BASE = "https://%s" % (WEBHOOK_HOST)
WEBHOOK_URL_PATH = "/%s/" % (token)

app = flask.Flask(__name__)

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

@bot.message_handler(commands=['start', 'help'])
def handle_start_help(message):
        bot.send_message(message.chat.id, text="Hello, my friend")

bot.remove_webhook()
time.sleep(0.1)
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)

完整的错误跟踪:

2018-10-30 12:40:33,351: Exception on /*token*/ [POST]
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/kleratoni/bot/bot/Main.py", line 90, in webhook
    bot.process_new_updates([update])
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 326, in process_new_updates
    self.process_new_callback_query(new_callback_querys)
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 354, in process_new_callback_query
    self._notify_command_handlers(self.callback_query_handlers, new_callback_querys)
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 1490, in _notify_command_handlers
    self._exec_task(message_handler['function'], message)
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 464, in _exec_task
    task(*args, **kwargs)
  File "/home/*accountName*/bot/bot/Main.py", line 535, in callback_inline
    MainMenuActions.BetMenu_BackButton(call.message, bot, db)
  File "/home/*accountName*/bot/bot/MainMenuActions.py", line 130, in BetMenu_BackButton
    bot.send_message(message.chat.id, text=config.StringContent["StartPlayWords"], reply_markup=keyborads.keyboardMainmenu)
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/__init__.py", line 598, in send_message
    reply_markup, parse_mode, disable_notification))
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/apihelper.py", line 140, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/home/*accountName*/.local/lib/python3.7/site-packages/telebot/apihelper.py", line 54, in _make_request
    timeout=(connect_timeout, read_timeout), proxies=proxy)
  File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3.7/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3.7/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/usr/lib/python3.7/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.7/http/client.py", line 1321, in getresponse
    response.begin()
  File "/usr/lib/python3.7/http/client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.7/http/client.py", line 257, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.7/socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 300, in recv_into
    return self.recv_into(*args, **kwargs)
  File "/usr/lib/python3.7/site-packages/urllib3/contrib/pyopenssl.py", line 285, in recv_into
    return self.connection.recv_into(*args, **kwargs)
  File "/usr/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1814, in recv_into
    self._raise_ssl_error(self._ssl, result)
  File "/usr/lib/python3.7/site-packages/OpenSSL/SSL.py", line 1639, in _raise_ssl_error
    _raise_current_error()
  File "/usr/lib/python3.7/site-packages/OpenSSL/_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'ssl3_get_record', 'decryption failed or bad record mac')]

【问题讨论】:

标签: flask python-requests openssl pythonanywhere py-telegram-bot-api


【解决方案1】:

从 pyTelegramBotAPI github repo 上的一个问题来看,您使用的请求版本与您使用的 OpenSSL 版本之间似乎不兼容。尝试改用 requests 2.10.0。

【讨论】:

  • 我已经测试了很多。它没有帮助。只是错误文本已更改为:requests.exceptions.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] 解密失败或坏记录 mac (_ssl.c:2281) 我已降级为“初学者”帐户并且它工作正常。但我需要更多的“CPU 时间”和磁盘可用空间。
  • 当您说它在降级时有效但在升级时无效 - 您在升级时做了哪些更改。例如:您现在使用自己的自定义域名吗?
  • @conrad,如果我没记错的话,除了 uWSGI 工作人员的数量外,什么都没有改变。我在 Pythonanywhere 上的项目没有自定义域,我没有任何其他 Python 模块。
  • 我会尝试在 virtualenv 中运行所有内容,并确保您在更改之间重新加载了 webapp 等。这听起来肯定是请求版本/包版本兼容性问题
【解决方案2】:

我遇到了同样的错误,我这样修复了

token = 'bot_token'
bot = telebot.TeleBot(token, threaded=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2017-08-18
    • 1970-01-01
    • 2021-12-12
    • 2018-03-27
    • 2022-11-14
    相关资源
    最近更新 更多