在 C# 中实现 conversations.create
Implement conversations.create in C#
我正在查看 Slack 关于 conversations.create 的文档,但我不确定如何将它集成到 C# 中。我需要将他们的 Slack API 解决方案导入到我的代码中才能使用吗?任何帮助都会很棒!
您可以使用 httpclient 或 restsharp(我个人最喜欢的)调用 slacks web api。
您会从您的应用程序调用 https://slack.com/api/conversations.create,它不像您下载的 SDK。
restsharp 代码:
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);
要使用 C# 创建频道,您需要做的就是向相应的 API 方法发出 POST 请求。 channels.create
可以,但我推荐更新的 conversations.create
API 方法。
在 C# 中可以通过多种方式发出 POST 请求。下面是一个使用 HttpClient
的示例,这是首选方法。查看 this post 的替代品。
这是一个例子:
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();
}
}
}
注意:为了安全起见,您需要的令牌保存在环境变量中,这是一种很好的做法。
我正在查看 Slack 关于 conversations.create 的文档,但我不确定如何将它集成到 C# 中。我需要将他们的 Slack API 解决方案导入到我的代码中才能使用吗?任何帮助都会很棒!
您可以使用 httpclient 或 restsharp(我个人最喜欢的)调用 slacks web api。
您会从您的应用程序调用 https://slack.com/api/conversations.create,它不像您下载的 SDK。
restsharp 代码:
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);
要使用 C# 创建频道,您需要做的就是向相应的 API 方法发出 POST 请求。 channels.create
可以,但我推荐更新的 conversations.create
API 方法。
在 C# 中可以通过多种方式发出 POST 请求。下面是一个使用 HttpClient
的示例,这是首选方法。查看 this post 的替代品。
这是一个例子:
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();
}
}
}
注意:为了安全起见,您需要的令牌保存在环境变量中,这是一种很好的做法。