我正在尝试使用Telegram Bot
api和Webhook
托管Nginx
部署import sys
from flask import Flask, request
from telepot.delegate import per_chat_id, create_open, pave_event_space
import telepot
import requests
from settings import TOKEN, WAIT_TIME, PORT, URL
try:
from Queue import Queue
except ImportError:
from queue import Queue
class MessageCounter(telepot.helper.ChatHandler):
def __init__(self, *args, **kwargs):
super(MessageCounter, self).__init__(*args, **kwargs)
def on_chat_message(self, msg):
print(msg)
# other stuff
app = Flask(__name__)
update_queue = Queue() # channel between `app` and `bot`
bot = telepot.DelegatorBot(TOKEN, [
pave_event_space()(
per_chat_id(), create_open, MessageCounter, timeout=10),
])
bot.message_loop(source=update_queue) # take updates from queue
@app.route('/abc', methods=['GET', 'POST'])
def pass_update():
update_queue.put(request.data) # pass update to bot
return 'OK'
cf = open('cert', 'r')
if __name__ == '__main__':
bot.setWebhook(URL, cf)
app.run(port=PORT, debug=True)
。
这是我的代码(my_bot.py):
URL
Nginx
是我的服务器的网址my_bot.py
正在运行。
在此settings.py
文件旁边,我有一个TOKEN
文件,其中包含URL
和PORT
以及cert
和ssl
文件,其中包含{ {1}}证书。我还配置了Nginx
设置,我几乎可以确定设置是否正确。
现在,当我尝试使用python my_bot.py
运行代码时,我收到此错误:
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 594, in urlopen
chunked=chunked)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 391, in _make_request
six.raise_from(e, None)
File "<string>", line 2, in raise_from
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 387, in _make_request
httplib_response = conn.getresponse()
File "C:\Python36\lib\http\client.py", line 1331, in getresponse
response.begin()
File "C:\Python36\lib\http\client.py", line 297, in begin
version, status, reason = self._read_status()
File "C:\Python36\lib\http\client.py", line 279, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: <html>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "my_bot.py", line 101, in <module>
bot.setWebhook(URL, cf)
File "C:\Python36\lib\site-packages\telepot\__init__.py", line 638, in setWebhook
return self._api_request('setWebhook', _rectify(p), files)
File "C:\Python36\lib\site-packages\telepot\__init__.py", line 398, in _api_request
return api.request((self._token, method, params, files), **kwargs)
File "C:\Python36\lib\site-packages\telepot\api.py", line 130, in request
r = fn(*args, **kwargs) # `fn` must be thread-safe
File "C:\Python36\lib\site-packages\urllib3\request.py", line 148, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Python36\lib\site-packages\urllib3\poolmanager.py", line 244, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 671, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 671, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 671, in urlopen
release_conn=release_conn, **response_kw)
File "C:\Python36\lib\site-packages\urllib3\connectionpool.py", line 643, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Python36\lib\site-packages\urllib3\util\retry.py", line 303, 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: /bot343287574:AAGJPgohCCviXA2RawmMqgucUKRJDZK3Z7o
/setWebhook (Caused by ProtocolError('Connection aborted.', BadStatusLine('<html>\r\n',)))
很抱歉错误信息格式错误,此代码有什么问题?