soxtoby / SlackNet

A comprehensive Slack API client for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Message Metadata

williamb1024 opened this issue · comments

Would you consider adding support for Metadata within the Message and MessageEvent classes?

The metadata field is documented here:
https://api.slack.com/methods/chat.postMessage

I've added support in a clone, but I've used JToken as the event_payload type, which you may not want to do in your public API. Although I'm happy to create a pull request, if you'd like.

Sounds like a good idea to me - I'll try and add something in the next few days.

What do you think about it being a string, rather than a JToken? Given that it can be any arbitrary JSON, I'm not sure I want to make any assumptions about how it's serialized.

I don't believe a string would be a good choice, assuming your intent is to serialize the string as the content of the metadata property. The metadata value has a specific format (https://api.slack.com/metadata/using) along with a number of design guidelines (https://api.slack.com/reference/metadata).

The JToken has some issues though. During serialization the JSON Serializer Settings are applied to the JToken and its children. So far this has caused issues with DateTime serialization, as the postMessage call is forcing a date only format for date serialization. Any serialization attributes from the serialized class are lost once it put into a JToken instance, which limits the usefulness of this method. It does make the deserialization easier though, since it's easy to process the incoming metadata and store it until the code that knows the metadata type can actually deserialize it into an actual instance.

Really, I haven't come up with a clean method of defining the message's metadata that works well both when serializing for PostMessage and when deserializing as part of an event. I haven't spent much time looking at the deserialization side, because the JToken implementation is "mostly" working for me at the moment, but I think the solution may lie in using different objects for serialization and deserialization. Serialize the EventPayload as an object, deserialize it as a JToken and provide an extension method to deserialize that value to a specific instance type.

I won't have time to work on my slack project until this weekend. If I come up with anything better during that time, I'll let you know.

Thanks for the feedback, it really helped. I've added metadata support in the v0.11.3 release. I ended up with two properties when posting messages: MetadataJson, which has a JToken for the payload allowing maximum flexibility, and MetadataObject, that lets you specify any object you like, which will be serialized using the usual Slack conventions. When metadata comes back from Slack, it can be deserialized with a ToObject method, although a JToken payload is exposed as well, just in case.

Let me know if you run into any problems.