0

here is some context, im staring to use the slack BOLD SDK for python and while the guide is great... posting those gigantic block of code for the block kit part is a mess so I want to create a new class/function and import it .. so, instead of this:

main.py
-------
@app.message("hello")
def message_hello(message, say):
    # say() sends a message to the channel where the event was triggered
    say(
        blocks=[
            {
                "type": "section",
                "text": {"type": "mrkdwn", "text": f"Hey there <@{message['user']}>!"},
                "accessory": {
                    "type": "button",
                    "text": {"type": "plain_text", "text": "Click Me"},
                    "action_id": "button_click"
                }
            }
        ],
        text=f"Hey there <@{message['user']}>!"
    )

I can do this:

msg.py:
--------
def hello():
       return blocks=[
            {
                "type": "section",
                "text": {"type": "mrkdwn", "text": f"Hey there <@{message['user']}>!"},
                "accessory": {
                    "type": "button",
                    "text": {"type": "plain_text", "text": "Click Me"},
                    "action_id": "button_click"
                }
            }
        ],
        text=f"Hey there <@{message['user']}>!"

main.py
--------
import from lib msg

@app.message("hello")
def message_hello(message, say):
    # say() sends a message to the channel where the event was triggered
    say(msg.hello())

Its been a while since I used Python last time and im having issues with the return part at msg.py.. I tried to put the code between single quotes, double quotes, triple double quotes, ticks, parenthesis, curly braces.. nothing works..

anyone can refresh my memory and help me?.

Thanks!

Santos
  • 177
  • 1
  • 1
  • 15

2 Answers2

0

I'm not convinced that what you're trying to achieve is actually useful.

You're just moving the "gigantic block of code mess" from one function to another, except now you have two functions instead of one. Either function is meaningless without the other - since they depend on each other so much, it makes sense to merge them, or to just not split them up in the first place.

The "keyword argument-y" syntax in the return statement of the hello function is not valid syntax. hello will have to return some kind of collection, like a tuple or list, or a dictionary even, since you're trying to return more than one thing. This means that, later on in your message_hello function, you'd have to unpack your returned data structure (not a big deal).

You have two f-strings that expect to access a variable named message. At the time these literals are evaluated, no such variable exists within the scope of the function, so that's a runtime error. Another reason to not split your original code into two separate functions.

If you really want to do it this way, you'll probably end up having to do something like the following:

def hello():
    return [
        {
            "type": "section",
            "text": {"type": "mrkdwn", "text": "Hey there <@{}>!"},
            "accessory": {
                "type": "button",
                "text": {"type": "plain_text", "text": "Click Me"},
                "action_id": "button_click"
            }
        }
    ], "Hey there <@{}>!"

@app.message("hello")
def message_hello(message, say):
    from msg import hello
    kwargs = dict(zip(("blocks", "text"), hello()))
    kwargs["blocks"]["text"]["text"].format(message["user"])
    kwargs["text"].format(message["user"])
    say(**kwargs)

Again, you'll have to do all the string formatting later in message_hello, which is not great because now this function needs to know about the structure of the object(s) returned by hello.

Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • Thanks!.. I kinda make it to work by just returning a proper json object but reading your reply I think I can make it better... too bad there is no JSX syntax for python like in node JS for this.. is so much better.. – Santos Aug 12 '21 at 21:14
0

I "fix it" by passing a proper JSON object:

def msg_click(message):
    return {
        "blocks": [
                     {
                         "type": "section",
                         "text": {"type": "mrkdwn", "text": f"Hey there <@{message['user']}>!, please click the button :point_right:"},
                         "accessory": {
                             "type": "button",
                             "text": {"type": "plain_text", "text": "Click Me"},
                             "action_id": "button_click"
                         }
                     }
                 ],
        "text": f"Hey there <@{message['user']}>!"
    }

I simply used the json format from the block kit builder... messy but works.

Santos
  • 177
  • 1
  • 1
  • 15