rubenlagus / Deepthought

Telegram Client written in Java to support multiple custom implementations
74 stars 41 forks source link

Create instance TLAbsInputMedia for Send message with document or image file #18

Open edbrik opened 7 years ago

edbrik commented 7 years ago

Hi.

I really tired create te object TLAbsInputMedia but i cant send messages with files or images This is the code I use

TLAbsInputMedia media = new TLInputMediaDocument();
File initialFile = new File("C:\\virtual\\vivian.jpg");
InputStream targetStream = new FileInputStream(initialFile);
media.deserialize(targetStream, null);
kernel.getKernelComm().sendMedia(databaseManager.getUserById(16264778), media);
amin1softco commented 7 years ago

i think you must follow bellow code

    int task = kernel.getKernelComm().getUploader().requestTask("C:\\virtual\\vivian.jpg", null);
    kernel.getKernelComm().getUploader().waitForTask(task);
    int resultState = kernel.getKernelComm().getUploader().getTaskState(task);
    Uploader.UploadResult result = kernel.getKernelComm().getUploader().getUploadResult(task);

TLAbsInputFile inputFile;

    if (result.isUsedBigFile()) {
      inputFile = new TLInputFileBig();
      inputFile.setId(result.getFileId());
      inputFile.setName("vivian.jpg");
      inputFile.setParts(result.getPartsCount());
    } else {
      inputFile = new TLInputFile();
      inputFile.setId(result.getFileId());
      inputFile.setName("vivian.jpg");
      inputFile.setParts(result.getPartsCount());  
    }

TLAbsInputMedia  _media = new TLInputMediaUploadedPhoto(); // for only send image png , jpg not as document
          ((TLInputMediaUploadedPhoto)_media).setCaption("caption");
          ((TLInputMediaUploadedPhoto)_media).setFile(inputFile);

TLAbsInputPeer _peer= (TLAbsInputPeer) new TLInputPeerUser();
         ((TLInputPeerUser)_peer).setUserId(id); //get user id from your db
         ((TLInputPeerUser)_peer).setAccessHash(hash); //get user hash from your db

  TLRequestMessagesSendMedia request = new TLRequestMessagesSendMedia();
            request.setPeer(_peer);
            request.setMedia(_media);
            request.setRandomId(rnd.nextLong());
 TLUpdates tlUpdates = (TLUpdates) kernel.getKernelComm().getApi().doRpcCall((TLMethod) request);
edbrik commented 7 years ago

hi.

thank you very much, one question more.

for send xlsx (MS Excel) and pdf files i try use the TLInputMediaUploadedDocumentbut doest work.

TLAbsInputMedia  _media = new TLInputMediaUploadedDocument (); 

          ((TLInputMediaUploadedDocument )_media).setCaption("caption");
          ((TLInputMediaUploadedDocument )_media).setFile(inputFile);
amin1softco commented 7 years ago

for pdf file must change as follow

TLAbsInputMedia _media = new TLInputMediaUploadedDocument();
         TLVector<TLAbsDocumentAttribute> attributes = new TLVector<>();
           TLDocumentAttributeFilename fileNam = new TLDocumentAttributeFilename();
           fileNam.setFileName("yourfile.pdf");
            attributes.add(fileNam);

          ((TLInputMediaUploadedDocument) _media).setCaption("caption");
           ((TLInputMediaUploadedDocument) _media).setMimeType("application/pdf");//this based file extension
          ((TLInputMediaUploadedDocument) _media).setFile(inputFile);
          ((TLInputMediaUploadedDocument) _media).setAttributes(attributes);
Extension MIME Type
.doc      application/msword
.dot      application/msword

.docx     application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx     application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm     application/vnd.ms-word.document.macroEnabled.12
.dotm     application/vnd.ms-word.template.macroEnabled.12

.xls      application/vnd.ms-excel
.xlt      application/vnd.ms-excel
.xla      application/vnd.ms-excel

.xlsx     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx     application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm     application/vnd.ms-excel.sheet.macroEnabled.12
.xltm     application/vnd.ms-excel.template.macroEnabled.12
.xlam     application/vnd.ms-excel.addin.macroEnabled.12
.xlsb     application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt      application/vnd.ms-powerpoint
.pot      application/vnd.ms-powerpoint
.pps      application/vnd.ms-powerpoint
.ppa      application/vnd.ms-powerpoint

.pptx     application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx     application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx     application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam     application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm     application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm     application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm     application/vnd.ms-powerpoint.slideshow.macroEnabled.12
edbrik commented 7 years ago

Hi .

The first time I uploaded an image worked fine but when I try again it does not send anything.

i change the code for

request.setRandomId(rnd.nextLong());

to

request.setRandomId(Math.round(Math.random()));

amin1softco commented 7 years ago

no problem i think this better maybe negative value create problem!


Math.abs(new Random().nextLong())
``
edbrik commented 7 years ago

Works great, thank you very much for the support!!

edbrik commented 7 years ago

hi amin1softco one question please.

How to know if the file was sent successfully?

amin1softco commented 7 years ago

hi edbrik i think with 1-try catch block and 2-stored result in tlUpdates and check read stat with updates recived

if problem occur you may get RPCException and better @rubenlagus say answer really i not member of telegram support 😄

edbrik commented 7 years ago

Thanks for your support :D

mehdi1602 commented 7 years ago

Hi Amin and edbrik, could you please help me in finding a way to read old messages from a channel or supergroup. for example think that the user want to move and see old messages. how could I have access to old messages?

amin1softco commented 7 years ago

hi mehdi1602 with argument of TLRequestMessagesGetHistory you can handle this i think .

mehdi1602 commented 7 years ago

thanks Amin, I used as below

                TLRequestMessagesGetHistory tlReq = new TLRequestMessagesGetHistory();
                TLAbsInputPeer _peer = (TLAbsInputPeer) new TLInputPeerChat();
                ((TLInputPeerChat) _peer).setChatId(update.getChannelId()); 
                tlReq.setPeer(_peer);
                TLAbsMessages messagesEx = kernelComm.getApi().doRpcCall(tlReq);`

but all the time i get exceptions INVALID_CHANNEL_ID or INVALID_CHAT_ID

          ` org.telegram.api.engine.RpcException: CHAT_ID_INVALID`

could you please help me to know why I get this error?

mehdi1602 commented 7 years ago

hi amin, could you please let me know how can i download (photo , videos, and all files in general) that is related to a message? in other word how can i work with telegram downloader?