-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass non-slack arguments when using handlers #559
Comments
Hi @tom0010, thanks for asking the question! As you've already noticed, there is no way to add custom arguments to listener functions. The recommended way is to add your additional values to the https://slack.dev/bolt-python/concepts#global-middleware @app.middleware
def set_db_object(context, body, next):
context["db"] = acquire_database_session(body)
next() Hope this helps. |
Hey @seratch thanks for the info and the quick response! app.py: from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack_bolt.app import App
from handlers.message import Message
from flask import Flask
app = Flask(__name__)
BOT_TOKEN = os.environ["SLACK_TOKEN"]
APP_TOKEN = os.environ["SLACK_APP_TOKEN"]
SIGNING_SECRET = os.environ["SLACK_SIGNING_SECRET"]
bolt = App(token=BOT_TOKEN, signing_secret=SIGNING_SECRET)
Message().add_to(bolt)
SocketModeHandler(bolt, APP_TOKEN).connect()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=10325, threaded=True) handlers/message.py: from handlers.messages import assign
class Message():
def add_to(self, app):
app.message(re.compile(r"!assign to <@(\S+)>"))(assign.assign_ticket) handlers/messages/assign.py def assign_ticket(message, say, logger):
logger.info(f"{message['text']} by {message['user']}")
user = message["text"][24:-1:]
text = "my message"
return say(f"{user} you have been assigned {text}") Tree: .
├── handlers
│ ├── __init__.py
│ ├── message.py
│ ├── messages
│ │ ├── __init__.py
│ │ ├── assign.py
└── app.py |
@tom0010 You can register global middleware in a similar way: bolt = App(token=BOT_TOKEN, signing_secret=SIGNING_SECRET)
class Middleware():
def _set_db_object(self, context, body, next):
context["db"] = acquire_database_session(body)
next()
def add_to(self, app):
app.middleware(self._set_db_object)
Middleware().add_to(bolt)
Message().add_to(bolt) Regarding the original question here, everything is clear now. Would you mind closing this issue? |
@seratch thanks, even though this works, it's not really something I was looking for. I was hoping that I could pass the db into the class which would be a new file. It doesn't seem possible? It would mean I would have to have that Middleware class inside the app.py: bolt = App(token=BOT_TOKEN, signing_secret=SIGNING_SECRET)
something = "1234345345"
Middleware().add_to(bolt, something)
Message().add_to(bolt) handlers/middleware.py: class Middleware():
def _set_db_object(self, context, body, next, something):
context["db"] = something
next()
def add_to(self, app, something):
app.middleware(self._set_db_object(something)) This gives me this error: Traceback (most recent call last):
File "/Users/tom/project/./app.py", line 35, in <module>
Middleware().add_to(bolt, something)
File "/Users/tom/project/handlers/middleware.py", line 8, in add_to
app.middleware(self._set_db_object(something))
TypeError: _set_db_object() missing 3 required positional arguments: 'body', 'next', and 'something' But this turns out that it is pretty much the same as before when I tried to pass arguments in my previous post. |
@tom0010 As you saw, you cannot add the class Middleware():
def _set_db_object(self, context, body, next):
context["db"] = self.something
next()
def add_to(self, app, something):
self.something = something
app.middleware(self._set_db_object)
Middleware().add_to(bolt, init_something()) I would recommend using constructor argument for class Middleware():
def __init__(self, something):
self.something = something
def _set_db_object(self, context, body, next):
context["db"] = self.something
next()
def add_to(self, app):
app.middleware(self._set_db_object)
Middleware(init_something()).add_to(bolt) Either is fine. |
Oh yes, using |
Looking at #542
I'd like to do something similar, I'd like to pass extra args into my function.
bolt already gives you
body
,ack
andlogger
as some examples, I'd like to also pass my own ones.However, as this is done automatically, I can't pass any args as python then complains about the positional args missing.
Here is some more context: https://community.slack.com/archives/CHL4CLRCZ/p1640959405021200
One of the args I'd like to pass is actually an sqlalchemy object. Because after my slack action (button) is clicked, I want to double check my DB to make sure the user has been authed and there is an entry in the DB for said user. Now I have this working already when using a big file, but I'm trying to move away from having huge files, and have it more modular by importing different files using handlers as per @seratch's example (https://community.slack.com/archives/CHL4CLRCZ/p1634250648022400?thread_ts=1633856012.019600&cid=CHL4CLRCZ):
Category (place an
x
in each of the[ ]
)Requirements
Please read the Contributing guidelines and Code of Conduct before creating this issue or pull request. By submitting, you are agreeing to those rules.
The text was updated successfully, but these errors were encountered: