【问题标题】:How shoud I fix this Java error, caused by it`s working on Heroku?我应该如何修复这个 Java 错误,由它在 Heroku 上工作引起的?
【发布时间】:2019-09-05 19:28:58
【问题描述】:

我已经在 Heroku 上成功部署了我的应用,但是我的应用在运行时崩溃了。 我收到错误消息:

错误 R10(启动超时)-> Web 进程未能在内部绑定到 $PORT 90 秒启动

我在网上找到了这段代码,它粘贴在主类中 - 没有结果:

public static String PORT = System.getenv("PORT");
public static String SERVER_URL = System.getenv("SERVER_URL");

过程文件:

web: java $JAVA_OPTS -Dserver.port=$PORT -cp 
target/classes:target/dependency/* Bot

Pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>tgBot</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.telegram</groupId>
            <artifactId>telegrambots</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals><goal>copy-dependencies</goal></goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

主类:

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;

import java.io.IOException;

public class Bot extends TelegramLongPollingBot {

    public static String PORT = System.getenv("PORT");
    public static String SERVER_URL = System.getenv("SERVER_URL");

    public static void main(String[] args) {
        ApiContextInitializer.init();
        TelegramBotsApi bot = new TelegramBotsApi();
        try {
            bot.registerBot(new Bot());
        } catch (TelegramApiRequestException e) {
            e.printStackTrace();
        }
    }

    public void onUpdateReceived(Update update) {

        Message message = update.getMessage();
        Methods method = new Methods();
        Answers answer = new Answers();
        Model model = new Model();
        if (message != null && message.hasText()) {
            if (message.getText() == answer.row1Button) {
                method.sendMsg(message, answer.faq);
            }
            String s = message.getText();
            if ("/start".equals(s) || "Справка/помощь по боту".equals(s) || "/help".equals(s)) {
                method.sendMsg(message, answer.faq);
            } else if ("/api".equals(s)) {
                method.sendMsg(message, answer.api);
            } else {
                try {
                    method.sendMsg(message, Weather.getWeather(message.getText(), model));
                } catch (IOException e) {
                    method.sendMsg(message, answer.fail);
                }
            }
        }
    }


    public String getBotUsername() {
        return "Weather";
    }

    public String getBotToken() {
        return "my bot token :D";
    }
}

【问题讨论】:

  • 此应用是否处理 Web 请求(即 HTTP 请求)?如果没有,您需要将Procfile 中的web: 更改为bot: 或类似名称。 web 需要一个 HTTP 端口绑定
  • 是的,它是一个简单的电报机器人。据我所知,它适用于 HTTPS 请求
  • 确保它绑定到 0.0.0.0 和 PORT
  • 是的,我已经检查过了,但没有结果

标签: java heroku telegram telegram-bot


【解决方案1】:

这可以帮助您https://github.com/pengrad/telegram-bot-heroku,但它使用其他库来处理 Telegram Bot API – java-telegram-bot-api

有一个 Procfile(需要在那里更新主类)和用于部署的 build.gradle 文件。
默认设置 Webhook:

public class Main {
    public static void main(String[] args) {

        final String portNumber = System.getenv("PORT");
        if (portNumber != null) {
            port(Integer.parseInt(portNumber));
        }

        // current app url to set webhook
        // should be set via heroku config vars
        // https://devcenter.heroku.com/articles/config-vars
        // heroku config:set APP_URL=https://app-for-my-bot.herokuapp.com
        final String appUrl = System.getenv("APP_URL");

        // define list of bots
        BotHandler[] bots = new BotHandler[]{
                new TestTelegramBot()
        };

        // set bot to listen https://my-app.heroku.com/BOTTOKEN
        // register this URL as Telegram Webhook
        for (BotHandler bot : bots) {
            String token = bot.getToken();
            post("/" + token, bot);
            if (appUrl != null) {
                bot.getBot().execute(new SetWebhook().url(appUrl + "/" + token));
            }
        }
    }
}

可以轻松更改为长轮询:

bot.setUpdatesListener(updates -> {
    for (Update update : updates) {
        onUpdateReceived(update);
    }
    return UpdatesListener.CONFIRMED_UPDATES_ALL;
});

【讨论】:

    猜你喜欢
    • 2023-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多