1

I am using this example to upload message with attachment to slack:

How to upload any file on Slack via Slack-App in c#

I want to add user mention to the text variable.

I used both format id and name version but it didn't work. It displays as plain text with no notification to users

parameters3["text"] = "<@userName>";

update:

 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 mrkdwn_in {get; set;}
            public string footer_icon { get; set; }
        }
        // a slack file class
        class SlackFile
        {
            public String id { get; set; }
            public String name { get; set; }
            public String permalink_public { get; set; }
            public string permalink { get; set; }
        }        

        // reponse from file methods
        class SlackFileResponse
        {
            public bool ok { get; set; }
            public String error { get; set; }
            public SlackFile file { get; set; }
        }
        // reponse from message methods
        class SlackMessageResponse
        {
            public bool ok { get; set; }
            public String error { get; set; }
            public String channel { get; set; }
            public String ts { get; set; }
        }

this is my caller

public static void salesCongrat(string imgUrl, string msg)
{

    string postedMessage = "Outside message text";

    var sampleAttachment = new SlackAttachment[]

{
 new SlackAttachment {

            fallback = "browser cannot display this",
            text = "<@UEMTUFSM>",
            color = "e5345e",   
            pretext = "",
            author_name = " ",
            author_icon = "",
            author_link = "",
            title = msg,
            title_link = "",
            mrkdwn_in = "[\"text\"]",
            image_url=   imgeuURL,
            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);

}

file response :

{"ok":true,"channel":"","ts":"","message":{"bot_id":"","type":"message","text":"Outside message text","user":"","ts":"","attachments":[{"author_name":" ","fallback":"browser cannot display this","text":"<@>","title":" ","footer":"Posted at 1\/4\/2019\/ 18:47","id":1,"color":"e5345e"}]}}
Coder
  • 121
  • 9
  • It would be awesome if you could provide a [mcve] with all code inside the question itself. – mjwills Apr 01 '19 at 07:50
  • 1
    Looking at https://api.slack.com/changelog/2017-09-the-one-about-usernames you need to use the user ID - Looking at your code you are using a username at present? – Jono20201 Apr 01 '19 at 07:56
  • Yes when I try the user Id , slack shows an empty container. When I hover over it I get private user info . It doesn't send mention notification to user – Coder Apr 01 '19 at 07:58
  • sharing the screen : https://i.imgur.com/CS22LjR.png – Coder Apr 01 '19 at 08:10

1 Answers1

1

Your code is posting the mention ("<@アユ三君>", lets assume its a user ID) in the main message, but not in the attachment as your title suggests.

To include the mention in the attachment you need to put it in the initialization for your attachment object.

Examples:

            var sampleAttachment = new SlackAttachment[]                
            {
               new SlackAttachment
               {
                    fallback = "browser cannot display this",
                    text = "<@U12345678>",
                    color = "e5345e",                       
                    title = msg,
                    image_url = imgeURL,
                    footer = $"Posted at {DateTime.Now.Day}"
               } 
            };

Your mention will be empty and shown with a "private user info" tool tip, when Slack can not find the user ID. To double-check that your user ID is valid you can call users.list in your browser to get a list of all existing user Ids. Make sure to use the same token, that you are using in your app.

Example:

https://slack.com/api/users.list?token=MYTOKEN
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • 1
    Thanks I was hoping you see my question. I rewrote after adding mrkdown_in still not working – Coder Apr 01 '19 at 15:43
  • I think it posts fine but the notification doesnt occur also the information is hidden .When I hover over it I get *private user info* message – Coder Apr 01 '19 at 15:54
  • It looks like Slack can not find the user ID you provided in the message. I added some info to my answer including how to get a list of all valid user IDs for your workspace. – Erik Kalkoken Apr 01 '19 at 16:12
  • Also I found that you don't actually need the mrkdwn_in field. so I removed it from my answer. – Erik Kalkoken Apr 01 '19 at 16:12
  • 1
    Thank you ! you helped me again . – Coder Apr 01 '19 at 16:22
  • Is it possible to reach you outside StackOverflow ? or use a threadlike features to communicate? – Coder Apr 01 '19 at 16:22
  • yes. Stack Overflow has a build-in chat. Happy to talk to you there. And you can also reach me on Twitter under @ErikKalkoken – Erik Kalkoken Apr 01 '19 at 16:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191033/discussion-between-erik-kalkoken-and-coder). – Erik Kalkoken Apr 01 '19 at 16:29