电报Javabot。设置网络钩子
Telegram Javabot. Setting webhook
我用 rubenlagus api 在 Java 上创建了电报机器人。现在我无法设置网络钩子。我知道 webhook 的这些规则:
*支持 IPv4,Webhooks 目前不支持 IPv6。
*在端口 443、80、88 或 8443 上接受来自 149.154.167.197-233 的传入 POST。
*能够处理 TLS1.0+ HTTPS 流量。
*提供受支持的、非通配符、经过验证或自签名的证书。
*使用与您在设置时提供的域匹配的 CN 或 SAN。
*提供所有中间证书以完成验证链。
我有一个带有经过验证的 ssl 证书的域名。Qualys test 显示 A+ rank.Server 支持 IPv4。 443
端口正在监听。服务器在端口 443 上接受来自 149.154.167.197-233 的传入 POST。我使用此 rubenlagus api 方法创建 TelegramApi
private static TelegramBotsApi createNoSelfSignedTelegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(
"src/main/resources/server.jks",//path to KeyStore for the server
"myPassword", //Key store password for the serve
"https://example.com:443", //External url
"https://localhost:443"); //Internal url
}
我通过这些命令获得了 server.jks
- openssl pkcs12 -export -in mydomain.net.crt -inkey mydomain.key > keypair.p12
- keytool -importkeystore -srckeystore keypair.p12 -destkeystore server.jks -srcstoretype pkcs12
这是我的代码:
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi(
"src/main/resources/server.jks",
"mypassword",
"https://example.com:443",
"https://localhost:443");
BotHook webhookBot = new BotHook(options);
botsApi.registerBot(webhookBot);
当我启动程序时,我收到这个
Jul 28, 2018 3:27:59 PM
org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:443]
Jul 28, 2018 3:27:59 PM org.glassfish.grizzly.http.server.HttpServer
start
INFO: [HttpServer] Started.
但是 bot work.I 在服务器的日志中看不到这个:
2018/07/29 15:08:43 [error] 1166#1166: *453 openat() "/var/www/www->root/data/www/example.net/callback/WebhookClass failed (2: No such file or >directory),
client: 149.154.167.227, server: example.net request: "POST >/callback/WebhookClass HTTP/1.1", host: "example.net"
Grizzly 似乎无法处理 http 请求。当我尝试通过此 curl 命令检查它时
curl -X POST -i http://217.0.0.1:443/callback
我收到了
curl: (7) Failed to connect to 217.0.0.1 port 443: Connection timed out
我多次检查了 TelegramBotsApi 构造函数中传递的所有参数。
问题似乎出在您的基础设施上,而不是代码上。 TelegramBotsApi 在端口 443 上启动 http Grizzly 服务器,该服务器处理来自 Telegram 的机器人相关请求。 Telegram 将通过其外部 URL https://example.com:443 访问服务器。
您提供的服务器日志看起来像是 Nginx,对吗?因此,我假设您已将 Nginx 服务器配置为接受 https://example.com:443 的请求。来自 Telegram 的请求由 Nginx 而不是 Grizzly 服务器处理。 Nginx 以 404 响应,因为它没有在 /callback/* 路径上配置任何处理程序。
您可以通过多种方式将来自 Telegram 的请求发送至 example.com 并转发至 Grizzly:
- 运行 你的程序在 nginx 所在的服务器上 运行。您需要使用另一个端口,例如 8443。设置服务器的防火墙规则以允许 8443 上的传入连接。
配置 nginx 将所有匹配 /callback/* 的 http 请求转发到您的 Grizzly 服务器。将以下内容添加到 nginx 配置文件的服务器部分:
location /callback {
proxy_pass https://<YOUR_TELEGRAM_BOT_SERVER>:443;
}
其中 YOUR_TELEGRAM_BOT_SERVER 是主机名或服务器的 IP 地址 运行 您的程序。注册 bot api 时,请确保使用与 nginx 服务器相同的证书。
我用 rubenlagus api 在 Java 上创建了电报机器人。现在我无法设置网络钩子。我知道 webhook 的这些规则:
*支持 IPv4,Webhooks 目前不支持 IPv6。
*在端口 443、80、88 或 8443 上接受来自 149.154.167.197-233 的传入 POST。
*能够处理 TLS1.0+ HTTPS 流量。
*提供受支持的、非通配符、经过验证或自签名的证书。
*使用与您在设置时提供的域匹配的 CN 或 SAN。
*提供所有中间证书以完成验证链。
我有一个带有经过验证的 ssl 证书的域名。Qualys test 显示 A+ rank.Server 支持 IPv4。 443 端口正在监听。服务器在端口 443 上接受来自 149.154.167.197-233 的传入 POST。我使用此 rubenlagus api 方法创建 TelegramApi
private static TelegramBotsApi createNoSelfSignedTelegramBotsApi() throws TelegramApiException {
return new TelegramBotsApi(
"src/main/resources/server.jks",//path to KeyStore for the server
"myPassword", //Key store password for the serve
"https://example.com:443", //External url
"https://localhost:443"); //Internal url
}
我通过这些命令获得了 server.jks
- openssl pkcs12 -export -in mydomain.net.crt -inkey mydomain.key > keypair.p12
- keytool -importkeystore -srckeystore keypair.p12 -destkeystore server.jks -srcstoretype pkcs12
这是我的代码:
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi(
"src/main/resources/server.jks",
"mypassword",
"https://example.com:443",
"https://localhost:443");
BotHook webhookBot = new BotHook(options);
botsApi.registerBot(webhookBot);
当我启动程序时,我收到这个
Jul 28, 2018 3:27:59 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:443]
Jul 28, 2018 3:27:59 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
但是 bot work.I 在服务器的日志中看不到这个:
2018/07/29 15:08:43 [error] 1166#1166: *453 openat() "/var/www/www->root/data/www/example.net/callback/WebhookClass failed (2: No such file or >directory), client: 149.154.167.227, server: example.net request: "POST >/callback/WebhookClass HTTP/1.1", host: "example.net"
Grizzly 似乎无法处理 http 请求。当我尝试通过此 curl 命令检查它时
curl -X POST -i http://217.0.0.1:443/callback
我收到了
curl: (7) Failed to connect to 217.0.0.1 port 443: Connection timed out
我多次检查了 TelegramBotsApi 构造函数中传递的所有参数。
问题似乎出在您的基础设施上,而不是代码上。 TelegramBotsApi 在端口 443 上启动 http Grizzly 服务器,该服务器处理来自 Telegram 的机器人相关请求。 Telegram 将通过其外部 URL https://example.com:443 访问服务器。
您提供的服务器日志看起来像是 Nginx,对吗?因此,我假设您已将 Nginx 服务器配置为接受 https://example.com:443 的请求。来自 Telegram 的请求由 Nginx 而不是 Grizzly 服务器处理。 Nginx 以 404 响应,因为它没有在 /callback/* 路径上配置任何处理程序。
您可以通过多种方式将来自 Telegram 的请求发送至 example.com 并转发至 Grizzly:
- 运行 你的程序在 nginx 所在的服务器上 运行。您需要使用另一个端口,例如 8443。设置服务器的防火墙规则以允许 8443 上的传入连接。
配置 nginx 将所有匹配 /callback/* 的 http 请求转发到您的 Grizzly 服务器。将以下内容添加到 nginx 配置文件的服务器部分:
location /callback { proxy_pass https://<YOUR_TELEGRAM_BOT_SERVER>:443; }
其中 YOUR_TELEGRAM_BOT_SERVER 是主机名或服务器的 IP 地址 运行 您的程序。注册 bot api 时,请确保使用与 nginx 服务器相同的证书。