我正在尝试通过Slack的API使用chat.postMesage,但无法将附件作为邮件的一部分发送。我认为可以使用image_url作为attachment对象的一部分,以便将图像显示为消息的一部分。
我在响应中没有收到任何错误,但也没有看到任何附件。邮件正在发布中,但没有任何附件。
这就是我想要做的
public async Task<string> PostMessage()
{
var response = string.Empty;
var slacAttributes = new stackAttributes
{
channel = "testapp",
text = $" {DateTime.Now} > {Environment.NewLine} Good Morning all!!!{Environment.NewLine} new line",
attachments = new slackAttachments { fallback = "exception", text = "image text",title="kuku", image_url = "https://i.imgur.com/jO9N3eJ.jpg" }
};
try
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "xoxp-927360717313-937536168112-927367533025-c1065234477a3de10257bc69f523f789");
var atttrJson = slacAttributes;
var json = new JavaScriptSerializer().Serialize(atttrJson);
var buffer = System.Text.Encoding.UTF8.GetBytes(json);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage result = await client.PostAsync("https://slack.com/api/chat.postMessage", byteContent);
if(result.IsSuccessStatusCode)
{
var content = await result.Content?.ReadAsByteArrayAsync();
response = Encoding.UTF8.GetString(content, 0, content.Length);
}
}
}
catch(Exception e)
{
throw new Exception($"An error occured while Posting to slack.{e}");
}
return response;
}发布于 2020-02-14 00:11:57
attachments属性必须是附件对象的数组。从您的代码看,您似乎只提供了一个附件对象,而不是一个数组。
https://stackoverflow.com/questions/60208199
复制相似问题