DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Microsoft Azure Active Directory
  • Build Your Own AI Avatar App From Scratch in Less Than 8 Hours
  • The Technology Stack Needed To Build a Web3 Application
  • How To Use IBM App Connect To Build Flows

Trending

  • My LLM Journey as a Software Engineer Exploring a New Domain
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Hybrid Cloud vs Multi-Cloud: Choosing the Right Strategy for AI Scalability and Security
  1. DZone
  2. Data Engineering
  3. Databases
  4. Tutorial: Build a Serverless API Back-end for Slack

Tutorial: Build a Serverless API Back-end for Slack

Use Azure Functions to build a Slack app

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Updated Jul. 31, 20 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
8.5K Views

Join the DZone community and get the full member experience.

Join For Free

Webhook backends are a popular use case for Serverless functions. FaaS (Functions-as-a-service) offerings make it relatively easy to provide an HTTP endpoint which hosts the Webhook logic which can be as simple as sending an email to something as entertaining as responding with funny GIFs!

In this tutorial, we will explore funcy — a Serverless webhook backend which is a trimmed down version of the awesome Giphy for Slack. The (original) Giphy Slack app returns a bunch of GIFs for a search term, and the user can pick one of them. Funcy tweaks it a bit by simply returning a (single) random image for a search keyword using the Giphy Random API.

This blog post provides a step-by-step guide to getting the application deployed to Azure Functions and hooking it up with your Slack workspace. The code is available on GitHub for you to grok.

Overview

funcy is built as a Slash Command within Slack. As a user, you can invoke it from your Slack workspace using /funcy <your search term>. This, in turn, invokes our Webhook deployed to Azure Functions — which is nothing but a bunch of Java code. It calls the Giphy Random API and returns the result back to the user.

For example, invoking it from your Slack workspace using /funcy serverless will return a random GIF.

The upcoming sections will guide you through the following:

  • Pre-requisites
  • Slack setup and configuration
  • Deploying to Azure Functions

Prerequisites

