How to upload a file - NO image - and comment it at the same time to Slack via SlackAPI using c#












0















I can upload images to Slack via my SlackAPI and I can upload files and I can upload images with comments - but I can not do this with files that are no images.
I'm sure it's a problem with my message-structure - have a look: All this is done via HttpClient!



This is my working fileUpload-method:



    public MultipartFormDataContent SendFileToChannel()
{
var requestContent = new MultipartFormDataContent();
var fileContent = new StreamContent(GetFile.ReadFile());
requestContent.Add(new StringContent(token), "token");
requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));

return requestContent;
}


there is no 'channel' in this method because I don't want to publish it yet.
Then I set "public_url_shared": true and get the public URL(with another method).
In the response evreything seems to be okay, shared is set to true and I get a permalink_public-value which I pass on to post a message containing this file but...



Now I should be able to post a message while using the permalink_url I get from the second method. And this works with images. But it doesn't work with files.
I allways get the error response "no_file_data".
Here is my method for this:



    public MultipartFormDataContent SendMessageWithFile(SlackFileResponse resp)
{
var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(token), "token");
requestContent.Add(new StringContent(channel), "channel");
requestContent.Add(new StringContent(text), "text");
requestContent.Add(new StringContent("[{ "fallback":"Anhang", "text":"", "":"" + resp.permalink_public + ""}]"), "attachments");

return requestContent;
}


Am I doing something wrong here? Because via the RTM-Slack one can easily just drag&drop a file in there and add a message to it. So it has to be possible via the SlackAPI ,too. Right?










