rubenlagus / TelegramBots

Java library to create bots using Telegram Bots API
https://telegram.me/JavaBotsApi
MIT License
4.76k stars 1.22k forks source link

Error on starting #877

Open nicecraftz opened 3 years ago

nicecraftz commented 3 years ago

EXCEPTION

Exception in thread "main" java.lang.NullPointerException
    at
org.telegram.telegrambots.facilities.TelegramHttpClientBuilder.createConnectionManager(TelegramHttpClientBuilder.java:36)
    at org.telegram.telegrambots.facilities.TelegramHttpClientBuilder.build(TelegramHttpClientBuilder.java:28)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$ReaderThread.start(DefaultBotSession.java:155)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession.start(DefaultBotSession.java:78)
    at org.telegram.telegrambots.meta.TelegramBotsApi.registerBot(TelegramBotsApi.java:75)
    at Main.main(Main.java:10)

MAIN CLASS:

import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

public class Main {

    public static void main(String[] args) {
        try {
            TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
            telegramBotsApi.registerBot(new VoltaBot());
        } catch(TelegramApiException exception) {
            exception.printStackTrace();
        }
    }
}

BOT CLASS


import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.meta.generics.BotOptions;
import org.telegram.telegrambots.meta.generics.LongPollingBot;

public class VoltaBot implements LongPollingBot {

    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields
            message.setChatId(update.getMessage().getChatId().toString());
            message.setText(update.getMessage().getText());
        }
    }

    @Override
    public BotOptions getOptions() {
        return null;
    }

    @Override
    public void clearWebhook() throws TelegramApiRequestException {

    }

    @Override
    public String getBotUsername() {
        return "voltabot";
    }

    @Override
    public String getBotToken() {
        return "myToken";
    }
}
aNNiMON commented 3 years ago

Replace implements LongPollingBot with extends TelegramLongPollingBot, then remove these methods:

@Override
public BotOptions getOptions() {
    return null;
}

@Override
public void clearWebhook() throws TelegramApiRequestException {
}
nicecraftz commented 3 years ago

Replace implements LongPollingBot with extends TelegramLongPollingBot, then remove these methods:

@Override
public BotOptions getOptions() {
    return null;
}

@Override
public void clearWebhook() throws TelegramApiRequestException {
}

ty very much!

rubenlagus commented 3 years ago

@NiceCraftz Solved? Good to close?

nicecraftz commented 1 year ago

Yes