123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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<Tuple<string, string>>();
- request.Add(new Tuple<string, string>("channel", channelId));
- request.Add(new Tuple<string, string>("user", userId));
- request.Add(new Tuple<string, string>("blocks", BuildBlocks()));
- var callback = new Action<PostMessageResponse>((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<dynamic>();
- blocks.Add(new
- {
- type = "section",
- text = new
- {
- type = "mrkdwn",
- text = "Section text"
- }
- });
- var actionElements = new List<dynamic>();
- 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;
- }
- }
- }
|