发布消息松弛

我想在x通道上发布消息松弛我需要发送以下x参数如何将以下参数发送到网站

“ Channels ”:“XXXXX”,“令牌”:“令牌”,“文字”:“文字”

我在c#mvc应用程序中编码 .

public async Task<HttpResponseMessage> SendMessageAsync(string message, string channel = null, string userName = null)
{
    using (var client = new HttpClient())
    {
        string url = "https://fake2604.slack.com/api/chat.postMessage?token=myToken&channel=" + channel + "&text=Hello World";

        var payLoad = new
        {
            text = message,
                   channel,
                   userName,
        };

        var serializedPayload = JsonConvert.SerializeObject(payLoad);

        var response = await client.PostAsync(url, new StringContent(serializedPayload, Encoding.UTF8, "application/json"));
        return response;
    }
}

这不起作用 . 它只是添加了我在OAuth页面中选择的 Channels 的集成,我再次通过Add to Slack按钮 .

回答(1)

2 years ago

我想问题是你的webhook网址,你正在添加更多的东西 . 这个代码肯定工作:

public async Task<HttpResponseMessage> SendMessageAsync(string message)
    {
        var payload = new
        {
            text = message,
            channel="x",
            userName="y",
        };
        HttpClient httpClient = new HttpClient();
        var serializedPayload = serializer.ToJson(payload);
        var response = await httpClient.PostAsync("url",
            new StringContent(serializedPayload, Encoding.UTF8, "application/json"));
        return response;
    }