To create a first version of the event handling function for Slack.
This version only responds to the user by repeating the information the bot received.
Note: Diagrams are created using https://www.draw.io/
Slack Post a message API: https://api.slack.com/methods/chat.postMessage
Slack events: https://api.slack.com/events/message
-
Create actions directory
-
Create a file called slackapp-event.js. This is a IBM Cloud Functions Node.js action that is responsible for fetching Slack bot information that was saved to the Cloudant database during registration and listening and responding to Slack event.
-
Modify the code to do the following
- Add a function that will post a message to the channel
/** * Posts a message to a channel with Slack Web API * * @param {String} accessToken - authorization token * @param {String} channel - the channel to post to * @param {String} text - the text to post * @param {function} callback - function(err, responsebody) * @returns {null} Nothing */ function postMessage(accessToken, channel, text, callback) { request( { url: "https://slack.com/api/chat.postMessage", method: "POST", form: { token: accessToken, channel, text } }, (error, response, body) => callback(error, body) ); }
- Add code to ignore certain event types
// Identify if there are message subtypes to ignore See https://api.slack.com/events/message for details const ignoreEventSubTypes = [ "bot_message", "group_join", "channel_join", "group_leave", "channel_leave" ]; if (_.includes(ignoreEventSubTypes, args.event.subtype, 0)) { // Console.log("This is message subtype we can ignore: ", args.event.subtype); return { statusCode: 200 }; }
- Add code to reply to the user when a message is posted in the channel (leverage the method for posting above)
// Reply to the message function (registration, callback) { if (event.event.type === "message" && !event.event.bot_id) { console.log(`Processing message from ${event.event.user}: ${event.event.text}`); // This repeats the message from the use back to the channel and should likely not be used postMessage( registration.bot.bot_access_token, event.event.channel, `Hey ${event.event.user}, you said ${event.event.text}`, (err, result) => { callback(err); } ); return true; } return callback(null); }
-
Deploy the function to IBM Cloud. You will be running the shell script deploy.sh that contains IBM Cloud Functions / OpenWhisk command line commands to create or delete a package and web actions.
-
Ensure your
ibmcloud wsk
command line interface is property configured with:ibmcloud wsk list
This shows the packages, actions, triggers and rules currently deployed in your namespace.
-
Reuse the parameters.json configurations previously prepared from lab0, copying it to the same directory as your deploy.sh
cp ../../lab00-preparation/parameters.json .
-
Create the actions:
For linux or Mac user, you may need to add the execute permission first
chmod +x deploy.sh
On the command line you can then run the following command to deploy.
./deploy.sh --install
If all goes well it outputs:
Creating slackapp package ok: created package slackapp Adding app registration command ok: created action slackapp/slackapp-register Adding app event processing ok: created action slackapp/slackapp-event OAuth URL: https://us-south.functions.cloud.ibm.com//api/v1/web/<org_space>/slackapp/slackapp-register Event Subscription Request URL: https://us-south.functions.cloud.ibm.com//api/v1/web/<org_space>/slackapp/slackapp-event
Note: the script can also be used to --uninstall the Cloud Functions artifacts to --update the artifacts if you change the action code.
-
-
Go to your Slack API service page to add a bot user
-
In Slack add the event subscription
-
Go to the Event Subscriptions section of your app
-
Enable Events
-
Set the Request URL to the URL of the
slack-event
web action. The URL should look likehttps://us-south.functions.cloud.ibm.com/api/v1/web/your-org_your-space/slackapp/slackapp-event
.You can run the command below to determine the Request URL:
./deploy.sh --install
Slack will contact this URL immediately. It should turn to Verified if the Cloud Functions configuration steps worked.
-
In Slack add the bot event
This allows us to react to direct messages sent to a bot. We could select more event type but for our simple app will only deal with this one today.
- Repeat for the following events: message.channels, message.groups, message.mpim
- Save the changes
-
Under OAuth and Permissions, add a new Redirect URL. This URL will be called when a user installs your application in a team. It should point to the
slackapp-register
web action. The URL should look likehttps://us-south.functions.cloud.ibm.com/api/v1/web/your-org_your-space/slackapp/slackapp-register
-
Click Save URLs
Our app is finally ready to be installed!
-
To see what's happening behind the scene as Slack calls our actions, open a new command prompt and run
ibmcloud wsk activation poll
Leave it running. Actions triggered by Slack will show up there
-
Go to Manage Distribution
-
Click the Add to Slack button
-
Select the team where to install the app
-
Authorize the app
After a few seconds, the app is installed. You should see logs appearing in the activation polling as Slack will be sending the registration info we will use later to interact with Slack channels and users.
In your web browser, you should see
Registration was successful. You can try the command in Slack or send a direct message to the bot.
Ideally you would redirect to another page once the registration is completed.
-
In Slack, add the bot to the channel
- Select the channel
- Click the cog wheel for "Channel Settings"
- Select "Invite new members to join..."
- Type the name of the bot and invite the bot to the channel
-
Type a message and get it sent back out to you
You are now ready to start Lab 01 - Step 02 - Get more info