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"}]}}