I am looking at Slack’s documentation on conversations.create and I’m not sure how to integrate it in C#. Do I need to import their Slack API solution into my code to use it? Any help would be great!
Asked
Active
Viewed 279 times
1
-
Hey, It looks like you are new here. Welcome to SO. You typically show what work you have done so far on a problem before posing a question. If you can google an answer then your question probably isn't off to a good start. I'm not going to downvote your question but, its all but guarnteed that others will or, that this question will be closed. https://github.com/Inumedia/SlackAPI – Terrance Oct 23 '19 at 14:43
-
Hi Terrance thank you! Yeah that’s true I did a lot of Googling and none of the sources has helped. Don’t get me wrong I would post code if I had code :) – Chris Oct 23 '19 at 14:45
-
Did you use the link? I just googled "Slack API C#" and this was at the top. You would just need to add the nuget package and reference the namespace. https://github.com/Inumedia/SlackAPI – Terrance Oct 23 '19 at 14:47
-
If any of the posted answers solved your question please consider marking it as solution by clicking on the checkmark. TY! – Erik Kalkoken Oct 23 '19 at 18:17
2 Answers
1
To create a channel with C# all you need to do is make a POST request to the respective API method. channels.create
will work, but I recommend the newer conversations.create
API method.
There are many way how to you can make a POST request in C#. Here is an example using HttpClient
, which is the preferred approach. Check out this post for alternatives.
Here is an example:
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace SlackExamples
{
class CreateChannels
{
private static readonly HttpClient client = new HttpClient();
static async Task CreateChannel()
{
var values = new Dictionary<string, string>
{
{ "token", Environment.GetEnvironmentVariable("SLACK_TOKEN") },
{ "name", "cool-guys" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("https://slack.com/api/conversations.create", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
static void Main(string[] args)
{
CreateChannel().Wait();
}
}
}
Note: The token you need it kept in an environment variable for security purposes, which is good practice.

Erik Kalkoken
- 30,467
- 8
- 79
- 114
0
you can use httpclient or restsharp (my personal favorite) to call slacks web api.
You'd be calling https://slack.com/api/conversations.create from your application, it's not like an sdk you download.
restsharp code:
var client = new RestClient("https://slack.com/api/chat.postMessage");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "7efd9a78-827d-4cbf-a80f-c7449b96d31f");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-type", "application/json");
request.AddHeader("Authorization", "Bearer xoxb-1234-56789abcdefghijklmnop");
request.AddParameter("undefined", "{\"channel\":\"C061EG9SL\",\"text\":\"I hope the tour went well, Mr. Wonka.\",\"attachments\": [{\"text\":\"Who wins the lifetime supply of chocolate?\",\"fallback\":\"You could be telling the computer exactly what it can do with a lifetime supply of chocolate.\",\"color\":\"#3AA3E3\",\"attachment_type\":\"default\",\"callback_id\":\"select_simple_1234\",\"actions\":[{\"name\":\"winners_list\",\"text\":\"Who should win?\",\"type\":\"select\",\"data_source\":\"users\"}]}]}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

terrencep
- 675
- 5
- 16
-
Using the Restsharp 3rd party library for sending a POST request is another viable approach. However your example is sending a message, not creating a channel as requested by OP. Also you don't need the postman or cache-control header. – Erik Kalkoken Oct 23 '19 at 14:53