package Bot;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendDocument;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
[enter image description here](https://i.sstatic.net/efyeLCvI.png)
public class TelegramBot extends TelegramLongPollingBot
{
@Override
public String getBotUsername() {
return "TestBobr_Bot";
}
@Override
public String getBotToken() {
return "7231977079:AAGoxvN54d-r0J4VJnYORkiKDuvJuk1g7UI";
}
@Override
public void onUpdateReceived(Update update)
{
// getting FileId, ChatId, Text of the message and name of the user
String DocumentId = update.getMessage().getDocument().getFileId();
String ChatId = update.getMessage().getChatId().toString();
String UserFirstName = update.getMessage().getChat().getFirstName();
String UserLastName = update.getMessage().getChat().getLastName();
String Text = update.getMessage().getText();
// formatting unix time into usual one
Integer UnixTime = update.getMessage().getDate();
Date date = new Date(UnixTime*1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("GMT+3"));
String formattedDate = sdf.format(date);
// sending the message back
SendMessage SentMessage = new SendMessage();
SendDocument SentDocument = new SendDocument();
SentDocument.setChatId(ChatId);
SentDocument.setCaption("This is a caption");
SentDocument.setDocument(DocumentId);
SentMessage.setText("User " + UserFirstName + " " + UserLastName + " Wrote: " + Text + " at " + formattedDate);
SentMessage.setChatId(ChatId);
try {
this.execute(SentMessage);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
}
Here’s one of my classes, called “TelegramBot”. It gets user’s message and time, when the user wrote it. then it formats unix tome into usual time and sends the back to user “<User’s name> wrote: <user’s message> at . I want my bot to get the photo, sent by user and send it back using File ID. In Full API reference for developers, writing telegram bots writed,that you can send files, by your document’s file ID using method SetDocument() like here:
SendDocument SentDocument = new SendDocument();
SentDocument.setChatId(ChatId);
SentDocument.setCaption("This is a caption");
SentDocument.setDocument(DocumentId[enter image description here](https://i.sstatic.net/mRLzv0Ds.png));
In reference for developers writed, that this method gets InputFile or FileId as String. But when I try to write in brackets name of String, which I got from user, it shows me an error:
‘setDocument([email protected] InputFile)’ in ‘org.telegram.telegrambots.meta.api.methods.send.SendDocument’ cannot be applied to ‘()’
So, as I understood, this method just can’t get File Id? If yes, how can I send files using File Id in other ways? If no, what do I do wrong? I’m just beginning developer and I’m really sorry if the code is bad. I hope you will help me. I’ll be very thankful.
Михаил Барыкин is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.