无法添加Telegram机器人库;初学者失败?

huangapple 未分类评论59阅读模式
标题翻译

Unable to add a Telegram Bot Library; Beginners fail?

问题

我刚刚开始远离大学任务,做自己的项目。我想编写一个Java Telegram机器人来与进一步的课程互动。不幸的是,我无法正确添加依赖,或者它无法导入所有函数。我尝试遵循多个教程,但无论哪个教程都会出现错误。其中最有希望的教程之一是以下链接:https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started

我按照说明(使用Maven添加库)并放入了代码。在这之后,我导入了所需的库。然而,程序无法调用"execute"方法,我不知道原因。希望我已经足够详细地说明了主题。提前谢谢。

主类:

import org.telegram.telegrambots.ApiContextInitializer;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.LongPollingBot;

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

        ApiContextInitializer.init();

        TelegramBotsApi botsApi = new TelegramBotsApi();

        try {
            botsApi.registerBot((LongPollingBot) new Bot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

机器人类:

import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

public class Bot extends TelegramLongPollingBot {

    @Override
    public void onUpdateReceived(Update update) {
        // We check if the update has a message and the message has text
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                    .setChatId(update.getMessage().getChatId())
                    .setText(update.getMessage().getText());
            try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        return null;
    }

    @Override
    public String getBotToken() {
        return null;
    }

    @Override
    public void onClosing() {

    }
}

错误:

Error:(16, 17) java: cannot find symbol
  symbol:   method execute(org.telegram.telegrambots.meta.api.methods.send.SendMessage)
  location: class Bot
英文翻译

I've just started to go away from tasks of the university and do my own projects.
I want to program a Java Telegram Bot to interact with further classes. Unfortunately I'm not able to add the dependency right or it just cant import all of the functions. I tried to follow multiple tutorials but I got errors in either of them. One of the most promising tutorials was the following: https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started

I followed the instructions (added the library with Maven) and put in the code. After this I imported the needed librarie. However the program isn't able to call the method "execute" and I don't know why.
I hope I specified the topic detailled enough.
Thank you in advance.

Main Class:

import org.telegram.telegrambots.ApiContextInitializer;
        import org.telegram.telegrambots.meta.TelegramBotsApi;
        import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
        import org.telegram.telegrambots.meta.generics.LongPollingBot;

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

        ApiContextInitializer.init();

        TelegramBotsApi botsApi = new TelegramBotsApi();

        try {
            botsApi.registerBot((LongPollingBot) new Bot());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
    }
}

Bot Class

import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

public class Bot extends TelegramLongPollingBot {

    @Override
    public void onUpdateReceived(Update update) {
        // We check if the update has a message and the message has text
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = new SendMessage() // Create a SendMessage object with mandatory fields
                    .setChatId(update.getMessage().getChatId())
                    .setText(update.getMessage().getText());
            try {
                execute(message); // Call method to send the message
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        return null;
    }

    @Override
    public String getBotToken() {
        return null;
    }

    @Override
    public void onClosing() {

    }
}

Error

Error:(16, 17) java: cannot find symbol
  symbol:   method execute(org.telegram.telegrambots.meta.api.methods.send.SendMessage)
  location: class Bot

答案1

得分: 0

在 POM.xml 文件中,我添加了以下依赖:

<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
<dependency>
    <groupId>org.telegram</groupId>
    <artifactId>telegrambots</artifactId>
    <version>4.9.2</version>
</dependency>

你也可以直接下载这个 JAR 包。你必须使用 BotFather 创建和定义机器人,使用一个用户名,获取令牌并创建继承 TelegramLongPollingBot 的类。

你的问题出在令牌和用户名上,它们总是返回 null。你必须返回使用 BotFather 创建的配置。

英文翻译

in the POM.xml file I added the follow dependency:

&lt;!-- https://mvnrepository.com/artifact/org.telegram/telegrambots --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.telegram&lt;/groupId&gt;
	&lt;artifactId&gt;telegrambots&lt;/artifactId&gt;
	&lt;version&gt;4.9.2&lt;/version&gt;
&lt;/dependency&gt;

You can also download directly the jar.
You must create and define the bot with BotFather, use a username, take the token and create the class that extends TelegramLongPollingBot.

Your problem is on the token and username that return always null. You must return the configuration created with BothFather.

答案2

得分: 0

你必须在获取器(getters)中设置botToken和botUsername,分别为机器人的令牌(token)和用户名(username)。

return "<token>";

return "<username_of_bot>";

至少最小限度为令牌。

英文翻译

You must set in the getters botToken and botUsername the token and username of bot.

return &quot;&lt;token&gt;&quot;;

return &quot;&lt;username_of_bot&gt;&quot;;

at least minimum the token.

huangapple
  • 本文由 发表于 2020年3月16日 15:41:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/60701997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定