Skip to content
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

Unable to access thread_ts in message_replied message event payloads #796

Open
4 of 10 tasks
SpencerKaiser opened this issue Feb 13, 2021 · 6 comments
Open
4 of 10 tasks
Labels
discussion M-T: An issue where more input is needed to reach a decision enhancement M-T: A feature request for new functionality TypeScript-specific
Milestone

Comments

@SpencerKaiser
Copy link

Description

Describe your issue here.

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

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

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

  • I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version: ^3.2.0

node version: ^14

Expected result:

thread_ts will be available for messages

Actual result:

Compiler error

Code

  bolt.message(subtype('message_replied'), ({ message }) => {
    console.log(message.thread_ts);
  })

Property 'thread_ts' does not exist on type 'MessageEvent'.

@gitwave gitwave bot added the untriaged label Feb 13, 2021
@seratch seratch added question M-T: User needs support to use the project TypeScript-specific and removed untriaged labels Feb 14, 2021
@seratch
Copy link
Member

seratch commented Feb 14, 2021

Hi @SpencerKaiser , I know this may be a bit confusing but you can access the value via message.message.thread_ts.

app.message("foo", async ({ message }) => {
  if (message.subtype === "message_replied") {
    message.message.thread_ts
  }
});

event arg works in the same way:

app.message("foo", async ({ event }) => {
  if (event.subtype === "message_replied") {
    event.message.thread_ts
  }
});

@seratch seratch changed the title message_replied subtype types are broken Unable to access thread_ts in message_replied message event payloads Feb 14, 2021
@SpencerKaiser
Copy link
Author

SpencerKaiser commented Feb 14, 2021

@seratch thanks for the super quick response!!

I actually tried that earlier and had a similar problem... I know that value is there, but I can't seem to make any progress on getting it out safely:

types.ts

import { App, Middleware } from '@slack/bolt';

export type AppMiddlewareFunction<Args> = (app: App) => Middleware<Args>;

messageReplied.ts

export const messageReplied: AppMiddlewareFunction<SlackEventMiddlewareArgs<'message'>> = (app: App) => async ({
  message,
  event,
}) => {
  console.log(message.message);
  // Property 'message' does not exist on type 'MessageEvent'.
  // Property 'message' does not exist on type 'GenericMessageEvent'.
  
  console.log(message.thread_ts);
  // Property 'thread_ts' does not exist on type 'MessageEvent'.
  // Property 'thread_ts' does not exist on type 'EKMAccessDeniedMessageEvent'.

  console.log(event.message);
  // Property 'message' does not exist on type 'MessageEvent'.
  // Property 'message' does not exist on type 'GenericMessageEvent'.

  console.log(message.thread_ts);
  // Property 'thread_ts' does not exist on type 'MessageEvent'.
  // Property 'thread_ts' does not exist on type 'EKMAccessDeniedMessageEvent'.
};

I know that pattern might look a little strange, but it was the best thing my team and I could figure out to make the handlers easily testable (which they really are!!). Any idea what the issue is there?

@seratch
Copy link
Member

seratch commented Feb 14, 2021

@SpencerKaiser interestingly (it's a TypeScript compiler's magic), having if statement makes the value's type compatible with "message_replied" events.

@SpencerKaiser
Copy link
Author

I think the issue on my end is that SlackEventMiddlewareArgs<'message'> is yielding MessageEvent which does't include the properties from MessageRepliedEvent 🙁

Are you (or is someone else from your team) a Types guru that might now what's going on there?

@seratch
Copy link
Member

seratch commented Feb 14, 2021

yielding MessageEvent which does't include the properties from MessageRepliedEvent

MessageEvent is not fully deterministic yet. The type is a union type for ten types: https://github.com/slackapi/bolt-js/blob/%40slack/bolt%403.2.0/src/types/events/message-events.ts#L3-L11

Thus, the type provides only the fields available for all the possible types. And then, once you tell the subtype is "message_replied` (by having if statement as above), the TS compiler can narrow it down. I know this sounds a bit tricky but this is how this library works.

If this library provides type parameter like the one for app.action: https://github.com/slackapi/bolt-js/blob/%40slack/bolt%403.2.0/src/App.ts#L477-L480

app.action({"action_id": "aid", "type": "block_actions"}, async ({ action, ack }) => {
  action // this is compatible with `block_actions` payload
});

The following code may work.

app.message({"pattern": "foo", "subtype": "message_replied"}, async ({ message }) => {
    message.message.thread_ts // this compiles
});

@seratch seratch added discussion M-T: An issue where more input is needed to reach a decision enhancement M-T: A feature request for new functionality and removed question M-T: User needs support to use the project labels Feb 19, 2021
@SpencerKaiser
Copy link
Author

Setting that subtype to message_replied yields a message of type never ☹️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion M-T: An issue where more input is needed to reach a decision enhancement M-T: A feature request for new functionality TypeScript-specific
Projects
None yet
Development

No branches or pull requests

2 participants