send image to telegram as InputStream instead of File

  Kiến thức lập trình

I have a Java code that works fine, it sends a local image from a local directory to a Telegram user:

public class TelegramBot extends TelegramLongPollingBot {

    public TelegramBot(...) {
        super(botToken);
        this.botName = botName();
    }

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

    @Override
    public void onUpdateReceived(Update update) {
        var message = update.getMessage();
        var incomingMessage = Objects.isNull(message.getText()) ? "" : message.getText().toLowerCase();
        var user = message.getFrom();
        var userId = user.getId();

        execute(SendPhoto.builder()
                .chatId(userId)
                .photo(new InputFile(new java.io.File("/home/xxx/img/hello.png")))
                .build());
    }
}

Maven:

<dependency>
    <groupId>org.telegram</groupId>
    <artifactId>telegrambots</artifactId>
    <version>${telegrambots.version}</version>
</dependency>

But I need to send images from a JAR file as InputStream:

InputStream s = this.getClass().getResourceAsStream("/img/hello.png")

Is that possible somehow? Unfortunately, the examples, API docs, and Google did not help.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT