我已经创建了一个反馈文本框,人们可以使用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)"
不知道还能做什么
这是我的代码
public partial class MainWindow : Window, INotifyPropertyChanged
{
private byte[] attachFileBytes;
private string attachFileName;
........more code......
private void AttachFileButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
attachFileName = openFileDialog.FileName; // Use 'FileName' instead of 'FileNames'
attachFileBytes = File.ReadAllBytes(attachFileName);
}
}
private async void SendFeedback_Click(object sender, RoutedEventArgs e)
{
string feedback = txtFeedback.Text;
if (string.IsNullOrEmpty(feedback))
{
MessageBox.Show("Please enter your feedback before sending.");
return;
}
string username = Environment.UserName;
// Get the current date and time
string dateTime = DateTime.Now.ToString();
string message = $"Feedback: {feedback}\nUsername: {username}\nDate and Time: {dateTime}";
string slackWebhookUrl = "MY WEBHOOK URL";
// Create the payload to send to Slack
string payload = "{\"text\": \"" + message + "\"}";
using (var client = new HttpClient())
{
await client.PostAsync(slackWebhookUrl, new StringContent(payload));
}
if (attachFileBytes != null)
{
// Create the payload for the file attachment
MultipartFormDataContent fileContent = new MultipartFormDataContent();
fileContent.Add(new ByteArrayContent(attachFileBytes), "file", attachFileName);
Console.WriteLine("File attachment payload created.");
string slackApiToken = "API BOT TOKEN";
// Upload the file to Slack using the Slack API
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", slackApiToken);
var response = await client.PostAsync("https://slack.com/api/files.upload", fileContent);
if (response.IsSuccessStatusCode)
{
string result = await response.Content.ReadAsStringAsync();
Console.WriteLine($"File uploaded successfully. Response: {result}");
}
else
{
string errorResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Failed to upload file. Error Response: {errorResponse}");
}
}
}
MessageBox.Show("Feedback sent successfully!");
txtFeedback.Text = "";
attachFileBytes = null;
attachFileName = null;
feedbackPanel.Visibility = Visibility.Collapsed;
feedbackbutton.Visibility = Visibility.Collapsed;
// Show the "Open Feedback Box" button
openFeedbackButton.Visibility = Visibility.Visible;
}
private void OpenFeedbackButton_Click(object sender, RoutedEventArgs e)
{
// Toggle visibility of feedback elements
if (feedbackPanel.Visibility == Visibility.Collapsed)
{
feedbackPanel.Visibility = Visibility.Visible;
feedbackbutton.Visibility = Visibility.Visible;
openFeedbackButton.Visibility = Visibility.Collapsed;
}
else
{
feedbackPanel.Visibility = Visibility.Collapsed;
feedbackbutton.Visibility = Visibility.Collapsed;
openFeedbackButton.Visibility = Visibility.Visible;
}
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<!-- First row for the TextBlock -->
<RowDefinition Height="*"/>
<!-- Second row for the TextBox -->
</Grid.RowDefinitions>
<StackPanel x:Name="feedbackPanel" Margin="20,300,0,0" Visibility="Collapsed">
<TextBlock Text="Feedback:" FontSize="16" FontWeight="Bold" HorizontalAlignment="Right" Margin="0,0,270,0" />
<Grid>
<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" />
<Button x:Name="upload" Content="Attach File" Margin="520,170,0,0" Width="100" Height="20" Click="AttachFileButton_Click" />
</Grid>
<Button x:Name="feedbackbutton" Content="Send Feedback" Click="SendFeedback_Click" Margin="0,10,185,0" HorizontalAlignment="Right" Height="50" Width="250" Cursor="Hand" />
</StackPanel>
<Button x:Name="openFeedbackButton" Content="Open Feedback Box" Click="OpenFeedbackButton_Click" Margin="20,530,185,0" Cursor="Hand" Width="250" Height="50" HorizontalAlignment="Right"/>
</Grid>
任何帮助将不胜感激
1条答案
按热度按时间cuxqih211#
我解决了这个问题,在payload中我写了“channel”,在fileContent中我写了“channels”(带“s”),通过改变payload来匹配fileContent解决了这个问题
string payload =“{“text”:“”+ message +“",“channels”:“”+ channelId +“"}";
根据文档,它需要具体的“渠道”