.NET Core NuGet package that helps with Slack interactions Available at https://www.nuget.org/packages/Slack.NetStandard
using Slack.NetStandard.Auth;
var builder = new OAuthV2Builder("clientId")
{
State = "stateGoesHere",
BotScope = "channels:read"
};
var redirectUri = builder.BuildUri();
using Slack.NetStandard.Auth;
var token = await OAuthV2Builder.Exchange(code,clientId,clientSecret);
using Slack.NetStandard;
var verifier = new RequestVerifier(signingSecret);
var verified = verifier.Verify(request.Headers[RequestVerifier.SignatureHeaderName], long.Parse(request.Headers[RequestVerifier.TimestampHeaderName]), request.Body);
var command = new SlashCommand(payloadText);
var message = new InteractionMessage();
message.Blocks.Add(new Section{Text = new PlainText("Only title is required")});
message.Blocks.Add(new Divider());
message.Send(command.ResponseUrl);
await command.Respond(message);
// or - if it's not from a slash command, any response url can use
await command.Response(responseUrl);
var view = new View
{
Type = "modal",
Title = "Create New Story",
Close = "Cancel",
Submit = "Submit",
Blocks = new IMessageBlock[]
{
new Section{Text = new PlainText("Only title is required")}
}
};
var client = new SlackWebApiClient(accessToken);
var response = await client.View.Open(triggerId,view);
var request = new PostMessageRequest {Channel = "C123456"};
request.Blocks.Add(new Section{Text = new PlainText("Hi There!")});
var client = new SlackWebApiClient("token");
await client.Chat.Post(request);
using Slack.NetStandard.EventsApi;
using Slack.NetStandard.EventsApi.CallbackEvents;
var eventObject = JsonConvert.DeserializeObject<Event>(input.Body);
if (eventObject is EventCallback callback)
{
switch(callback.Event)
{
case AppHomeOpened appHome:
break;
case GroupClose groupClose:
break;
}
}
var entities = TextParser.FindEntities("<@W123456|Steven>");
if(entities.First() is UserMention mention)
{
var userId = mention.UserId //W123456
var label = mention.Label //Steven
}
if(msg.Contains("envelope_id")) //If there's no envelope ID it's a Hello or Disconnect object
{
var env = JsonConvert.DeserializeObject<Envelope>(msg);
switch(env.Payload) {
case SlashCommand command:
//logic here
break;
case EventCallback evt:
//logic here
break;
case InteractionPayload payload:
//logic here
break;
}
var ack = new Acknowledge{EnvelopeId=env.EnvelopeId} //All messages must be acknowledged within a few seconds
Send(ack);
}
For a .NET 3.1 client that helps with a lot of the Socket Mode plumbing, the SocketSample app is now available at Slack.NetStandard.AsyncEnumerable