Before you proceed, ensure that you have the following ready - it shouldn't take too long

  • Maven - The Maven Plugin for Azure Functions is used to build and deploy your Java function. If you don't have Maven, please install v 3.0 or above from here.
  • Azure CLI - Follow the instructions to setup and login to the Azure CLI.
  • Slack Workspace - Please create a Slack workspace if you don't have one.
  • GIPHY API key - You need to create a GIHPY account (it's free!) and create an app. Each application you create will have its own API Key.

Please note down your GIPHY API key as you will be using it later

Configure Slack

Please note that the instructions in this section have been adapted from the Slack documentation.

Create a Slack App

Sign into your Slack Workspace. Start by creating a new Slack App.

Create a Slash Command

Once you're done creating the app, head to your app's settings page, and then click the Slash Commands feature in the navigation menu.

You'll be presented with a button marked Create New Command, and when you click on it, you'll see a screen where you'll be asked to define your new Slash Command with the required information.

Enter the required information. Please note that the Request URL field is the one where you will enter the HTTP endpoint of function which will be available after you deploy it. You can use a dummy URL as a placeholder just for the time being e.g. https://temporary.com:4242

Once you're done, hit Save to finish.

Install the App to Your Workspace

Once you're done creating the Slash Command, head to your app's settings page, click the Basic Information feature in the navigation menu, choose Install your app to your workspace and click Install App to Workspace. This will install the app to your Slack workspace to test your app and generate the tokens you need to interact with the Slack API.

As soon as you finish installing the app, the App Credentials will show up on the same page. You need to grab your Slack Signing Secret from there

Make a note of your app Signing Secret as you'll be using it later

Deploy to Azure

Start by cloning the GitHub repository and change into the application directory

Shell
xxxxxxxxxx
1
 
1
git clone https://github.com/abhirockzz/funcy-azure-functions
2
cd funcy-azure-functions


The pom.xml file contains the following attributes used by the Azure Functions Maven plugin - application name (functionAppName), region (functionAppRegion) and resource group (functionResourceGroup). It has default values for the above parameters, so you can choose to continue using them if you like.

  • functionAppName - funcyapp
  • functionAppRegion - westus
  • functionResourceGroup - java-functions-group

Please note that the app name must be unique across Azure.

If you wish to wish to change the values, please take a look at this snippet from pom.xml which highlights the <properties> which need to be updated

XML
xxxxxxxxxx
1
17
 
1
<properties>
2
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3
    <maven.compiler.source>1.8</maven.compiler.source>
4
    <maven.compiler.target>1.8</maven.compiler.target>
5
    <azure.functions.maven.plugin.version>
6
      1.3.1
7
    </azure.functions.maven.plugin.version>
8
    <azure.functions.java.library.version>
9
        1.3.0
10
    </azure.functions.java.library.version>
11
    <functionAppName>YOUR_APP_NAME</functionAppName>
12
    <functionAppRegion>AZURE_REGION</functionAppRegion>
13
    <functionResourceGroup>RESOURCE_GROUP_NAME</functionResourceGroup>
14
    <stagingDirectory>
15
      ${project.build.directory}/azure-functions/${functionAppName}      
16
    </stagingDirectory>
17
</properties>

The name of the function is not the same as application name (configured via pom.xml) and is specified using the @FunctionName annotation in the Java code for the function - in this case, the name is funcy.

You can now build the function and deploy it to Azure

Shell
xxxxxxxxxx
1
 
1
//build
2
mvn clean package
3
4
//deploy
5
mvn azure-functions:deploy


The results from a successful deployment will look something like this

Plain Text
xxxxxxxxxx
1
 
1
[INFO] Successfully updated the function app.funcyapp
2
[INFO] Trying to deploy the function app...
3
[INFO] Trying to deploy artifact to funcyapp...
4
[INFO] Successfully deployed the artifact to https://funcyapp.azurewebsites.net
5
[INFO] Successfully deployed the function app at https://funcyapp.azurewebsites.net
6
[INFO] ------------------------------------------------------------------------
7
[INFO] BUILD SUCCESS


Use the Azure CLI to list your Functions App

Shell
xxxxxxxxxx
1
 
1
az functionapp list --query "[].{hostName: defaultHostName, state: state}"


You should see a JSON output

JSON
xxxxxxxxxx
1
 
1
[
2
    {
3
        "hostName": "funcyapp.azurewebsites.net",
4
        "state": "Running"
5
    }
6
]

You should be able to see the function (under Function App menu) in Azure Portal

Once the deployment is successful, the function should be ready to serve requests and can be accessed over HTTP(s) at the following endpoint - https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>

For an application named funcyapp with a function called funcy, the endpoint would be

Plain Text
xxxxxxxxxx
1
 
1
https://funcyapp.azurewebsites.net/api/funcy


You're almost there!

Update your Azure Functions app

Now that the Azure Functions app is up and running, you need to update it to seed the Giphy API Key and Slack Signing Secret as environment variables.

Shell
xxxxxxxxxx
1
 
1
az functionapp config appsettings set --name <APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings "SLACK_SIGNING_SECRET=<SLACK_SIGNING_SECRET>" "GIPHY_API_KEY=<GIPHY_API_KEY>"


e.g.

Shell
xxxxxxxxxx
1
 
1
az functionapp config appsettings set --name funcyapp --resource-group java-functions-group --settings "SLACK_SIGNING_SECRET=foobarb3062bd293b1a838276cfoobar" "GIPHY_API_KEY=foobarrOqMb5fvJdIuxTCr3WUDfoobar"

Please refer to the documentation on How to manage a function app in the Azure portal for details.

Update the Slack app

Head to your app's settings page, and then click the Slash Commands feature in the navigation menu. Edit the command and replace the value for the Request URL field with your function HTTP(s) endpoint

funcy Time!

From your workspace, invoke the command

/funcy <search term>

Since you can't go wrong with cats, try

/funcy cat

Don't worry if you see a Timeout error from Slack after the first invocation. This is due to the 'cold start' problem where the function takes a few seconds to bootstrap but Slack expects a response in 3 seconds. Just retry (a couple of times), and things should be fine.

Resources

The below mentioned resources were leveraged specifically for developing the demo app presented in this blog post, so you're likely to find them useful as well!

  • Azure Functions developers guide (general) and the Azure Functions Java developer guide
  • Quickstart on how to use Java to create and publish a function to Azure Functions
  • Maven Plugin for Azure Functions
  • How to code and test Azure Functions locally
  • How to manage connections in Azure Functions
  • Maven Plugins for Azure Services on GitHub

I really hope you enjoyed and learned something from this article!

API Slack (software) app azure Build (game engine)

Opinions expressed by DZone contributors are their own.

Related

  • Microsoft Azure Active Directory
  • Build Your Own AI Avatar App From Scratch in Less Than 8 Hours
  • The Technology Stack Needed To Build a Web3 Application
  • How To Use IBM App Connect To Build Flows

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!