prjseal / SlackBotMessages

A .NET library to enable you to send bot messages in slack
MIT License
55 stars 19 forks source link

Added ability to set response type #13

Closed ajtatum closed 4 years ago

ajtatum commented 4 years ago

Without .NET Standard 2.0 there really isn't a nice way to get a Display/Description attribute. So, here's an easy implementation without any further changes.

The response type defaults to ephemeral.

ajtatum commented 4 years ago

I made my changes in VS 2019. I believe you'll have to change your configuration in AppVeyor or make changes to the code I submitted.

prjseal commented 4 years ago

Excellent. Thank you for this. I’ll try it out later

prjseal commented 4 years ago

I've tried testing it like this but I don't see any response in the channel. Any ideas what I need to enter in the text to test it?

        [Test]
        public void Emoji_Icon_Example()
        {
            var client = new SbmClient(WebHookUrl);

            var message = new Message
            {
                Username = "Alien",
                Text = "/who",
                IconEmoji = Emoji.Alien
            };

            message.SetResponseType(ResponseType.in_channel);

            var response = client.Send(message);
            Assert.AreEqual("ok", response.Result);
        }
ajtatum commented 4 years ago

I'm not sure, I tested it with the following two tests and they both showed up. Note that using it in the tests, the result will be the same. However, the response is different in a live environment when you have a Slack Bot with Slash Commands. Here are my tests:

/// <summary>
///     This example shows you how you can send response via in_channel
/// </summary>
[Test]
public void Response_In_Channel()
{
    var client = new SbmClient(WebHookUrl);

    var message = new Message("This is an in_channel response type.");

    message.SetResponseType(ResponseType.in_channel);
    var response = client.Send(message);
    Assert.AreEqual("ok", response.Result);
}

/// <summary>
///     This example shows you how you can send response via ephemeral
/// </summary>
[Test]
public void Response_In_Ephemeral()
{
    var client = new SbmClient(WebHookUrl);

    var message = new Message("This is an ephemeral response type.");

    message.SetResponseType(ResponseType.ephemeral);
    var response = client.Send(message);
    Assert.AreEqual("ok", response.Result);
}

I've attached two files from the bot I'm working on (https://github.com/ajtatum/StanLeeBot).

This is an "in_channel" response (Here's an usage example) Marvel-Ironman

This is an "ephemeral" response. (Here's an usage example) StanLee-Help

So, the tests I wrote above show the messages, but they'll both appear as though they're in_channel because of the context, but they do appear when using an Incoming WebHook from Slack.

Prior to making this change, all my messages sent through my bot appeared as ephemeral, meaning that nobody else in the channel was able to see the response. Once I added in the ability to set the response type, as shown in my examples, SlackBotMessages sends the message how you want it to appear.

ajtatum commented 4 years ago

I went ahead and added the four unit tests in there (two for BasicTests and two for BasicTestsAsync).

prjseal commented 4 years ago

Makes sense to me, thanks for this. I'll merge it now and do a release this evening.

prjseal commented 4 years ago

Version 2.1.0 released. This includes the response type and i've upgraded the project to .net standard to pave the way for future development if needed. I'm happy with this response type how it is though, so no need to go back and use description attributes.

prjseal commented 4 years ago

Just waiting for NuGet to publish it