slackapi / bolt-js

A framework to build Slack apps using JavaScript
https://tools.slack.dev/bolt-js/
MIT License
2.74k stars 393 forks source link

I have a question about the handler parameter of AppHomeOpenedEvent. #1678

Closed dogemad closed 1 year ago

dogemad commented 1 year ago

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

Requirements (place an x in each of the [ ])


I'm developing a bot in a TypeScript environment by creating a custom decorator like this:

@Events()
class HomeEventsImpl {
  @Event("app_home_opened")
  async appHomeOpened(
    // I bound the type that appears in app.event("app_home_opened", async (args) => {}) to args.
    args: SlackEventMiddlewareArgs<"app_home_opened"> & AllMiddlewareArgs
  ) {
    const { event, client, payload } = args;

    payload.view.state // i cant get it.
  }
}

in this case payload.view.state cannot be referenced.

According to the docs, the app_home_opened event has a payload of:

{
    "type": "app_home_opened",
    "user": "U061F7AUR",
    "channel": "D0LAN2Q65",
    "event_ts": "1515449522000016",
    "tab": "home",
    "view": {
        "id": "VPASKP233",
        "team_id": "T21312902",
        "type": "home",
        "blocks": [
           ...
        ],
        "private_metadata": "",
        "callback_id": "",
        "state":{
            ...
        },
        "hash":"1231232323.12321312",
        "clear_on_close": false,
        "notify_on_close": false,
        "root_view_id": "VPASKP233",
        "app_id": "A21SDS90",
        "external_id": "",
        "app_installed_team_id": "T21312902",
        "bot_id": "BSDKSAO2"
    }
}

If I'm doing something wrong, I'd like to know how to do it right. Help?

seratch commented 1 year ago

Hi @dogemad, thanks for asking this question!

In the case of the view for Home tab (note: app_home_opened is an event related to Home tab), view.state.values does not exist. The values can be provided mainly for the modal view data submissions with input blocks.

The document should be more accurate. I will escalate this issue to the document maintenance team.

dogemad commented 1 year ago

@seratch thanks for your comment.

However, since this code works in JavaScript, not TypeScript, it seems that it is not just a problem with the documentation. It seems that other services, including us, also pass values ​​to Homeview.state and refer to it.

// examples: google calendar home payload

{
   "ok":true,
   "view":{
      "id":"...",
      "team_id":"...",
      "type":"home",
      "blocks":[
         ...
      ],
      "private_metadata":"",
      "callback_id":"",
      "state":{
         "values":{
            "qkn":{
               "AGENDA_DATEPICKER":{
                  "type":"datepicker",
                  "selected_date":"2022-12-08"
               }
            }
         }
      },
      ...
   }
}

What do you think about it? If this is not what you intend, I think you should provide proper guidance on passing state values ​​and modifying documentation.

Thanks!

seratch commented 1 year ago

@dogemad Thanks for sharing this.

Indeed, when you have input blocks in a Home tab, state.values can be updated after clicking/selecting items. However, since there is no way to submit those values at a time (like you can do on a modal), a recommended way to receive the selected values is to have "dispatch_action: true" for the input blocks and then handle block_actions requests (app.action() handling patterns in bolt-js), which can be sent immediately after the user interaction happens.

Using input blocks in a Home tab does not bring significant benefits. Thus, we recommend going with either of the following approaches:

I hope this was helpful to you.

I will continue working with the document teams to improve this. Thanks again for the feedback!

dogemad commented 1 year ago

@seratch Thank you for your comments and hard work.

One more thing I want to mention is that among the Bolt js examples, I think the TypeScript examples also need to be updated. It's actually a typescript example with no types. If possible, how about providing a strongly typed recommended example for native events like you said?

I hope this was helpful to others too.

seratch commented 1 year ago

@dogemad

It's actually a typescript example with no types.

Sorry, I don't get the point here. Are you talking about this one? https://github.com/slackapi/bolt-js/tree/main/examples/getting-started-typescript If yes, the example is actually typed as the TS compiler compiles the code.

If possible, how about providing a strongly typed recommended example for native events like you said?

If what you meant by the above sentence is the example app should have more listener code examples in it, I agree that it's worth adding more example code.

dogemad commented 1 year ago

@seratch

Sorry, I don't get the point here. Are you talking about this one? https://github.com/slackapi/bolt-js/tree/main/examples/getting-started-typescript If yes, the example is actually typed as the TS compiler compiles the code.

As you said, the TypeScript compiler obviously works. However, I would say that the purpose of the TypeScript compiler is not only to execute, but also to define values, types, functions and classes. Because TypeScript actually has many uses. (such as decorators)

If what you meant by the above sentence is the example app should have more listener code examples in it, I agree that it's worth adding more example code.

I agree with this because I believe that kind examples create more users.

Thanks.

seratch commented 1 year ago

Thanks for your quick reply,

However, I would say that the purpose of the TypeScript compiler is not only to execute, but also to define values, types, functions and classes. Because TypeScript actually has many uses. (such as decorators)

If your pain point is related to the exported types of listener arguments and so on, those types may not be friendly enough for the developers who want to build a TypeScript framework/library on top of it (like you). However, since bolt-js' internal types are a bit complex, it's not easy to improve those types without breaking changes.

We know there are still many things to improve for TypeScript use cases. With that being said, we'd like to avoid having a single long discussion thread for everything related to TS (because it's hard to manage). So, whenever you have specific feedback/suggestions, please feel free to create a new issue for each.

dogemad commented 1 year ago

@seratch I understood what you said. Thank you for your valuable comments. I'll come back to another thread if I have any other feedback. thank you!