using Microsoft.AspNetCore.Http; using SlackAPI; using System.Text.Json; using System; using sera_slackbot.Library; using System.Collections.Generic; using SlackAPI.RPCMessages; namespace sera_slackbot.Commands { public class TestBotCommand : IBotCommand { private JsonFileSaver saver; private string channelId; private string userId; private string text; private string message; public TestBotCommand(JsonFileReader reader, JsonFileSaver saver) { } public void ExtractValidData(JsonElement slackEvent) { this.text = slackEvent.GetProperty("text").ToString(); this.channelId = slackEvent.GetProperty("channel").ToString(); this.userId = slackEvent.GetProperty("user").ToString(); } public bool WillTrigger(JsonElement slackEvent) { var text = slackEvent.GetProperty("text").ToString(); return text.StartsWith("test", StringComparison.InvariantCultureIgnoreCase); } public dynamic Run(SlackClient client) { var request = new List>(); request.Add(new Tuple("channel", channelId)); request.Add(new Tuple("user", userId)); request.Add(new Tuple("blocks", BuildBlocks())); var callback = new Action((response) => { Console.WriteLine("Direct API call {0}", response.error); }); Console.WriteLine(JsonSerializer.Serialize(request.ToArray(), new JsonSerializerOptions { WriteIndented = true })); client.APIRequestWithToken(callback, request.ToArray()); return new { code = StatusCodes.Status200OK, body = "" }; } private string BuildBlocks() { var blocks = new List(); blocks.Add(new { type = "section", text = new { type = "mrkdwn", text = "Section text" } }); var actionElements = new List(); actionElements.Add(new { type = "button", text = new { type = "plain_text", text = "Yes", emoji = false }, value = "yes" }); actionElements.Add(new { type = "button", text = new { type = "plain_text", text = "No", emoji = false }, value = "no" }); blocks.Add(new { type = "actions", elements = actionElements.ToArray() }); var jsonText = JsonSerializer.Serialize(blocks); return jsonText; } } }