rubenlagus / TelegramBots

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

TelegramApiException message. #310

Closed sajjaadalipour closed 7 years ago

sajjaadalipour commented 7 years ago

I have a problem with TelegramApiException when I send a message with bad chat id, exception message just is Error sending message i need return why can't send the message.

Clevero commented 7 years ago

Could you please provide a code snippet?

sajjaadalipour commented 7 years ago
public class LongPollingInstance extends TelegramLongPollingBot implements MessageSender {
    @Autowired
    public void setTelegramListener(TelegramListener telegramListener) {
        this.telegramListener = telegramListener;
    }

    private TelegramListener telegramListener;

    @Override
    public void onUpdateReceived(Update update) {
        telegramListener.onUpdate(update);
    }

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

    @Override
    public String getBotToken() {
        return "token is here";
    }

    @Override
    public void onClosing() {
        return;
    }

    @Override
    public void text(String chatId, String text) throws TelegramApiException {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId(chatId);
        sendMessage.setText(text);
        sendMessage.setParseMode("html");
        this.text(sendMessage);
    }    
@Override
    public void text(SendMessage sendMessage) throws TelegramApiException {
        execute(sendMessage);
    }

    @Override
    public void photo(String chatId, String url, String caption) throws TelegramApiException {
        SendPhoto sendPhoto = new SendPhoto();
        sendPhoto.setChatId(chatId);
        sendPhoto.setPhoto(url);
        sendPhoto.setCaption(caption);
        this.photo(sendPhoto);
    }

    @Override
    public void photo(SendPhoto sendPhoto) throws TelegramApiException {
        sendPhoto(sendPhoto);
    }

    @Override
    public void document(String chatId, String url, String caption) throws TelegramApiException {
        SendDocument sendDocument = new SendDocument();
        sendDocument.setChatId(chatId);
        sendDocument.setDocument(url);
        sendDocument.setCaption(caption);
        this.sendDocument(sendDocument);
    }

    @Override
    public void document(SendDocument sendDocument) throws TelegramApiException {
        sendDocument(sendDocument);
    }

}
sajjaadalipour commented 7 years ago

@Clevero Hello. Did you see my code !?

Clevero commented 7 years ago

@smartdeath Sorry, was a bit busy last days. Will look into it after work!

Clevero commented 7 years ago

Try to print the StackTrace (or analyze) rather than just reading the exception message

e.getMessage()

SendMessage ms = new SendMessage();
    ms.setChatId(133333337L); //bad chat id
    ms.setText(text);
    try {
        execute(ms);
    } catch (TelegramApiException e) {
        System.out.println(e.getMessage());
    }

Will cause the following error message:

Error sending message

e.printStackTrace()

SendMessage ms = new SendMessage();
    ms.setChatId(133333337L); //bad chat id
    ms.setText(text);
    try {
        execute(ms);
    } catch (TelegramApiException e) {
        e.printStackTrace();
    }

Will cause the following error message:

org.telegram.telegrambots.exceptions.TelegramApiRequestException: Error sending message: [400] Bad Request: chat not found
    at org.telegram.telegrambots.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:170)
    at org.telegram.telegrambots.api.methods.send.SendMessage.deserializeResponse(SendMessage.java:24)
    at org.telegram.telegrambots.bots.DefaultAbsSender.sendApiMethod(DefaultAbsSender.java:605)
    at org.telegram.telegrambots.bots.AbsSender.execute(AbsSender.java:53)
    at MyClass.sndMessage(MyClass.java:97)
    at MyClass.onUpdateReceived(MyClass.java:38)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:271)
sajjaadalipour commented 7 years ago

@Clevero I know can use of printStackTrace I want to get just message exception.

KyouinDev commented 7 years ago

@smartdeath try this way:

public String stackTrace(Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    e.printStackTrace(pw);
    String stacktrace = sw.toString();
    pw.close();

    try {
    sw.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return stacktrace;
}
sajjaadalipour commented 7 years ago

@Clevero , @KyouinDev Thank you . But I fixed it with this way :

    public void text(SendMessage sendMessage) throws TelegramApiException {
        try {
            execute(sendMessage);
        } catch (TelegramApiRequestException e) {
            logger.error(e.getApiResponse() + " " + e.getErrorCode());
        }
    }