Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload file to telegram servers and use it afterwards #1332

Open
igolyudov opened this issue Mar 10, 2024 · 1 comment
Open

Upload file to telegram servers and use it afterwards #1332

igolyudov opened this issue Mar 10, 2024 · 1 comment

Comments

@igolyudov
Copy link

InputFile class have javadoc described "Input file used to upload a file to Telegram server and use it afterwards".
But how i can get file_id for uploaded file and use it afterwards for send user?

@NaveenB2004
Copy link

NaveenB2004 commented Apr 7, 2024

If you're using the bot to upload (literally send) Document (general file, as opposed to photos, voice messages and audio files), you will receive the Message object that holding the mentioned Document object after successful execution. You can get the file_id, Identifier for this file, which can be used to download or reuse the file from that object.

// bot token
final String BOT_TOKEN = "123456xxxxxy";
// assign the client
TelegramClient telegramClient = new OkHttpTelegramClient(BOT_TOKEN);

// target chat id that file should be sent (String/Long)
final String CHAT_ID = "123456xxxxx";
// java.io.File (solid file) or String (URL to the target file) or String (file_id of the target file - your question)
final java.io.File FILE = new File("location");
// final String FILE = "https://example.com/targetFile";

SendDocument doc = SendDocument.builder()
   .chatId(CHAT_ID)
   .document(new InputFile(FILE))
   .build();
// other doc settings

Message message = null;
try {
     message = telegramClient.execute(doc);
} catch (TelegramApiException e) {
     // handle the exception
}
        
if (message != null) {
     String fileId = message.getDocument().getFileId();
     // other operations
}

Or, you can send your target file to the bot and get the update using the consume(Update update) method by invoking String fileId = update.getMessage().getDocument().getFileId();.

Now you have the fileId. Use it in the SendDocument builder as .document(new InputFile(fileId)) to send it. 👍 See also InputFile.

Maybe this is out of the question, but you can use bots like Telegram Bot Raw to get the Message object details (in JSON format) without configuring your own bot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants