1

I would like to add more than one attachment into my postmessage call.

I am using the sample code from this post How to upload any file on Slack via Slack-App in c#

    public class SlackAttachment
    {
        public string fallback { get; set; }
        public string color { get; set; }
        public string pretext { get; set; }
        public string author_name { get; set; }
        public string author_link { get; set; }
        public string author_icon { get; set; }
        public string title { get; set; }
        public string title_link { get; set; }
        public string text { get; set; }
        public string image_url { get; set; }
        public string thumb_url { get; set; }
        public string footer { get; set; }
        public string footer_icon { get; set; }
    }


    public static void SlackPoster(string imgUrl, string channelName){

        string postedMessage = "MessageText";
        var sampleAttachment = new SlackAttachment[]

    {
     new SlackAttachment {
                fallback = "",
                text = "",
                color = "134f46",
                pretext = "",
                author_name = "",
                author_icon = "",
                author_link = "",
                title = $"",
                title_link = "",
                image_url = imgUrl,
             thumb_url = @"https://i.imgur.com/aFAA6tj.png\",
                footer = $"Posted at {DateTime.Now.Day}/" +
           $"{DateTime.Now.Month}/{DateTime.Now.Year}/ " +
           $"{DateTime.Now.Hour}:{DateTime.Now.Minute }",
                footer_icon = ""
                         }
    };

        var attachmentsJson = JsonConvert.SerializeObject(sampleAttachment);
        var data = new NameValueCollection();
        data["token"] = myToken;
        data["channel"] = channelName;
        data["as_user"] = "true";
        data["text"] = postedMessage;
        data["attachments"] = attachmentsJson;
        var client = new WebClient();
        var response = client.UploadValues("https://slack.com/api/chat.postMessage", "POST", data);
        string responseInString = Encoding.UTF8.GetString(response);
        Console.WriteLine(responseInString);

I create another attachment variable sampleAttachment2 and serialize it:

var attachmentsJson2 = JsonConvert.SerializeObject(sampleAttachment2);

but I don't know the right syntax to add both attachments into

data["attachments"] object 

Appreciate your help

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
Coder
  • 121
  • 9

1 Answers1

0

sampleAttachment is an array of SlackAttachment objects. To add more attachment just add more SlackAttachment objects to that array.

Example:

var sampleAttachment = new SlackAttachment[]
{
    new SlackAttachment {
            fallback = "",
            text = "1st attachment"
    },
    new SlackAttachment {
            fallback = "",
            text = "2nd attachment",
    }
};

The correct syntax for the API parameter attachments is an array of attachment array's as JSON string.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Thanks. There is another thing I want to ask. I am trying to create a slack blocks class to represents : Section Divider Image Actions and Context . Can I create a question with what I tried? and you help me with the syntax because it is difficult for me. – Coder Jun 23 '19 at 17:53
  • Of course. Just post a new question with your current code and what the issue is. I will be happy to help as best as I can – Erik Kalkoken Jun 23 '19 at 17:55