Answer the question
In order to leave comments, you need to log in
Bot not working what to do?
I'm just a little bit familiar with webhooks. I ate to fill in the bot on PythonAnyWhere, it seems to be filled in, but the bot does not respond, here is the code:
import telebot
import cherrypy
import config
WEBHOOK_HOST = '35.173.69.207'
WEBHOOK_PORT = 8443 # 443, 80, 88 или 8443 (порт должен быть открыт!)
WEBHOOK_LISTEN = '0.0.0.0' # На некоторых серверах придется указывать такой же IP, что и выше
WEBHOOK_SSL_CERT = 'webhook_cert.pem' # Путь к сертификату
WEBHOOK_SSL_PRIV = 'webhook_pkey.pem' # Путь к приватному ключу
WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % config.token
bot = telebot.TeleBot(config.token)
# Наш вебхук-сервер
class WebhookServer(object):
@cherrypy.expose
def index(self):
if 'content-length' in cherrypy.request.headers and \
'content-type' in cherrypy.request.headers and \
cherrypy.request.headers['content-type'] == 'application/json':
length = int(cherrypy.request.headers['content-length'])
json_string = cherrypy.request.body.read(length).decode("utf-8")
update = telebot.types.Update.de_json(json_string)
# Эта функция обеспечивает проверку входящего сообщения
bot.process_new_messages([update.message])
bot.process_new_updates([update])
return ''
else:
raise cherrypy.HTTPError(403)
# Хэндлер на все текстовые сообщения
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
bot.reply_to(message, message.text)
# Снимаем вебхук перед повторной установкой (избавляет от некоторых проблем)
bot.remove_webhook()
# Ставим заново вебхук
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
certificate=open(WEBHOOK_SSL_CERT, 'r'))
# Указываем настройки сервера CherryPy
cherrypy.config.update({
'server.socket_host': WEBHOOK_LISTEN,
'server.socket_port': WEBHOOK_PORT,
'server.ssl_module': 'builtin',
'server.ssl_certificate': WEBHOOK_SSL_CERT,
'server.ssl_private_key': WEBHOOK_SSL_PRIV
})
# Собственно, запуск!
cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}})
Answer the question
In order to leave comments, you need to log in
How to do what? Do what makes up 95% of the time of those who program. It's not web reading, and it's not copy-paste. This is not strange - debugging programs, ie. boring, meticulous, to tears from the eyes of the search for errors.
And we don't even know what "Doesn't work" means. You didn't provide any error messages or traces.
Well, in your case, I would start by sorting it out, and if you removed something useful along with the comments. With thoughtless copy-paste, this happens.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question