简体   繁体   English

为机器人使用 Telegraf webhook 时出现 404 错误

[英]404 error using Telegraf webhook for a bot

I'm trying to make a Telegram bot working, and I'm using Telegraf.js.我正在尝试使 Telegram 机器人正常工作,并且我正在使用 Telegraf.js。 I've set up a webhook by adding a subdomain to my domain name with a default Apache VirtualHost.我通过使用默认 Apache VirtualHost 向我的域名添加子域来设置 Webhook。
It's basically working but for a reason I've got a 404 error from the Telegram's webhook API info (getWebhookInfo):它基本上可以正常工作,但出于某种原因,我从 Telegram 的 webhook API 信息 (getWebhookInfo) 收到了 404 错误:

{
  "ok": true,
  "result": {
    "url": "https://subdomain.domain.tld/telegraf/<chain-of-64-letters-and-numbers>",
    "has_custom_certificate": false,
    "pending_update_count": 12,
    "last_error_date": 1652692046,
    "last_error_message": "Wrong response from the webhook: 404 Not Found",
    "max_connections": 40,
    "ip_address": "XX.XX.XX.XX"
  }
}

I've noticed a suffix has been appended for a reason (I'm supplying only the subdomain URL without any path), it's probably from Telegraf.js but it seems to be causing the 404 error.我注意到出于某种原因附加了一个后缀(我只提供没有任何路径的子域 URL),它可能来自 Telegraf.js,但它似乎导致了 404 错误。

My code:我的代码:

const { Telegraf } = require("telegraf");
require("dotenv").config();

// Bot initialization
const token = process.env.API_KEY;
if (token === undefined) {
    throw new Error("Token not defined!");
}

const bot = new Telegraf(token/*, {
    telegram: {
        apiRoot: "http://127.0.0.1:" // I'm planning to use tdlib/telegram-bot-api later on
    }
}*/);

// Commands handling
bot.start(ctx => {
    console.info("ok!");
    ctx.reply("Hello from bot!");
});

bot.on("text", () => console.info("text received"));

bot.command("test", ctx => ctx.reply("Working!!"));

bot.command("quit", ctx => ctx.leaveChat());

// Bot launching
bot.launch({
    webhook: {
        domain: "https://subdomain.domain.tld",
        port: 3000 // I've seen 3000 is frequently used so let's use that
    }
}).then(
    resolve => console.info(`Bot '${bot.botInfo.username}' (${bot.botInfo.id}) started successfully (${resolve || "OK"})`),
    reject => console.info(`Bot '${bot.botInfo.username}' (${bot.botInfo.id}) failed to start: ${reject}`)
);

// Graceful stop handling
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

My VirtualHost:我的虚拟主机:

<VirtualHost *:443>
    ServerName subdomain.domain.tld

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Let's Encrypt
    SSLCertificateFile /etc/letsencrypt/live/domain.tld-0001/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain.tld-0001/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

What am I doing wrong?我究竟做错了什么?

I forgot to add the ProxyPass on my Apache VirtualHost :我忘了在我的 Apache VirtualHost上添加ProxyPass

<VirtualHost *:443>
    ServerName subdomain.domain.tld

    # Missing part
    ProxyPreserveHost On
    ProxyRequests Off
    AllowEncodedSlashes NoDecode
    SSLProxyEngine On
    SSLProxyCheckPeerCN Off
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
    # End of missing part

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Let's Encrypt
    SSLCertificateFile /etc/letsencrypt/live/domain.tld-0001/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/domain.tld-0001/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

Now it's working like a charm.现在它就像一个魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM