codywb / slack-pyblock-builder

A lightweight Python library with a declarative syntax for simplifying building apps using Slack's Block Kit UI framework
BSD 3-Clause "New" or "Revised" License
6 stars 0 forks source link

Possibility to transform to dict #1

Open marverix opened 2 weeks ago

marverix commented 2 weeks ago

Hi,

first of thank you for such nice lib! I really don't understand why this is not included in slack_bolt package. I hope that one day it will be :)

I'm using slack_bolt to create Bot that reacts on some commands. slack_bolt don't understand example Section instance, and throwing exceptions. It would be super nice if there would be possibility to transform any object from this library to dict. For example Section(...).to_dict(), or even Message(...).to_dict().

What do you think?

Br, Marek

codywb commented 2 weeks ago

Marek,

Thanks, man! I saw your star the other day and it really made my day. I also don't understand why Slack didn't include something like this in their official library. I wrote this library while working on my own bot/app for work in Python and it's essentially the result of me learning and improving as a developer and programmer over time. It's not perfect, but it's a lot better than doing it with just vanilla slack_bolt, for sure!

I am planning to re-do the library at some point using Pydantic to make this very thing much easier. For now, however, I wonder if you couldn't get away with simply accessing the dictionary of the Section object using its block attribute?

For example:

section = Section().set_text("Hello World!")
print(section.block)

should return:

{'type': 'section', 'text': {'type': 'mrkdwn', 'text': 'Hello World!'}}

In other words, a dictionary representation of the Section object's blocks.

FYI, for Element and Object classes you can do the same with the .json attribute!