TelegramBot. "Webhook can be set up only on ports 80, 88, 443 or 8443" error on heroku webserver
Asked Answered
A

1

3

I am trying to deploy telegram simple echo bot on heroku with webhook and cherrypy server. My example:

#!/usr/bin/python3.4
# -*- coding: utf-8 -*-
import telebot
import cherrypy
import os

TOKEN = 'mytoken'
WEBHOOK_HOST = 'quiet-springs-24190'
WEBHOOK_PORT = int(os.environ.get('PORT', 443))  # 443, 80, 88 или 8443 (port must be open!)
WEBHOOK_LISTEN = '0.0.0.0'  # On some servers, you need write ip similar to webhook_host

WEBHOOK_SSL_CERT = './webhook_cert.pem'  # path to certificate
WEBHOOK_SSL_PRIV = './webhook_pkey.pem'  # path to private key

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (TOKEN)

bot = telebot.TeleBot(TOKEN)


# our webhook-server
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_updates([update])
            return ''
        else:
            raise cherrypy.HTTPError(403)


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

print(bot.get_webhook_info())

# remove webhook before adding
bot.remove_webhook()

bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH)

# CherryPy setting
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
})

# running
cherrypy.quickstart(WebhookServer(), WEBHOOK_URL_PATH, {'/': {}})

Certificate is commented, because, as I understood, heroku has own certificate,,, After running, I have error in logs:

2017-08-06T14:55:46.022286+00:00 app[web.1]: telebot.apihelper.ApiException: A request to the Telegram API was unsuccessful. The server returned HTTP 400 Bad Request. Response body:
2017-08-06T14:55:46.022290+00:00 app[web.1]: [b'{"ok":false,"error_code":400,"description":"Bad Request: bad webhook: Webhook can be set up only on ports 80, 88, 443 or 8443"}']

But I run my server on 443 port.
Is that possible to deploy Telegram Bot on Heroku using webhook? How I can do that?

Actinometer answered 6/8, 2017 at 15:5 Comment(0)
N
1

I may have been too late for that. but I went through these same problems and after testing other ports it worked. For me it was the 8443

Newsmagazine answered 8/6, 2022 at 12:37 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewNope

© 2022 - 2024 — McMap. All rights reserved.