如何在松弛附件消息中添加提及?

时间:2019-04-01 07:45:03

标签: c# slack-api

我正在使用此示例上传带有松弛附件的邮件:

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

我想在 text 变量中添加用户提及。

我同时使用了格式ID和名称版本,但没有用。 它显示为纯文本,不通知用户

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

更新:

 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; }
        }

这是我的来电者

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);

}

文件响应:

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

1 个答案:

答案 0 :(得分:1)

您的代码在主邮件中发布了提及(“ <@アユ三君>”,假设其为用户ID),但标题中并未提及附件。

要将提及内容包含在附件中,您需要将其放入附件对象的初始化中。

示例:

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

当Slack找不到用户ID时,您的提及将为空,并带有“私人用户信息”工具提示。要仔细检查您的用户ID是否有效,可以在浏览器中调用users.list,以获取所有现有用户ID的列表。确保使用您在应用程序中使用的相同令牌。

示例:

https://slack.com/api/users.list?token=MYTOKEN