XAML 上传文件到slack with files.上传方法

9w11ddsr  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(137)

我已经创建了一个反馈文本框,人们可以使用slack Webhook URL将反馈发送到我的个人slack频道,现在我试图添加一个文件上传按钮,以便用户可以使用Slack API令牌上传图像/文件,然而,当我点击发送消息时,消息正在成功发送,但没有文件上传到我的频道。
我添加了一个调试,实际上我在控制台中收到了一个成功的响应
"已创建文件附件负载。文件上传成功。回复:{"ok ":true," file ":{" id":"F05MA6S1RPH ","已创建":1692031941,“时间戳”:1692031941,“姓名”:" me-logo3.png","title":"me-logo3 "," mimetype ":" image/png","filetype":"png "," pretty_type ":" PNG","user":"U05KZR8E40N "," user_team ":" T05KRT9Q44X","可编辑":false,"size":25673,"mode":"hosted "," is_external ":false," external_type ":"," is_public ":false," public_url_shared":false," display_as_bot":false," username":""," url_private":www.example.com…….”
“files:write”作用域在下面启用
"OAuth权限(& P)"
不知道还能做什么
这是我的代码

  1. public partial class MainWindow : Window, INotifyPropertyChanged
  2. {
  3. private byte[] attachFileBytes;
  4. private string attachFileName;
  5. ........more code......
  6. private void AttachFileButton_Click(object sender, RoutedEventArgs e)
  7. {
  8. OpenFileDialog openFileDialog = new OpenFileDialog();
  9. if (openFileDialog.ShowDialog() == true)
  10. {
  11. attachFileName = openFileDialog.FileName; // Use 'FileName' instead of 'FileNames'
  12. attachFileBytes = File.ReadAllBytes(attachFileName);
  13. }
  14. }
  15. private async void SendFeedback_Click(object sender, RoutedEventArgs e)
  16. {
  17. string feedback = txtFeedback.Text;
  18. if (string.IsNullOrEmpty(feedback))
  19. {
  20. MessageBox.Show("Please enter your feedback before sending.");
  21. return;
  22. }
  23. string username = Environment.UserName;
  24. // Get the current date and time
  25. string dateTime = DateTime.Now.ToString();
  26. string message = $"Feedback: {feedback}\nUsername: {username}\nDate and Time: {dateTime}";
  27. string slackWebhookUrl = "MY WEBHOOK URL";
  28. // Create the payload to send to Slack
  29. string payload = "{\"text\": \"" + message + "\"}";
  30. using (var client = new HttpClient())
  31. {
  32. await client.PostAsync(slackWebhookUrl, new StringContent(payload));
  33. }
  34. if (attachFileBytes != null)
  35. {
  36. // Create the payload for the file attachment
  37. MultipartFormDataContent fileContent = new MultipartFormDataContent();
  38. fileContent.Add(new ByteArrayContent(attachFileBytes), "file", attachFileName);
  39. Console.WriteLine("File attachment payload created.");
  40. string slackApiToken = "API BOT TOKEN";
  41. // Upload the file to Slack using the Slack API
  42. using (var client = new HttpClient())
  43. {
  44. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", slackApiToken);
  45. var response = await client.PostAsync("https://slack.com/api/files.upload", fileContent);
  46. if (response.IsSuccessStatusCode)
  47. {
  48. string result = await response.Content.ReadAsStringAsync();
  49. Console.WriteLine($"File uploaded successfully. Response: {result}");
  50. }
  51. else
  52. {
  53. string errorResponse = await response.Content.ReadAsStringAsync();
  54. Console.WriteLine($"Failed to upload file. Error Response: {errorResponse}");
  55. }
  56. }
  57. }
  58. MessageBox.Show("Feedback sent successfully!");
  59. txtFeedback.Text = "";
  60. attachFileBytes = null;
  61. attachFileName = null;
  62. feedbackPanel.Visibility = Visibility.Collapsed;
  63. feedbackbutton.Visibility = Visibility.Collapsed;
  64. // Show the "Open Feedback Box" button
  65. openFeedbackButton.Visibility = Visibility.Visible;
  66. }
  67. private void OpenFeedbackButton_Click(object sender, RoutedEventArgs e)
  68. {
  69. // Toggle visibility of feedback elements
  70. if (feedbackPanel.Visibility == Visibility.Collapsed)
  71. {
  72. feedbackPanel.Visibility = Visibility.Visible;
  73. feedbackbutton.Visibility = Visibility.Visible;
  74. openFeedbackButton.Visibility = Visibility.Collapsed;
  75. }
  76. else
  77. {
  78. feedbackPanel.Visibility = Visibility.Collapsed;
  79. feedbackbutton.Visibility = Visibility.Collapsed;
  80. openFeedbackButton.Visibility = Visibility.Visible;
  81. }
  82. }
  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="Auto"/>
  4. <!-- First row for the TextBlock -->
  5. <RowDefinition Height="*"/>
  6. <!-- Second row for the TextBox -->
  7. </Grid.RowDefinitions>
  8. <StackPanel x:Name="feedbackPanel" Margin="20,300,0,0" Visibility="Collapsed">
  9. <TextBlock Text="Feedback:" FontSize="16" FontWeight="Bold" HorizontalAlignment="Right" Margin="0,0,270,0" />
  10. <Grid>
  11. <TextBox x:Name="txtFeedback" Height="200" TextWrapping="Wrap" AcceptsReturn="True" HorizontalAlignment="Right" Margin="10,0,185,0" Width="250" Grid.Row="1" VerticalContentAlignment="Top" Padding="10" SpellCheck.IsEnabled="True" Language="en-US" />
  12. <Button x:Name="upload" Content="Attach File" Margin="520,170,0,0" Width="100" Height="20" Click="AttachFileButton_Click" />
  13. </Grid>
  14. <Button x:Name="feedbackbutton" Content="Send Feedback" Click="SendFeedback_Click" Margin="0,10,185,0" HorizontalAlignment="Right" Height="50" Width="250" Cursor="Hand" />
  15. </StackPanel>
  16. <Button x:Name="openFeedbackButton" Content="Open Feedback Box" Click="OpenFeedbackButton_Click" Margin="20,530,185,0" Cursor="Hand" Width="250" Height="50" HorizontalAlignment="Right"/>
  17. </Grid>

任何帮助将不胜感激

cuxqih21

cuxqih211#

我解决了这个问题,在payload中我写了“channel”,在fileContent中我写了“channels”(带“s”),通过改变payload来匹配fileContent解决了这个问题
string payload =“{“text”:“”+ message +“",“channels”:“”+ channelId +“"}";

  1. using (var client = new HttpClient())
  2. {
  3. // Send the message to Slack
  4. await client.PostAsync(slackWebhookUrl, new StringContent(payload));
  5. if (attachFileBytes != null)
  6. {
  7. // Create the payload for the file attachment
  8. MultipartFormDataContent fileContent = new MultipartFormDataContent();
  9. fileContent.Add(new ByteArrayContent(attachFileBytes), "file", attachFileName);
  10. fileContent.Add(new StringContent(channelId), "channels");

根据文档,它需要具体的“渠道”

相关问题