Bolt-js: Create a Hubot robot.respond() equivalent middleware

Created on 31 Jan 2020  路  5Comments  路  Source: slackapi/bolt-js

Description

I've been playing around with Bolt to replace Botkit and I'm having some trouble getting directMention to work for messages sent in direct message to the bot. Code snippet and debug logs below.

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

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

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

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] 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: 1.5.0

node version: 12.14.1

OS version(s): Windows 10

Steps to reproduce:

  1. See code snippet below

Expected result:

A channel of the type "im" should be considered a directMention.

Actual result:

app.message never executes if directMention is used.

Attachments:

app.js

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

// Initializes your app with your bot token and signing secret
const app = new App({
    token: process.env.SLACK_BOT_TOKEN,
    signingSecret: process.env.SLACK_SIGNING_SECRET,
    logLevel: LogLevel.DEBUG
});

function logPayload({payload, next}) {
    console.log(
        `Inside logPayload
        Type: ${payload.type}, Text: ${payload.text}, Channel Type: ${payload.channel_type}`
    );
    next()
}

app.use(logPayload);

(async () => {
    // Start your app
    await app.start(process.env.PORT || 3001);

    console.log('鈿★笍 Bolt app is running!');
})();


app.message(directMention(), 'howdy', ({ message, say }) => {
    console.log(
        `Inside howdy
        Type: ${message.type}, Text: ${message.text}, Channel Type: ${message.channel_type}`
    );

    say('Howdy back');
});

Debug logs with results from above.

[DEBUG]   The logLevel given to WebClient was ignored as you also gave logger
[DEBUG]   initialized
[DEBUG]   apiCall('auth.test') start
[DEBUG]   will perform http request
鈿★笍 Bolt app is running!
[DEBUG]   http response received
[DEBUG]   Conversation context not loaded: Conversation not found
Inside logPayload
        Type: message, Text: howdy, Channel Type: im

enhancement

Most helpful comment

Digressing to the original ask (instead of creating a separate issue):
In Hubot, I could use robot.respond() and it would do two things:

  • handle direct mentions
  • handle messages sent in direct messages

Since directMention requires an @mention (regardless of the conversation type), I think a nice enhancement here would be for either a new middleware to act like this, or an update to directMention() that will handle both.
That way, developers can easily create something that doesn't require users to @mention the bot within a DM (which is somewhat redundant)

All 5 comments

Since creating this issue, I upgraded the bots OAuth scope from "bot" to the new granular permissions, leaving all of the existing permissions selected. Now the bot only responds in direct message when using directMention when I @botname someCommand. It no longer responds in channels when directMention is used in the same way.

This is likely a separate issue, but figured I would bring it up.

Hey @kmartin-isp,

One of the changes that came with granular bot permissions is that bots can only post in channels that they are already a member of. This is most likely why you aren't seeing the bot posting in those channels. You can invite the bot to a channel using the slack interface and the /invite command. Or you can invite them programmatically by using conversations.join method and channels:join.

We are working on offering a new scope that would allow bots to post to a public channel without joining it. You can see more about it at https://api.slack.com/authentication/quickstart#coming_soon.

Hope that answers your questions.

@stevengill is there any work being done to allow bots to post in private channels and direct messages as the bot (and not in response to a slash command or the user pressing a button) ?

I had another issue and was also curious if this will be possible in the future.

@jacklein Let me answer on behalf of him. Unfortunately, the Slack team doesn't have plans to offer a similar ability for private channels and DMs at the moment. As I guided at #401, your feedback from the Help Center would be greatly appreciated.

Digressing to the original ask (instead of creating a separate issue):
In Hubot, I could use robot.respond() and it would do two things:

  • handle direct mentions
  • handle messages sent in direct messages

Since directMention requires an @mention (regardless of the conversation type), I think a nice enhancement here would be for either a new middleware to act like this, or an update to directMention() that will handle both.
That way, developers can easily create something that doesn't require users to @mention the bot within a DM (which is somewhat redundant)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dschinkel picture dschinkel  路  4Comments

Falci picture Falci  路  5Comments

dschinkel picture dschinkel  路  4Comments

simonsayscode picture simonsayscode  路  3Comments

TK95 picture TK95  路  3Comments