[Python] Telegram bot in Python with webhook. Deploying on server with Digitalocean droplet

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I want to understand how recently created webhook works. Having some code in python3 I was able to see that the webhook was set but my telegram bot doesn't respond to commands /start,'any text'. Only when I refresh URL for which I created webhook it is possible to see that the GET method works.

    Bot configuration .py file:

    import logging
    import time
    import flask
    import telebot


    API_TOKEN = 'My Token from bot father'

    WEBHOOK_HOST = 'My domain name bot.vpsprovider.com'
    WEBHOOK_PORT = 88 # 443, 80, 88 or 8443 (port need to be 'open')
    WEBHOOK_LISTEN = '0.0.0.0' # In some VPS you may need to put here the IP addr

    WEBHOOK_SSL_CERT = '/path/to/fullchain.pem' # Path to the ssl certificate
    WEBHOOK_SSL_PRIV = '/path/to/privkey.pem' # Path to the ssl private key


    WEBHOOK_URL_BASE = 'https://{var1}:{var2}'.format(var1=WEBHOOK_HOST, var2=WEBHOOK_PORT)
    WEBHOOK_URL_PATH = '/{var3}/'.format(var3 = API_TOKEN)

    logging.basicConfig(level=logging.DEBUG)


    bot = telebot.TeleBot(API_TOKEN)

    app = flask.Flask(__name__)



    @app.route('/', methods=['GET', 'HEAD'])
    def index():
    return ''



    @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=['help', 'start'])
    def send_welcome(message):
    bot.reply_to(message,
    ("Hi there, I am EchoBot.\n"
    "I am here to echo your kind words back to you."))



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



    bot.remove_webhook()

    time.sleep(0.1)

    # Set webhook
    logging.info('set webhook')
    bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
    certificate=open(WEBHOOK_SSL_CERT, 'r'))
    logging.info('webhook set')

    # Start flask server
    app.run(host=WEBHOOK_LISTEN,
    port=WEBHOOK_PORT,
    ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV),
    debug=True
    )


    This is what I have put inside server block configuration file: Nginx server configuration file within sites-enabled:

    #Bot config.
    server {

    root /path/to/serverblock/bot;
    index index.html index.htm;
    server_name bot.vpsprovider.com; # managed by Certbot

    location / {
    try_files $uri $uri/ =404;
    }
    error_page 405 =200 $uri;

    listen [::]:88 ssl; # managed by Certbot
    listen 88 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/path/to/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/path/to/privkey.pem; # managed by Certbot
    include /etc/path/to/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/path/to/ssl-dhparams.pem; # managed by Certbot
    }



    Picture with: Debug process on server's console. Not sure why does it say running on https://my actual IP and not the DNS which I specified when registering certificate

    It is worth mentioning that in order to obtain a certificate I used Certbot's command -- sudo certbot --nginx -d bot.vpsprovider.com, as you can see I did not specify IP address but the domain name. However, when I run this script on server it says that:


    Running on https://20.115.90.11:88
    ​

    I expect it to be running on https://bot.vpsprovider.com instead. Is there something wrong with my nginx server file configuration or the way I requested certificate or should I look elsewhere?

    Continue reading...

Compartilhe esta Página