I’ve been really interested recently about the serverless architecture and how I could leverage it to build a Slack bot. Driven by my curiosity, I decided to challenge myself to build one on AWS Lambda. For the sake of this tutorial, I will walk-through a sample use case that you could replicate at your company.
A salesperson is actively interacting with colleagues on Slack. She would like to be aware of any new opportunities created in her Salesforce organization. She could open her browser every day to see for new material, but she’d prefer to be notified proactively so she can remain focused on high priority tasks.
As a developer, you think that Slack could be a good channel to post new opportunities, considering the sales team is interacting on it on a daily basis.
This article will walk-through the steps to create your first Slack bot integration. The bot will be a serverless app hosted as an AWS Lambda function, leveraging API Gateway to route the HTTP requests to the function and passively listening to outbound messages from Salesforce.
In order to follow the tutorial, you will need to have the following installed:
- yarn, a fast and reliable dependency manager.
- up, a serverless app orchestration toolset.
Considering NodeJS and TypeScript are quite common, we’ll skip their installation process and jump straight to Up.
1. Install & configure the orchestration tool
If you do not have the Up CLI toolset already, installing it is really easy - simply copy the following in your shell:
Make sure you have your AWS credentials in your
~/.aws/credentials file and have an IAM Policy setup.
For more information, refer to the documentation on how to setup your AWS Credentials.
2. Get the repository & dependencies
Before we get started, clone the Github repository and get all the dependencies using yarn:
3. Review Codebase
app.ts - the application server
app.ts file contains the server code that will be listening for incoming
requests. For this example, we are using Koa.js, a Web
framework designed by the team behind Express. It will parse the SOAP request and
post its content to Slack.
slack.ts - the Slack data service
slack.ts file contains the logic interacting with the Slack API.
src/typings/global.d.ts- global type definition
src/typings/slack.d.ts- Slack API’s type definition
package.json- your NodeJS package file
tsconfig.json- your TypeScript configuration file
up.json- the configuration file for Up (this is where environment variables are set)
webpack.config.js- the compiling config file for Webpack
4. Setup Slack
You’ll have to install custom bots and create one to get your own Slack auth token. Head over to:
Generate your first bot configuration and copy the API token to your clipboard. Then,
up.json file and replace the placeholder API token with the one in your
Now that this is all set, login into your Slack instance and create the
Slack channel - this is where our Slack bot will post new Salesforce opportunities.
5. Deploy & Retrieve HTTP Endpoint URL
Now that you have your codebase ready for deployment and Up configured, run from your terminal the following command:
This will do all the magic of building your app, deploy as a Lambda function and setup AWS API Gateway for it. No extra work required!
Finally, to get the staging URL of your HTTP server exposed on API Gateway, execute:
6. Set Workflow Rule & Outbound Message
Now that we have our Slack bot ready on AWS Lambda, it’s now time to configure Salesforce to send new opportunities to our serverless application. Since our serverless application is ready to process Outbound Messages coming from Salesforce, we’ll create a Workflow Rule and an Outbound Message.
Open the Workflow Rules wizard page in your Salesforce environment and click on New Rule.
Build > Create> Workflow & Approvals > Workflow Rules
Copy the following settings:
- Salesforce Object: Opportunity
- Rule Name: On Insert - Opportunity
- Evaluation Criteria: Created
- Rule Criteria: Formula evaluates to true, with
trueas the formula.
Then, create an Outbound Message - it should look like this:
Make sure to replace the Endpoint URL with your API Gateway URL.
When you are done setting up the Outbound Message, save it and Activate it!
7. Test it!
Everything should be ready to give it a shot. Spin up the Opportunity tab in your
Salesforce organization and create a new one. Make sure to populate the Name, Amount
and Close Date. On save, you should get a nicely crafted message straight into your
Congratulations - you’ve successfully setup your serverless Slack bot! At this point, anything is possible. You could push any other type of records to Slack using outbound messages or trigger some more complex messages using Apex Triggers and API calls.
For any question on how to get this to work, ping me on Twitter @jpmonette.