I am new to Slack APP development, I am developing the app using Slack bolt python. In my app I have configured the slack interaction using the button click, on clicking the button I am calling the corresponding listener(identified by action_id mentioned in the button). In the listener I am getting the block_actions payload which contains all the state values of the event, but in addition to that I want to some arguments to the listener function. Is it possible in any way that we can send additional arguments to the listeners
@app.action("change_time_period")
def handle_change_time_period(ack, context, body, client, logger, message, say):
# want the arguments here such as passing email_id from post_data
def post_data(user_id, email_id):
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
result = client.chat_postMessage(
channel=user_id,
blocks=[
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"emoji": True,
"text": "Change time period"
},
"style": "primary",
"value": "time_period",
"action_id": "change_time_period"
}
]
}
]
)
In the code above, I will be calling the post_data which will post the message containing the block kit, once the user clicks the button the handle_change_time_period will be called
This is now possible using message metadata:
When you post the message, include metadata with an event_payload containing the arguments you want to pass (and any event_type - doesn't really matter).
When the button is clicked, the body with which the action listener is invoked contains the message, and that will contain the full metadata that you passed (the structure could be slightly different in Python, e.g. in your example it looks like there's a top-level message argument, so maybe you don't need body; I've only tested this with Bolt for JS).
So, for example:
@app.action("change_time_period")
def handle_change_time_period(ack, context, body, client, logger, message, say):
email_id = body["message"]["metadata"]["event_payload"]["email_id"]
client.chat_postMessage(
channel=user_id,
blocks=[...]
metadata={
"event_type": "foo",
"event_payload": {
"email_id": "123"
}
}
)
(Just make sure to use the right metadata structure with event_type & event_payload when posting, as otherwise it seems the metadata is just silently ignored.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With