slackapi / node-slack-sdk

Slack Developer Kit for Node.js

Home Page:https://slack.dev/node-slack-sdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot pass the blocks in a web api response as chat.update argument

sizuhiko opened this issue · comments

Slack bolt can use like followings:

say({
  blocks: [
    {
      type: 'header',
      text: { type: 'plain_text', text: 'TITLE' },
    },
    {
      type: 'section',
      text: { type: 'plain_text', text:'BODY },
    },
  ],
  text: 'TEXT',
});

Each block types defined into blocks.ts.
https://github.com/slackapi/node-slack-sdk/blob/main/packages/types/src/block-kit/blocks.ts

I want update messages using client.chat.update API followings:

client.chat.update({ 
  channel,
  ts,
  blocks: [
    {
      type: 'header',
      text: { type: 'plain_text', text: 'UPDATE TITLE' },
    },
    {
      type: 'section',
      text: { type: 'plain_text', text:'UPDATE BODY },
    },
  ],
  text: 'TEXT'
});

But, occur compile error.

error TS2322: Type '"header"' is not assignable to type 'BlockType | undefined'.

234               type: 'header',
                  ~~~~

  node_modules/@slack/web-api/dist/response/ConversationsHistoryResponse.d.ts:937:5
    937     type?: BlockType;
            ~~~~
    The expected type comes from property 'type' which is declared here on type 'PurpleBlock'

The PurpleBlock's BlockType not have header, input and file existing into block-kit/blocks.ts.
https://github.com/slackapi/node-slack-sdk/blob/main/packages/web-api/src/response/ConversationsHistoryResponse.ts#L341

Packages:

Select all that apply:

  • @slack/web-api
  • @slack/rtm-api
  • @slack/webhooks
  • @slack/oauth
  • @slack/socket-mode
  • @slack/types
  • I don't know

Reproducible in:

The Slack SDK version

npm ls | grep -o "\S\+@\S\+$" | tr @ ' ' | awk -v q='"' '{print q$1q": "q"^"$2q","}' | grep slack

"slack/bolt": "^3.15.0",

Node.js runtime version

v18.12.1

Hi @sizuhiko, thanks for asking the question.

As you may have already noticed, the types of web API responses are automatically generated and are generally incompatible with WebClient's argument types. Consequently, as you have pointed out, it is not feasible to directly pass the response type objects. However, you can bypass this issue by casting the type as follows:

const response = (await client.conversations.history({
  channel: "C111",
}));
const blocks: KnownBlock[] | undefined = response.messages ? response.messages[0].blocks as KnownBlock[] : undefined;
await client.chat.update({
  channel: "C1111",
  ts: "111.222",
  text: "Hi!",
  blocks: blocks,
})

Alternatively, when you have a static set of blocks like the code snippet in this issue, it compiles without any problems:

const response = await client.chat.update({ 
  channel: "C1111",
  ts: "111.222",
  blocks: [
    {
      type: 'header',
      text: { type: 'plain_text', text: 'UPDATE TITLE' },
    },
    {
      type: 'section',
      text: { type: 'plain_text', text:'UPDATE BODY' },
    },
  ],
  text: 'TEXT'
});

Lastly, if you're comfortable using a 3rd party library for Slack API calls, my personal project could provide a solution:

Please be aware that this "slack-web-api-client" library is not officially maintained by Slack. Although I plan on maintaining it for a long time, if you prefer using the one maintained by the Slack team, please go ahead with the above workaround (i.e., type casting).

I hope this helps.

Thanks feedback, @seratch.

I have this problem for updating slack/bolt v3.14.0 to v3.15.0.
v3.14.0 no problem.

It had dependencies followings:

  • "@slack/types": "^2.8.0",
  • "@slack/web-api": "^6.7.1",

Exactly, I use to update messages on Conversation History partially.
I try casting the type to KnownBlock.

I resolved this problem to type casting PurpleBlock to KnownBlock.
Thanks.