我在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
}
}
抱歉,我不知道如何很好地格式化。
&#34; is_external&#34;和&#34; is_public&#34;这两个都是假的,也许这就是原因 - 但我怎样才能将它们设置为&#34; true&#34; ?
- &GT;&GT;谢谢你的编辑! :) 这是我使用的整个功能:
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,而不是频道的名称......愚蠢的错误:(
答案 0 :(得分:2)
如果在channel属性之后添加pretty = 1,它将更好。
示例:
string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + nfile + "&channels=[" + Channel + "]&pretty=1"
答案 1 :(得分:2)
这是一个使用RestSharp的简洁示例
public void UploadFile(string token, string filePath, string channel)
{
var client = new RestClient("https://slack.com");
var request = new RestRequest("api/files.upload", Method.POST);
request.AddParameter("token", token);
request.AddParameter("channels", channel);
var fileInfo = new FileInfo(filePath);
request.AddFile("file", File.ReadAllBytes(filePath), fileInfo.Name, contentType:"multipart/form-data");
//Execute the request
var response = client.Execute(request);
var content = response.Content;
}
答案 2 :(得分:1)
我不得不稍微改变你的代码,让它适合那些仍然在寻找它的人。我需要将文件数组转换为ASCII编码的字符串,然后将细节拉入参数。
public static void SlackSendFile(string token, string channelId, string filepath)
{
FileStream str = File.OpenRead(filepath);
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);
var encodedfile = System.Text.Encoding.ASCII.GetString(nfile);
string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + encodedfile + "&channels=" + channelId + "";
byte[] resp = webClient.UploadData(url, "POST", nfile);
var k = System.Text.Encoding.Default.GetString(resp);
Console.WriteLine(k);
}