Here is my code, it works (but not as it should), with which I can get the text of the post and 1 attached media file, but if there is more than 1 media file in the post, then I only get the last one.
I use the library, kotlogram
almost the same as telethon
try {
var messageText = ""
// Get most recent message
val tl = client.messagesGetHistory(inputPeer, 0, 0, 0, 1, 0, 0)
var mess = tl.messages
var filename: String
var index = 0
for (messages in mess) {
if (messages is TLMessage) {
val b: TLAbsMessageMedia? = messages.media
index++
when (b) {
is TLMessageMediaPhoto -> {
messageText = b.caption
filename = "photo$index.jpg"
println(messageText)
var ROOT_DIR = "C:\\fopii"
fos = withContext(Dispatchers.IO) {
FileOutputStream(File(ROOT_DIR, filename))
}
Input.add(b.getMediaInput()!!.inputFileLocation)
size.add(b.getMediaInput()!!.size)
// downloadSync closes the stream automatically
}
is TLMessageMediaDocument -> {
messageText = b.caption
val tlDocumentt = b.document.asDocument
val p = (tlDocumentt.attributes.stream()
.filter { attr: TLAbsDocumentAttribute? -> attr is TLDocumentAttributeFilename }
.findFirst().get() as TLDocumentAttributeFilename).fileName
filename = p
Input.add(b.getMediaInput()!!.inputFileLocation)
size.add(b.getMediaInput()!!.size)
var ROOT_DIR = "C:\\fopii"
fos = withContext(Dispatchers.IO) {
FileOutputStream(File(ROOT_DIR, filename))
}
}
}
}
println(Input)
println("$size a")
println(messageText)
client.downloadSync(Input.get(0), size.get(0), fos)
}
}catch (e: Exception){
e.printStackTrace()
}finally {
client.close()
}
I get only 1 latest media file, but I need everything
You use
val tl = client.messagesGetHistory(inputPeer, 0, 0, 0, 1, 0, 0)
If you look at the parameters for getHistory
in the documentation you set a limit of 1.
Try to increase the number to 10 or 20.
I don't know how many files you want to grab. But you need a pagination or an infinity scrolling. I can not imagine that it is allowed to set the limit > 100