user9731727
user9731727

Reputation:

Post a message to slack

I want to post a message to slack on x channel I need to send the following x parameters how do I send the following parameters to a website

"channel": "XXXXX", "token": "token", "text": "text"

I am coding in c# mvc application.

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

This is not working.It just adds an integration to the channel that I select in the OAuth page which again I got through Add to Slack button.

Upvotes: 0

Views: 1989

Answers (1)

Navideh
Navideh

Reputation: 11

I suppose the problem is with your webhook url which you are adding some more stuff to that.this code definitely work:

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

Upvotes: 1

Related Questions