• Breaking News

    Create your own Slack Bot with Node and JavaScript - Part II


    In Part 2 of this article, I will show you how to enable and respond to Slack Incoming Events.

    Part I is available here: Create your own Slack Bot with Node and JavaScript


    Enable Incoming WebHooks

    Load the Slack Bot settings via the "Your Apps" page: YOUR APPS

    From here select the Incoming Webhooks feature, and click the Activate Incoming Webhooks toggle to switch it on.

    Once enabled, under "Webhook URLs for Your Workspace" click the "Add New Webhook to Workspace" to add one.

    Keep the "Post To" event and pick a channel that the bot will post to, and then click to "Authorize your app".

    You'll be sent back to your app settings, and you should now see a new entry under the "Webhook URLs for Your Workspace" section.

    Use your Incoming Webhook URL to post a message

    Just copy the sample curl request to post to a channel and execute it.

    curl -X POST -H 'Content-Type: application/json' --data '{"text":"Hello World!"}' 
        https://hooks.slack.com/services/T000000/B000000/00000000    
    
    (update this code accordingly)

    And that's it!

    Go and check the channel that your app was installed into, and you should see that the "Hello, World" message has been posted by your app.

    Posting a message with Incoming Webhooks using Node JS

    This is the code you will need to achieve this goal:

    const { IncomingWebhook } = require('@slack/client');
    const url = 'https://hooks.slack.com/services/T00000000/B00000000/00000000' ;
    
    const webhook = new IncomingWebhook(url);
    
    webhook.send('Hello there', function(err, res) {onst ret = regex.test(x);
      if (err) { 
         console.log('Error :', err);
      } else { 
         console.log('Message sent: ', res); 
      }
    });
    Run it as you usually do and then go and check your Slack channel.

    Excellent! you've set up Incoming Webhooks for your Slack app and made a successful test call.

    The next post will address how to listen and respond to messages sent by other channel's members.


    No comments