Implement conversations.create in C#

穿精又带淫゛_ 提交于 2019-12-11 09:53:05

问题


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!


回答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.




回答2:


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


来源:https://stackoverflow.com/questions/58525345/implement-conversations-create-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!