stoiveyp / Slack.NetStandard

.NET Core package that helps with Slack interactions
MIT License
41 stars 16 forks source link

Request to add example of parsing socket envelope to an EventCallback #39

Closed freakalis closed 3 years ago

freakalis commented 3 years ago

Hi! Im trying to use the event api and socket mode sample as a base to build my own bot functionality. I'm kind of stuck on how to properly parse the envelope to an EventCallback such as for example an AppMention.

In the sample project the envelope response for example a AppMention gets parsed as an EventCallback and not a CallbackEvent with AppMention type. How to properly convert/pars the envelope to an AppMention reponse?

Would it be possible to add an example of that in the SocketSample project?

Thanks in advance!

stoiveyp commented 3 years ago

Hi @freakalis - can I confirm you’re using the latest version of the library? Any envelope with a type of ‘events_api’ should be cast to CallbackEvent as of 2.9.1 (small issue with the Slack docs meant events were incorrectly deserialised in 2.9.0)

I will look at adding an example though, so either way let’s keep the issue open until that’s done too 👍

freakalis commented 3 years ago

Hi @stoiveyp! Yes tried using 2.9.1 and also checked out latest master version of the repo locally for debugging purposes.

The slashCommand works as excepted in the SocketSample and var env = JsonConvert.DeserializeObject<Envelope>(msg); return as env SlashCommand.

But my AppMention only get returned as an EventCallback object with type "event_callback". Should I parse the envelope in some extra way to extract the AppMention data or is JsonConvert.DeserializeObject<Envelope>( ) supposed to handle the conversion all the way from Envelope with a Payload of CallbackEvents.AppMention?

stoiveyp commented 3 years ago

Okay. The behavior you're describing is the 2.9.0 behavior, which is what's confusing me.

As of v2.9.1 the envelope should handle the whole thing - as shown in the unit test here with AppHomeOpened: https://github.com/stoiveyp/Slack.NetStandard/blob/master/Slack.NetStandard.Tests/SocketTests.cs#L45

That unit test was built from running the SocketSample console app that is in the repo - the converter in 2.9.1 doesn't use EventCallback any more.

hmm...the envelope converter doesn't use EventCallback any more. Here's the code it uses for events_api envelopes, it treates everything as ICallbackEvent

https://github.com/stoiveyp/Slack.NetStandard/blob/master/Slack.NetStandard/JsonConverters/EnvelopeConverter.cs#L104

Even if it's a type we don't understand you'd end up with a generic CallbackEvent object rather than EventCallback

If you're sure your application is loading 2.9.1 then can you paste an example event you're receiving so I can run it against my tests.

stoiveyp commented 3 years ago

FYI - I've added a socket mode example to the bottom of the readme, with a simplified version of the code I'm currently using in my own WebSocket app. Hopefully we can figure out what's happening with your code so it runs in a similar way 🤞

freakalis commented 3 years ago

Thanks for the quick reply! At the moment I am using a locally checked out version of the repo running master with latest changes pulled. When i debug I can see that i gets to https://github.com/stoiveyp/Slack.NetStandard/blob/master/Slack.NetStandard/JsonConverters/EnvelopeConverter.cs#L104

I think the problem for me is that in CallbackEventConverter.

`jObject.Value("type")`` returns "event_callback" but I think it should return "app_mention".

Here is a .ToString() of the jObject in the EventConverter with some date scrubbed.

{{
  "token": "XXXX",
  "team_id": "XX",
  "api_app_id": "XX",
  "event": {
    "client_msg_id": "5b211338-6c73-47b3-a27e-f430ab88369a",
    "type": "app_mention",
    "text": "<@XXX> test3",
    "user": "XXX",
    "ts": "1611741508.001500",
    "team": "XX",
    "blocks": [
      {
        "type": "rich_text",
        "block_id": "0230",
        "elements": [
          {
            "type": "rich_text_section",
            "elements": [
              {
                "type": "user",
                "user_id": "U01K93F6CGK"
              },
              {
                "type": "text",
                "text": " test3"
              }
            ]
          }
        ]
      }
    ],
    "channel": "XX",
    "event_ts": "1611741508.001500"
  },
  "type": "event_callback",
  "event_id": "Ev01KTP0K222",
  "event_time": 1611741508,
  "authorizations": [
    {
      "enterprise_id": null,
      "team_id": "XX",
      "user_id": "XX",
      "is_bot": true,
      "is_enterprise_install": false
    }
  ],
  "is_ext_shared_channel": false,
  "event_context": "1-app_mention-XX-XX"
}}
freakalis commented 3 years ago

FYI - I've added a socket mode example to the bottom of the readme, with a simplified version of the code I'm currently using in my own WebSocket app. Hopefully we can figure out what's happening with your code so it runs in a similar way 🤞

I think there is a small typo where msg.Payload should be env.Payload in the switch

stoiveyp commented 3 years ago

@freakalis - Very confused now. What you're showing me IS an event callback. That's not a socket mode envelope.

Is socket mode enabled in your app? i.e. you're using a WebSocket to get the message not an HTTPS request?

Fixed the typo in the readme too - thanks for that (teach me to try and cut bits of my code out!)

freakalis commented 3 years ago

@freakalis - Very confused now. What you're showing me IS an event callback. That's not a socket mode envelope.

Is socket mode enabled in your app? i.e. you're using a WebSocket to get the message not an HTTPS request?

I think I'm using the socket mode :D My Slack App has socket mode enabled. I created my own console project and copied the code from https://github.com/stoiveyp/Slack.NetStandard/blob/23f919d2b6254afa62431c9f164e604683612a0e/SocketSample/Program.cs as my starting point. Then try to add my own logic in the ProcessMessages() function.

stoiveyp commented 3 years ago

😁 okay, that all sounds good - the problem is that your sample JObject doesn't track. According to the docs and my own testing we should be getting back something like this:

https://api.slack.com/apis/connections/socket-implement#home

So at least a payload and an envelope_id property so there's something to acknowledge. I'll wire up the app mention event in my sample and see if it's the events themselves that are causing a different schema

stoiveyp commented 3 years ago

@freakalis you are absolutely right - it appears that sometime since I released v2.9.1 Slack have entirely changed the events_api payload to an EventCallback 🤦‍♂️

I'll make the necessary changes now and get v2.9.2 out the door ASAP - thank you for sticking with me on this one!

stoiveyp commented 3 years ago

v2.9.2 has now been released and should be working as expected 🤞

freakalis commented 3 years ago

Hurray 🥳 now it works for me. Thanks for the help!

stoiveyp commented 3 years ago

I've updated the readme example to show EventCallback is now the type to look out for, and I've contacted the Slack API team to ensure it doesn't change back at some point.

Closing this issue but thanks again!