我应该如何解决由Java在Heroku上运行引起的Java错误?

时间:2019-04-15 18:48:55

标签: java heroku telegram telegram-bot

我已经在Heroku上成功部署了我的应用程序,但是我的应用程序在运行时崩溃了。 我错了:

  

错误R10(引导超时)-> Web进程无法绑定到$ PORT   启动90秒

我在互联网上发现了此代码,该代码粘贴在主类中-没有结果:

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

Procfile:

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";
    }
}

1 个答案:

答案 0 :(得分:0)

这可以帮助您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;
});