我正在用 C# 编写一个简单的控制台应用程序来与 Slack.com 进行通信。我正在通过他们的 WebApi 进行此操作。目前我知道如何发布消息(带有附件、彩色、链接、用户等)并将文件发送到服务器。
如果您以正常方式发送文件(键入文本框左侧的“上传文件”),该文件将显示在主对话窗口中。但是你怎么能通过 WebAPI 实现同样的事情呢?目前发送文件后,我可以在右侧面板中看到它,其中仅列出了所有文件。
还有第二个问题:是否可以更改消息中文本的颜色(附件中没有“行”)?
这是通过https://slack.com/api/files.upload发送文件后的响应
{
"ok": true,
"file": {
"id": "F04EX4***",
"created": 1429279966,
"timestamp": 1429279966,
"name": "Testing.txt",
"title": "Testing",
"mimetype": "text\/plain",
"filetype": "text",
"pretty_type": "Plain Text",
"user": "U*********",
"editable": true,
"size": 28,
"mode": "snippet",
"is_external": false,
"external_type": "",
"is_public": false,
"public_url_shared": false,
"url": "https:\/\/slack-files.com\/files-pub\/T*******\
/testing.txt",
"url_download": "https:\/\/slack-files.com\/files-pub\/T************\
/download\/testing.txt",
"url_private": "https:\/\/files.slack.com\/files-pri\/T*******\
/testing.txt",
"url_private_download": "https:\/\/files.slack.com\/files-pri\/T**********\
/download\/testing.txt",
"permalink": "https:\/\/******.slack.com\/files\/******\
/F0******\/testing.txt",
"permalink_public": "https:\/\/slack-files.com\/********",
"edit_link": "https:\/\/******.slack.com\/files\/****\/F******\/testing.txt\/edit",
"preview": "This is a test file number2.",
"preview_highlight": "<div class=\
"sssh-code\"><div class=\"sssh-line\"><pre>This is a test file number2.<\/pre><\/div>\n
<\/div>",
"lines": 1,
"lines_more": 0,
"channels": [],
"groups": [],
"ims": [],
"comments_count": 0
}
}
抱歉,我不知道如何很好地格式化它。
“is_external”和“is_public”都是假的,也许这就是原因 - 但我怎样才能将它们设置为“真”?
->> 感谢您的编辑!:) 这是我正在使用的整个功能:
public static void SlackSendFile()
{
FileStream str = File.OpenRead(@"C:\Users\Eru\Desktop\Testing.txt");
byte[] fBytes = new byte[str.Length];
str.Read(fBytes, 0, fBytes.Length);
str.Close();
var webClient = new WebClient();
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
var fileData = webClient.Encoding.GetString(fBytes);
var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, "Testing.txt", "multipart/form-data", fileData);
var nfile = webClient.Encoding.GetBytes(package);
string url = "https://slack.com/api/files.upload?token=" + Token + "&content=" + nfile + "&channels=[" + Channel + "]";
byte[] resp = webClient.UploadData(url, "POST", nfile);
var k = System.Text.Encoding.Default.GetString(resp);
Console.WriteLine(k);
Console.ReadKey();
}
EDIT1:在这一行:
byte[] resp = webClient.UploadData(url, "POST", nfile);
网址是:
https://slack.com/api/files.upload?token=*********&content=System.Byte[]&channels=[%23*****]
然后我传递字节数组。
编辑:
我已经解决了这个问题。问题是频道应该是频道的ID,而不是频道的名称......愚蠢的错误:(