【问题标题】:Unable to add a Telegram Bot Library; Beginners fail?无法添加电报机器人库;初学者失败?
【发布时间】:2020-06-27 07:56:33
【问题描述】:

我刚刚开始摆脱大学的任务并开始自己的项目。 我想编写一个 Java Telegram Bot 来与更多的类进行交互。不幸的是,我无法添加依赖权,或者它只是无法导入所有功能。我尝试遵循多个教程,但其中任何一个都有错误。最有前途的教程之一如下:https://github.com/rubenlagus/TelegramBots/wiki/Getting-Started

我按照说明(使用 Maven 添加了库)并输入了代码。在此之后,我导入了所需的库。但是程序无法调用方法“执行”,我不知道为什么。 我希望我指定的主题足够详细。 提前谢谢你。

主类:

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

【问题讨论】:

  • 你能发送 pom.xml 文件吗?还是你用了jar?
  • 我猜我用了一个罐子。因为我刚刚开始编程并且无法解决问题,所以项目不再进行。我不知道我什么时候会回到它。

标签: java bots telegram


【解决方案1】:

在 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 的令牌和用户名。您必须返回使用 BothFather 创建的配置。

【讨论】:

    【解决方案2】:

    你必须在getter botToken和botUsername中设置bot的token和用户名。

    return "<token>";
    
    return "<username_of_bot>";
    

    至少最小化令牌。

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 2016-11-11
      • 1970-01-01
      • 2020-03-22
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 2016-03-04
      相关资源
      最近更新 更多