Closed ramnes closed 2 years ago
Hey there! How serendipitous that you ask about this! I just got back to Slack Machine yesterday after a hiatus of a couple of months. I started experimenting with async yesterday. My plan is not only to support async, but to make Slack Machine async-only.
My initial idea was to use Bolt, but I think it's overkill for the low-level access I need. Slack SDK for Python actually also supports SocketMode and it can work with the Events API just as well.
I also debated for a while if Bolt was making Slack Machine obsolete, but from what I've seen, Bolt doesn't really let you build Slack bots in a scalable way, using a plugin-based architecture (see this issue for the problems with Bolt). This is what Slack Machine does well. So if I can implement async support with SocketMode and the Event API, I think it could really help adoption of Slack Machine
Anyways, thanks for your interest! I hope to build a first async version this weekend and release it as an alpha
Haha, glad to see that the universe is properly aligned today!
I definitely understand your feel that it's overkill for what you're currently building, but basing your work on top of it would let you profit from Slack engineering effort. It seems like they really want to make this the standard for developing bots, so you presumably would be able to use their newer client features easily in the future. And right now you wouldn't have to reimplement what they already implement, making the surface of Slack Machine smaller, which is great for you as a maintainer, and for contributors as well.
Let's digress a bit: I'm currently using Bolt to build a Slack bot but it's too low-level for my tastes. What I personally dream of (on top of the async support) is a rule matching system based on type hints, à la FastAPI. I quite liked what you did so far with named groups and I think that's a good base. But instead of having a regexp for matching, and then having to cast variables manually in the function, I would love to use a simpler notation in the decorator, type hints in the function signature, and then receive parameters that are correctly typed already.
That's worth another issue for sure, but my point is: this is the kind of feature that would really give value to Slack Machine, and that will most likely never exist in Bolt... not reimplementing Bolt a bit differently. Also, your current plugin-based architecture and Bolt don't feel mutually exclusive to me: the base class could hold the app instance at the class level. Plugins would then register their methods when instantiating. There are probably other ways too!
I think the tradeoff is all about this, actually: if you think that it would take you more time adapting your work on top of Bolt rather than implementing the newer Slack client features, then you should definitely do that, but don't forget to consider the future maintenance. From my experience, it's always harder in the long run to keep up-to-date a codebase that reimplements things than one that's based on top of other people work. :)
Anyway, sorry for the long answer, and whatever you end up doing, I'm eager to try the asynchronous machine!
PS – Without any pretension, if you need help / recommandations on how to switch to async, I've made the transition on several projects and have a few ones that support both sync and async, so I'd be happy to answer any question!
Thanks for your elaborate answer! Very insightful!
With regards to the underlying base of Slack Machine, there are basically 2 options:
I don't think the latter will receive less engineering effort from the Slack team than the former, because Bolt is actually built on top of the Python Slack SDK. The Python Slack SDK is just more low-level than Bolt. And what Bolt adds on top of the Python Slack SDK, is mostly convenience in a similar way as Slack Machine does. E.g. decorators for writing handlers for certain events.
But Slack Machine augments that with a true plugin system, that should make it easy to distribute functionality separate from your bot instance and reuse it between bots.
When I look at the code examples between Bolt and the Python Slack SDK, for my purposes there are not so many differences when it comes to using Socket Mode (which is what I'm focussing on right now).
So unless I'm missing something, there is no convincing reason to use Bolt instead of the Python Slack SDK, given that the latter will be maintained because it's used in the former?
With regards to the rule matching: when you talk about having typed arguments in your function and casting input automatically, how far would that go? What I mean is: FastAPI lets you define nested models (using Pydantic), which makes sense for an API with JSON input. For a Slack bot it's much simpeler, because you're always dealing with unstructured text (Slack messages) so the only casting you could do, is to int
, bool
, str
and maybe date
/datetime
? Or am I missing something?
And you'd still need regex I think, because the text is unstructured?
Can you given an example of what a listener function would look like and what input it would respond to?
Btw, I have a Gitter room setup (see README) to discuss things in chat, but it's not really used right now. We could use it to have more real-time in-depth discussions at some point. I also contemplated moving to Slack for the community.
I don't think the latter will receive less engineering effort from the Slack team than the former, because Bolt is actually built on top of the Python Slack SDK. The Python Slack SDK is just more low-level than Bolt.
So unless I'm missing something, there is no convincing reason to use Bolt instead of the Python Slack SDK, given that the latter will be maintained because it's used in the former?
You're completely right. I was under the impression than Bolt was implementing most things async and events-related, but digging a bit more I'm discovering now that I completely overlooked the SDK, and that it's far more complete than I thought it was. Sorry for the noise!
With regards to the rule matching: when you talk about having typed arguments in your function and casting input automatically, how far would that go? What I mean is: FastAPI lets you define nested models (using Pydantic), which makes sense for an API with JSON input. For a Slack bot it's much simpeler, because you're always dealing with unstructured text (Slack messages) so the only casting you could do, is to int, bool, str and maybe date/datetime? Or am I missing something?
I was referring to the path parameters. :)
You can cast any path parameter to an int, a float, a Decimal, an UUID, a BSON ObjectId... or anything that can be casted from a string.
An alternative is Flask / Starlette "convertors", that work with regular expressions underneath, but they don't rely on type hints and thus feel a bit less modern.
So, I made quite some progress on the async story! You can find my work in this draft PR. It's still WIP, but the basics work now:
TODO:
@respond_to
and @listen_to
decorators work in plugins. @process
is next on my todo list and should be really easy.The work on the branch can already be tested. I've added a script in the root called run_dev_async.py
which runs the async version of Slack Machine. The code for sync + async currently live side by side, with the async code living in machine_v2
. I want to finish this and start testing it rigorously. If it works, and some users (like you?) have tested it as well, I actually want to remove the sync code, because I don't plan on maintaining 2 versions. People can always fall back to older versions if they want to.
If you want to test it, you'll have to create your own Slack app. You can use the following App Manifest to get started quickly:
Update @process
now works as well!
Async code has been moved to the machine.asyncio
package
Update: RBAC now works! And all of the async code is type checked with MyPy
This has been fixed now that #592 has been merged. Published as v0.26.0
Hey there!
Do you have any plan for supporting async code?
I've seen that you want to use the Events API (#469), so maybe bolt-python and its async backend could be used as a base?