Search code examples
c#slack

How to upload more than one attachments in slack C#


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


Solution

  • 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.