【问题标题】:Telegram bot in Python with webhook. Deploying on server with Digitalocean droplet带有 webhook 的 Python 电报机器人。使用 Digitalocean droplet 在服务器上部署
【发布时间】:2022-12-07 21:58:05
【问题描述】:

我想了解最近创建的 webhook 是如何工作的。在 python3 中有一些代码,我能够看到 webhook 已设置,但我的电报机器人不响应命令 /start,'任何文本'。只有当我刷新为其创建 webhook 的 URL 时,才能看到 GET 方法有效。

机器人配置 .py 文件:

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
        )

这是我放在服务器块配置文件中的内容:启用站点内的 Nginx 服务器配置文件:

#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
}

合影: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

值得一提的是,为了获取证书,我使用了 Certbot 的命令——sudo certbot --nginx -d bot.vpsprovider.com,如您所见,我没有指定 IP 地址,而是指定了域名。然而,当我在服务器上运行这个脚本时,它说:

https://20.115.90.11:88 上运行

我希望它在https://bot.vpsprovider.com 上运行。我的 nginx 服务器文件配置或我请求证书的方式有问题还是我应该去别处看看?

【问题讨论】:

    标签: python flask server webhooks digital-ocean


    【解决方案1】:

    #机器人配置。 服务器 {

    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
    

    }

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2019-03-14
      • 2019-01-08
      • 2021-02-08
      • 2017-02-21
      相关资源
      最近更新 更多