1

I've built a slash command on slack that tries to utilize slack's built-in file upload and accepts text together with media files (and posts it to another channel if specific keywords are present).

However, whenever I get the payload of the command I only get the textual part of the message, and the image/video/voice memo are left out.

Is it possible to get user uploaded files through a slash command or slack bot? How shall I go about it?

I've tried adding the scopes files:read and files:write (together with the standard commands) and sent a message with an uploaded image or voice memo (recorded via slack).

In both cases all I got was only the text part of the command:

token=<TOKEN>&team_id=<TEAM_ID>&team_domain=<DOMAIN>&channel_id=<CHANNEL_ID>&channel_name=directmessage&user_id=<USER_ID>&
user_name=<USERNAME>&command=%2Fcreate&text=can+I+send+a+%23voice+%23memo&
api_app_id=<APP_ID>&is_enterprise_install=false&
response_url=https%3A%2F%2Fhooks.slack.com%2F<...>&trigger_id=<TRIGGER_ID>
Nuriel
  • 3,731
  • 3
  • 23
  • 23

1 Answers1

0

I ended up using the Slack Bolt library which supports all the new slack features and events. By doing that you gain access to the full message body, which also includes the event attribute, in which you can find files array.

Code sample:

const { App } = require('@slack/bolt');

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
  socketMode: true,
  appToken: process.env.SLACK_APP_TOKEN
});

// Listens to incoming messages that contain "hello"
app.message('hello', async ({ message, say, logger, body, event }) => {
    // notice event.files
    logger.info('Body', body, 'Files', event.files);

    await say(`Hey there <@${message.user}>!`);
});

This message will react to any message containing "hello", log files array within event and reply with a friendly message.

Example files array logged by the logger (3 seconds voice memo in this case):

[
  {
    id: '<file-id>',
    created: 1671452738,
    timestamp: 1671452738,
    name: 'audio_message.webm',
    title: 'audio_message.webm',
    mimetype: 'audio/webm',
    filetype: 'webm',
    pretty_type: 'WebM',
    user: '<user-id>',
    user_team: '<team-id>',
    editable: false,
    size: 39033,
    mode: 'hosted',
    is_external: false,
    external_type: '',
    is_public: false,
    public_url_shared: false,
    display_as_bot: false,
    username: '',
    subtype: 'slack_audio',
    transcription: { status: 'processing' },
    url_private: 'https://files.slack.com/files-tmb/<ids>/audio_message_audio.mp4',
    url_private_download: 'https://files.slack.com/files-tmb/<ids>/download/audio_message_audio.mp4',
    duration_ms: 2421,
    aac: 'https://files.slack.com/files-tmb/<ids>/audio_message_audio.mp4',
    audio_wave_samples: [
       1,  0,  0,  0,  1,  1,  1,  0,  0,   0,  0,  0,
       0,  0,  0,  0,  0,  0,  0,  0,  0,   0,  0,  0,
       0,  0,  0, 12, 37, 47, 54, 62, 68,  79, 80, 58,
      39, 30, 22, 18, 46, 82, 87, 90, 94, 100, 96, 72,
      51, 76, 77, 62, 38, 47, 51, 51, 60,  68, 70, 60,
      52, 42, 54, 66, 62, 61, 63, 50, 53,  55, 58, 47,
      40, 36, 32, 27, 23, 16, 11,  6,  4,   4,  4,  3,
       2,  2,  4,  5,  4,  3,  3,  3,  2,   2,  2,  2,
       1,  1,  1,  1
    ],
    media_display_type: 'audio',
    permalink: 'https://<workplace-id>.slack.com/files/<file-id>/audio_message.webm',
    permalink_public: 'https://slack-files.com/<file-id>',
    has_rich_preview: false,
    file_access: 'visible'
  }
]

I couldn't find any specific documentation about sending files to a slack bot, hopefully that helps.

Nuriel
  • 3,731
  • 3
  • 23
  • 23