share|improve this question



























    0















    I can upload images to Slack via my SlackAPI and I can upload files and I can upload images with comments - but I can not do this with files that are no images.
    I'm sure it's a problem with my message-structure - have a look: All this is done via HttpClient!



    This is my working fileUpload-method:



        public MultipartFormDataContent SendFileToChannel()
    {
    var requestContent = new MultipartFormDataContent();
    var fileContent = new StreamContent(GetFile.ReadFile());
    requestContent.Add(new StringContent(token), "token");
    requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));

    return requestContent;
    }


    there is no 'channel' in this method because I don't want to publish it yet.
    Then I set "public_url_shared": true and get the public URL(with another method).
    In the response evreything seems to be okay, shared is set to true and I get a permalink_public-value which I pass on to post a message containing this file but...



    Now I should be able to post a message while using the permalink_url I get from the second method. And this works with images. But it doesn't work with files.
    I allways get the error response "no_file_data".
    Here is my method for this:



        public MultipartFormDataContent SendMessageWithFile(SlackFileResponse resp)
    {
    var requestContent = new MultipartFormDataContent();
    requestContent.Add(new StringContent(token), "token");
    requestContent.Add(new StringContent(channel), "channel");
    requestContent.Add(new StringContent(text), "text");
    requestContent.Add(new StringContent("[{ "fallback":"Anhang", "text":"", "":"" + resp.permalink_public + ""}]"), "attachments");

    return requestContent;
    }


    Am I doing something wrong here? Because via the RTM-Slack one can easily just drag&drop a file in there and add a message to it. So it has to be possible via the SlackAPI ,too. Right?










    share|improve this question

























      0












      0








      0








      I can upload images to Slack via my SlackAPI and I can upload files and I can upload images with comments - but I can not do this with files that are no images.
      I'm sure it's a problem with my message-structure - have a look: All this is done via HttpClient!



      This is my working fileUpload-method:



          public MultipartFormDataContent SendFileToChannel()
      {
      var requestContent = new MultipartFormDataContent();
      var fileContent = new StreamContent(GetFile.ReadFile());
      requestContent.Add(new StringContent(token), "token");
      requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));

      return requestContent;
      }


      there is no 'channel' in this method because I don't want to publish it yet.
      Then I set "public_url_shared": true and get the public URL(with another method).
      In the response evreything seems to be okay, shared is set to true and I get a permalink_public-value which I pass on to post a message containing this file but...



      Now I should be able to post a message while using the permalink_url I get from the second method. And this works with images. But it doesn't work with files.
      I allways get the error response "no_file_data".
      Here is my method for this:



          public MultipartFormDataContent SendMessageWithFile(SlackFileResponse resp)
      {
      var requestContent = new MultipartFormDataContent();
      requestContent.Add(new StringContent(token), "token");
      requestContent.Add(new StringContent(channel), "channel");
      requestContent.Add(new StringContent(text), "text");
      requestContent.Add(new StringContent("[{ "fallback":"Anhang", "text":"", "":"" + resp.permalink_public + ""}]"), "attachments");

      return requestContent;
      }


      Am I doing something wrong here? Because via the RTM-Slack one can easily just drag&drop a file in there and add a message to it. So it has to be possible via the SlackAPI ,too. Right?










      share|improve this question














      I can upload images to Slack via my SlackAPI and I can upload files and I can upload images with comments - but I can not do this with files that are no images.
      I'm sure it's a problem with my message-structure - have a look: All this is done via HttpClient!



      This is my working fileUpload-method:



          public MultipartFormDataContent SendFileToChannel()
      {
      var requestContent = new MultipartFormDataContent();
      var fileContent = new StreamContent(GetFile.ReadFile());
      requestContent.Add(new StringContent(token), "token");
      requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));

      return requestContent;
      }


      there is no 'channel' in this method because I don't want to publish it yet.
      Then I set "public_url_shared": true and get the public URL(with another method).
      In the response evreything seems to be okay, shared is set to true and I get a permalink_public-value which I pass on to post a message containing this file but...



      Now I should be able to post a message while using the permalink_url I get from the second method. And this works with images. But it doesn't work with files.
      I allways get the error response "no_file_data".
      Here is my method for this:



          public MultipartFormDataContent SendMessageWithFile(SlackFileResponse resp)
      {
      var requestContent = new MultipartFormDataContent();
      requestContent.Add(new StringContent(token), "token");
      requestContent.Add(new StringContent(channel), "channel");
      requestContent.Add(new StringContent(text), "text");
      requestContent.Add(new StringContent("[{ "fallback":"Anhang", "text":"", "":"" + resp.permalink_public + ""}]"), "attachments");

      return requestContent;
      }


      Am I doing something wrong here? Because via the RTM-Slack one can easily just drag&drop a file in there and add a message to it. So it has to be possible via the SlackAPI ,too. Right?







      c# slack-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 12:03









      Fabian HeldFabian Held

      789




      789
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Here is why this approach does not work:



          Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url (and thumb_url) for attachments.



          Check out this page for a list of all available attachment properties.



          If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.



          To include a comment with your file upload just set the initial_comment property in your API call.



          Example:



          var requestContent = new MultipartFormDataContent();
          var fileContent = new StreamContent(GetFile.ReadFile());
          requestContent.Add(new StringContent(token), "token");
          requestContent.Add(new StringContent("my_channel"), "channels");
          requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
          requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));





          share|improve this answer


























          • I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

            – Erik Kalkoken
            Nov 16 '18 at 13:34











          • Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

            – Fabian Held
            Nov 16 '18 at 13:35











          • You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

            – Erik Kalkoken
            Nov 16 '18 at 13:52











          • I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

            – Erik Kalkoken
            Nov 16 '18 at 13:57








          • 1





            Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

            – Fabian Held
            Nov 16 '18 at 14:17












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337534%2fhow-to-upload-a-file-no-image-and-comment-it-at-the-same-time-to-slack-via-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Here is why this approach does not work:



          Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url (and thumb_url) for attachments.



          Check out this page for a list of all available attachment properties.



          If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.



          To include a comment with your file upload just set the initial_comment property in your API call.



          Example:



          var requestContent = new MultipartFormDataContent();
          var fileContent = new StreamContent(GetFile.ReadFile());
          requestContent.Add(new StringContent(token), "token");
          requestContent.Add(new StringContent("my_channel"), "channels");
          requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
          requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));





          share|improve this answer


























          • I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

            – Erik Kalkoken
            Nov 16 '18 at 13:34











          • Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

            – Fabian Held
            Nov 16 '18 at 13:35











          • You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

            – Erik Kalkoken
            Nov 16 '18 at 13:52











          • I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

            – Erik Kalkoken
            Nov 16 '18 at 13:57








          • 1





            Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

            – Fabian Held
            Nov 16 '18 at 14:17
















          1














          Here is why this approach does not work:



          Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url (and thumb_url) for attachments.



          Check out this page for a list of all available attachment properties.



          If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.



          To include a comment with your file upload just set the initial_comment property in your API call.



          Example:



          var requestContent = new MultipartFormDataContent();
          var fileContent = new StreamContent(GetFile.ReadFile());
          requestContent.Add(new StringContent(token), "token");
          requestContent.Add(new StringContent("my_channel"), "channels");
          requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
          requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));





          share|improve this answer


























          • I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

            – Erik Kalkoken
            Nov 16 '18 at 13:34











          • Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

            – Fabian Held
            Nov 16 '18 at 13:35











          • You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

            – Erik Kalkoken
            Nov 16 '18 at 13:52











          • I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

            – Erik Kalkoken
            Nov 16 '18 at 13:57








          • 1





            Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

            – Fabian Held
            Nov 16 '18 at 14:17














          1












          1








          1







          Here is why this approach does not work:



          Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url (and thumb_url) for attachments.



          Check out this page for a list of all available attachment properties.



          If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.



          To include a comment with your file upload just set the initial_comment property in your API call.



          Example:



          var requestContent = new MultipartFormDataContent();
          var fileContent = new StreamContent(GetFile.ReadFile());
          requestContent.Add(new StringContent(token), "token");
          requestContent.Add(new StringContent("my_channel"), "channels");
          requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
          requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));





          share|improve this answer















          Here is why this approach does not work:



          Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url (and thumb_url) for attachments.



          Check out this page for a list of all available attachment properties.



          If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.



          To include a comment with your file upload just set the initial_comment property in your API call.



          Example:



          var requestContent = new MultipartFormDataContent();
          var fileContent = new StreamContent(GetFile.ReadFile());
          requestContent.Add(new StringContent(token), "token");
          requestContent.Add(new StringContent("my_channel"), "channels");
          requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
          requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 14:01

























          answered Nov 16 '18 at 13:25









          Erik KalkokenErik Kalkoken

          13.6k32653




          13.6k32653













          • I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

            – Erik Kalkoken
            Nov 16 '18 at 13:34











          • Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

            – Fabian Held
            Nov 16 '18 at 13:35











          • You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

            – Erik Kalkoken
            Nov 16 '18 at 13:52











          • I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

            – Erik Kalkoken
            Nov 16 '18 at 13:57








          • 1





            Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

            – Fabian Held
            Nov 16 '18 at 14:17



















          • I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

            – Erik Kalkoken
            Nov 16 '18 at 13:34











          • Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

            – Fabian Held
            Nov 16 '18 at 13:35











          • You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

            – Erik Kalkoken
            Nov 16 '18 at 13:52











          • I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

            – Erik Kalkoken
            Nov 16 '18 at 13:57








          • 1





            Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

            – Fabian Held
            Nov 16 '18 at 14:17

















          I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

          – Erik Kalkoken
          Nov 16 '18 at 13:34





          I like to add that there is an undocumented API method called files.share that allows you to share uploaded files later. However, I would not recommend you to use it, since they require a legacy token to work and are officially not supported.

          – Erik Kalkoken
          Nov 16 '18 at 13:34













          Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

          – Fabian Held
          Nov 16 '18 at 13:35





          Hey, I'm sure I tried that already. When I use the files.upload I can not add any comment to it for the upload... I added the neccesary parameter(text) but it doesn't show when I use that method. And when I use chat.postMessage I obviously can not add a file... What am I doing wrong?

          – Fabian Held
          Nov 16 '18 at 13:35













          You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

          – Erik Kalkoken
          Nov 16 '18 at 13:52





          You can actually include a comment with the upload, but you need to provide the correct parameter to the API.

          – Erik Kalkoken
          Nov 16 '18 at 13:52













          I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

          – Erik Kalkoken
          Nov 16 '18 at 13:57







          I will add the info you need to my answer. But if I may: I would recommend you to start working more closely with the official Slack documentation. e.g if you look at files.upload you can see that there is no text property, but there is a comment property with a different name

          – Erik Kalkoken
          Nov 16 '18 at 13:57






          1




          1





          Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

          – Fabian Held
          Nov 16 '18 at 14:17





          Oh boy... yeah of course, I just looked it up. What an easy fix, it's called initial_comment. But come on... it is channel/channelS all over again... why do this? But yeah, I am trying to look into the documentation more, but it is hard for me to get a clue out of the most parts. This made sense to me now. Since I learned how message properties work here. But in the beginning this was like looking at hiroglyphs. Thank you very much, again! :)

          – Fabian Held
          Nov 16 '18 at 14:17




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337534%2fhow-to-upload-a-file-no-image-and-comment-it-at-the-same-time-to-slack-via-s%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python