DEV Community

Discussion on: Building a Slack bot with Nodejs

Collapse
 
aoberoi profile image
Ankur Oberoi

Hey this is great! I have one small suggestion. In your last code example, you can simplify a little bit by using the client argument which Bolt passes into your listener. That client instance already knows the bot token, so you don't have to also read it from the context argument.

bot.event("app_mention", async ({ event, client }) => {
  try {
    await client.chat.postMessage({
      channel: event.channel,
      text: `Hey yoo <@${event.user}> you mentioned me`
    });
  }
  catch (e) {
    console.log(`error responding ${e}`);
  }
});
Enter fullscreen mode Exit fullscreen mode

And actually, you can make this even shorter (but then it starts to hide some of the interesting details you might want your readers to learn). The say argument already knows about the bot token, the channel ID, and knows that the method to call is chat.postMessage:

bot.event("app_mention", async ({ event, say }) => {
  try {
    await say(`Hey yoo <@${event.user}> you mentioned me`);
  }
  catch (e) {
    console.log(`error responding ${e}`);
  }
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oghenebrume50 profile image
Raphael Noriode

Hi sorry for the late reply but you are absolutely right, thank you for this.