我想了解最近创建的 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
}
图片:服务器控制台上的调试过程。不知道为什么它说在 https://我的实际 IP 上运行,而不是在我注册证书时指定的 DNS 上运行
值得一提的是,为了获取证书,我使用了 Certbot 的命令 -- sudo certbot --nginx -d bot.vpsprovider.com,正如你所看到的,我没有指定 IP 地址,而是指定了域名。然而,当我在服务器上运行这个脚本时,它说:
我希望它在 https://bot.vpsprovider.com 上运行。我的 nginx 服务器文件配置或我请求证书的方式有问题还是我应该去其他地方寻找?
#机器人配置。 服务器{
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
}
也许是一个老问题,但我也有类似的问题。 我不确定什么对我有帮助可能有很多原因:
https://api.telegram.org/bot[telegramTokenHere]/getWebhookInfo
显示与您的代码相同的 Webhook 地址