0

I'm building a Teams Bot following this Teams File Upload Bot example.

I'm trying to get a PDF file through, and have it responding back with the FileConsentCard followed by the FileInfoCard:

FileConsentCard and FileInfoCard

When clicking the content URL link provided, I get the following error where I'm not able to view the file or download.

FileInfoCard invalid link

The code I'm using is the same as the File Upload Bot example other than I've changed the file type to a PDF document on my web server.

Any ideas as to why the link it not valid? Some permissions maybe?

private CompletableFuture<Void> sendFileCard(
        TurnContext turnContext, String filename, long filesize
    ) { 
        
        Map<String, String> consentContext = new HashMap<>();
        consentContext.put("filename", filename);

        FileConsentCard fileCard = new FileConsentCard();
        fileCard.setDescription("This is the file I want to send you");
        fileCard.setSizeInBytes(filesize);
        fileCard.setAcceptContext(consentContext);
        fileCard.setDeclineContext(consentContext);

        Attachment asAttachment = new Attachment();
        asAttachment.setContent(fileCard);
        asAttachment.setContentType(FileConsentCard.CONTENT_TYPE);
        asAttachment.setName(filename);     

        Activity reply = turnContext.getActivity().createReply();
        reply.setAttachments(Collections.singletonList(asAttachment));

        return turnContext.sendActivityBlind(reply);
    }
private CompletableFuture<ResultPair<String>> upload(
        FileConsentCardResponse fileConsentCardResponse
    ) {
        AtomicReference<ResultPair<String>> result = new AtomicReference<>();       
        
        return CompletableFuture.runAsync(() -> {
            Map<String, String> context = (Map<String, String>) fileConsentCardResponse
                .getContext();
            File filePath = new File("files", context.get("filename"));
            HttpURLConnection connection = null;

            try {               
                
                URL url = new URL(fileConsentCardResponse.getUploadInfo().getUploadUrl());
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("PUT");
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Length", Long.toString(filePath.length()));
                connection.setRequestProperty(
                    "Content-Range",
                    String.format("bytes 0-%d/%d", filePath.length() - 1, filePath.length())
                );              

                try (
                    FileInputStream fileStream = new FileInputStream(filePath);
                    OutputStream uploadStream = connection.getOutputStream()
                ) {
                    byte[] buffer = new byte[4096];
                    int bytes_read;
                    while ((bytes_read = fileStream.read(buffer)) != -1) {                      
                        uploadStream.write(buffer, 0, bytes_read);
                    }                   
                }

                result.set(new ResultPair<String>(true, null));
            } catch (Throwable t) {             
                result.set(new ResultPair<String>(false, t.getLocalizedMessage()));
            } finally {                 
                if (connection != null) {
                    connection.disconnect();
                }
            }
        })
            .thenApply(aVoid -> result.get());
    }
private CompletableFuture<Void> fileUploadCompleted(
        TurnContext turnContext, FileConsentCardResponse fileConsentCardResponse
    ) {
        FileInfoCard downloadCard = new FileInfoCard();
        downloadCard.setUniqueId(fileConsentCardResponse.getUploadInfo().getUniqueId());
        downloadCard.setFileType(fileConsentCardResponse.getUploadInfo().getFileType());

        Attachment asAttachment = new Attachment();
        asAttachment.setContent(downloadCard);
        asAttachment.setContentType(FileInfoCard.CONTENT_TYPE);
        asAttachment.setName(fileConsentCardResponse.getUploadInfo().getName());        
        
        asAttachment.setContentUrl(fileConsentCardResponse.getUploadInfo().getContentUrl());

        Activity reply = MessageFactory.text(
            String.format(
                "<b>File uploaded.</b> Your file <b>%s</b> is ready to download",
                fileConsentCardResponse.getUploadInfo().getName()
            )
        );
        reply.setTextFormat(TextFormatTypes.XML);
        reply.setAttachment(asAttachment);

        return turnContext.sendActivityBlind(reply);
    }

I'm not getting any exceptions during the upload process, and it shows a file was created in my OneDrive but it's not available or empty.

OneDrive activity

lfdevjmp
  • 3
  • 4
  • We are also tried the same, but it is working fine from our end. https://user-images.githubusercontent.com/93527375/233080600-e2371d7b-9f46-4669-bd25-4d574c97f062.mp4 Could you please try to upload the other file? – Sayali-MSFT Apr 19 '23 at 13:06
  • It appears the issue occurs when files are uploaded directly to a teams site. https://answers.microsoft.com/en-us/msoffice/forum/all/hmm-looks-like-this-file-doesnt-have-a-preview-we/a3e77c84-356f-402a-9fff-cde013a48c25 – Sayali-MSFT Apr 19 '23 at 13:06

0 Answers0