C# 带有chat.postMessage的图像附件-Slack API
我正在尝试将chat.postmasage与Slack的API一起使用,但无法将附件作为消息的一部分发送。 我想我可以使用C# 带有chat.postMessage的图像附件-Slack API,c#,slack-api,C#,Slack Api,我正在尝试将chat.postmasage与Slack的API一起使用,但无法将附件作为消息的一部分发送。 我想我可以使用image\u url作为attachment对象的一部分,以便将图像显示为消息的一部分 我没有收到任何错误的答复,但也没有看到任何附件。 正在发布邮件,但没有任何附件 这就是我要做的 public async Task<string> PostMessage() { var response = string.Empty; var slacA
image\u 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;
}
public异步任务PostMessage()
{
var response=string.Empty;
var slacAttributes=新的stackAttributes
{
channel=“testapp”,
text=$“{DateTime.Now}>{Environment.NewLine}大家早上好!!!{Environment.NewLine}新行”,
附件=新的slackAttachments{fallback=“exception”,text=“image text”,title=“kuku”,image\u url=”https://i.imgur.com/jO9N3eJ.jpg" }
};
尝试
{
使用(var client=new HttpClient())
{
client.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“承载人”,“xoxp-927360717313-937536168112-927367533025-c1065234477a3de10257bc69f523f789”);
var atttrJson=slacAttributes;
var json=new JavaScriptSerializer().Serialize(atttrJson);
var buffer=System.Text.Encoding.UTF8.GetBytes(json);
var byteContent=新的ByteArrayContent(缓冲区);
byteContent.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/json”);
HttpResponseMessage结果=等待客户端。PostAsync(“https://slack.com/api/chat.postMessage“,字节内容);
if(结果。IsSuccessStatusCode)
{
var content=wait result.content?.ReadAsByteArrayAsync();
response=Encoding.UTF8.GetString(content,0,content.Length);
}
}
}
捕获(例外e)
{
抛出新异常($“过帐到slack时出错。{e}”);
}
返回响应;
}
附件属性必须是附件对象的数组。从代码看,您似乎只提供了一个附件对象,而不是一个数组。附件必须是一个附件数组。从您的代码来看,您似乎只提供了一个附件。当然,请将JSON添加到您的问题中。作为比较:这里有一个C#中的工作示例:我的问题似乎是将附件作为数组发送。我需要将其更改为数组并进行尝试。@Rob yes。确实做到了