发布消息松弛

时间:2018-06-12 20:53:06

标签: c# slack slack-api

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

"频道":" XXXXX","令牌":"令牌","文字":&# 34;文本"

我在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页面中选择的频道,我再次通过Add to Slack按钮。

1 个答案:

答案 0 :(得分:0)

我想问题出在您的webhook url上,您正在向其中添加更多内容。此代码肯定有效:

 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;
    }