0

Запрос https://api.telegram.org/bot<TOKEN>/getWebhookInfo дает результат вида:

{"ok":true,"result":{"url":"https://fastcolor.site:443","has_custom_certificate":false,"pending_update_count":16,"last_error_date":1568135087,"last_error_message":"Wrong response from the webhook: 500 Internal Server Error","max_connections":40}}

Хотя сайт подписан SSL сертификтом, выданным регистратором, поэтому самоподписанных сертификатов не создавал. Сам код бота такой:

from flask import Flask, request, jsonify
import telebot, time
import os

application = Flask(__name__)
bot = telebot.TeleBot('<TOKEN>')
bot.remove_webhook()
time.sleep(2)
bot.set_webhook(url="https://site.site:443")

@application.route("/", methods=['POST'])
def getMessage():
  r = request.get_json()
  if "message" in r.keys():
    chat_id = r["message"]["chat"]["id"]
    if "text" in r["message"]:
      text_mess = r["message"]["text"]
    else:
      bot.send_message(chat_id=chat_id, text="Какая то не понятная проблема", parse_mode='HTML')
      return "ok", 200
  if text_mess == '/start':
    bot.send_message(chat_id=chat_id, text="Привет WebHook")
    return "ok", 200

if __name__ == "main":
  application.run(host="0.0.0.0", port=int(os.environ.get('PORT', '443')), ssl_context=('cert.crt', 'cert.key'))

В чем может быть проблема? Указан порт для доступа по SSL сервера 443

1 ответ 1

0

Изменил структуру тела кода таким образом и всё заработало:

@bot.message_handler(commands=['start'])
def start(message):
    bot.reply_to(message, 'Hello, ' + message.from_user.first_name)

@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
    bot.reply_to(message, message.text)


@application.route("/<TOKEN>", methods=['POST'])
def getMessage():
    bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
    return "!", 200

@application.route("/")
def webhook():
    bot.remove_webhook()
    bot.set_webhook(url="https://www.site.site/<TOKEN>")
    return "!", 200

@application.route("/registration")
def registration():
    return render_template('registration.html')

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.