我应该如何修复这个Java错误,因为它在Heroku上工作?
我已经在Heroku上成功部署了我的应用程序,但我的应用程序在运行时崩溃。 我得到一个错误: 错误R10(启动超时)->Web进程无法绑定到内的$PORT 发射90秒 我在互联网上找到了这个代码,它粘贴在主类中-没有结果:我应该如何修复这个Java错误,因为它在Heroku上工作?,java,heroku,telegram,telegram-bot,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"); 程序文件: web: java $JAVA_O
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>
这可能对您有所帮助,但它使用其他库来处理Telegram Bot API——
有一个Procfile(需要更新那里的主类)和build.gradle文件用于部署。默认情况下,它设置Webhook:
公共类主{
公共静态void main(字符串[]args){
最终字符串portNumber=System.getenv(“端口”);
if(端口号!=null){
端口(整数.parseInt(端口号));
}
//设置webhook的当前应用程序url
//应通过heroku配置变量进行设置
// https://devcenter.heroku.com/articles/config-vars
//heroku配置:设置应用程序的URL=https://app-for-my-bot.herokuapp.com
最后一个字符串appUrl=System.getenv(“APP_URL”);
//定义机器人程序列表
BotHandler[]机器人程序=新的BotHandler[]{
新的TestTelegramBot()
};
//将bot设置为侦听https://my-app.heroku.com/BOTTOKEN
//将此URL注册为电报Webhook
用于(BotHandler机器人程序:机器人程序){
字符串标记=bot.getToken();
post(“/”+令牌,bot);
if(appUrl!=null){
bot.getBot().execute(新的SetWebhook().url(appUrl+“/”+token));
}
}
}
}
可以轻松更改为长轮询:
bot.setUpdatesListener(更新->{
用于(更新:更新){
onUpdateReceived(更新);
}
返回UpdatesListener.CONFIRMED\u UPDATES\u ALL;
});
这可能会对您有所帮助,但它使用其他库来处理Telegram Bot API——
有一个Procfile(需要更新那里的主类)和build.gradle文件用于部署。默认情况下,它设置Webhook:
公共类主{
公共静态void main(字符串[]args){
最终字符串portNumber=System.getenv(“端口”);
if(端口号!=null){
端口(整数.parseInt(端口号));
}
//设置webhook的当前应用程序url
//应通过heroku配置变量进行设置
// https://devcenter.heroku.com/articles/config-vars
//heroku配置:设置应用程序的URL=https://app-for-my-bot.herokuapp.com
最后一个字符串appUrl=System.getenv(“APP_URL”);
//定义机器人程序列表
BotHandler[]机器人程序=新的BotHandler[]{
新的TestTelegramBot()
};
//将bot设置为侦听https://my-app.heroku.com/BOTTOKEN
//将此URL注册为电报Webhook
用于(BotHandler机器人程序:机器人程序){
字符串标记=bot.getToken();
post(“/”+令牌,bot);
if(appUrl!=null){
bot.getBot().execute(新的SetWebhook().url(appUrl+“/”+token));
}
}
}
}
可以轻松更改为长轮询:
bot.setUpdatesListener(更新->{
用于(更新:更新){
onUpdateReceived(更新);
}
返回UpdatesListener.CONFIRMED\u UPDATES\u ALL;
});
此应用程序是否处理web请求(即HTTP请求)?如果没有,您需要将Procfile
中的web:
更改为bot:
或类似<代码>网络需要HTTP端口绑定是的,这是一个简单的电报机器人。据我所知,它适用于HTTPS请求确保它绑定到0.0.0.0和PORTYes,我已经签出了,但没有结果此应用程序可以处理web请求(即HTTP请求)?如果没有,您需要将Procfile
中的web:
更改为bot:
或类似<代码>网络需要HTTP端口绑定是的,这是一个简单的电报机器人。据我所知,它适用于HTTPS请求确保它绑定到0.0.0.0和PORTYes,我已经签出了,但没有结果
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";
}
}