This is a demo Azure Function application which will update a Slack avatar with a random image from http://thispersondoesnotexist.com/.
- Slack Workspace and Account
- Azure Subscription
- Sample code from branch
single-token-poc
Slack has moved away from legacy API Tokens and requires developers to utilize a Slack App approach. A Slack App is setup in a specific Slack Workspace and can be shared with other workspaces, however for this demonstration we will only be focused on a personal installation in a single directory.
Slack works in an OAuth 2.0 setup where permissions are defined as scopes. For this application we need users.profile:write. This permission allows the application to update the user's profile information (i.e. name, email, etc.) as well as update or remove their avatar. We will utilize the users.setPhoto web request to do our work.
- Browse to https://api.slack.com/apps
- Click on "Create New App"
- Choose "From an app manifest" to continue
- Choose the appropriate workspace for this application
- Click Next
- Paste the contents of
slack-app/manifest.yml
into theYAML
tab - Click Next
- Review the setup information and click Confirm when you're satisfied
You may be prompted to "Install to Workspace". If so, click the button to do so and follow the prompts before continuing.
After your app is created, it will appear in your apps list at https://api.slack.com/apps. Open that app and browse to "OAuth & Permissions" in the menu. Copy the User OAuth Token
presented at the top of the page. It must begin with xoxp-
to be valid.
Our Azure Function App only needs the function app itself, a storage account, and an app service plan. We will utilize the consumption plan to keep costs extremely low.
You can utilize the portal to configure you're own Azure Function App, however we have provided a template you can use. You will need to change the names of the resources however.
- Create a Resource Group to use
- Open the Resource Group
- Click Create -> Custom Deployment
- Click "Build your own template in editor"
- Paste the contents of
azure/functionapp.json
into the text box and click Save - Click "Edit parameters"
- Paste the contents of
azure/functionapp.parameters.json
into the text box and click Save- This step can be skipped if you wish to edit the parameters directly in the portal instead
- Change the values of the Name, Location, Hosting Plan Name, Storage Account Name, or another parameter as required
- Click "Review + create"
- Confirm the parameters look correct and review the terms provided
- Click Create
In order to access the Slack API, we need to provide the User OAuth Token
gathered earlier. In a normal setup, this would be stored securely in an Azure Key Vault
, however this is a proof of concept and the app will be deleted soon so we will simply use the Function App Application Settings
to save it.
- Open the Function App in the Azure Portal
- Click Configuration on the left menu
- On the Application Settings tab, click "New application setting"
- Name:
SlackToken
- Value:
<Token Copied Earlier>
- Click OK
- Click Save
- Click Continue
There are many ways to deploy an Azure Function App. We are going to perform a manual deployment in this example.
- Build and Publish (prepares for publish) the function app locally:
dotnet publish
- Browse to
bin/Debug/5.0/publish
- Zip the contents of the folder
- Open the function App in the Azure Portal
- Click "Advanced Tools" in the menu
- Click Go to open a new tab
- Go to Tools -> Zip Push Deploy
- Drag and drop, or click Upload, the ZIP file you created earlier
- Confirm that
slack-avatar.dll
and other files are in the top-level folder, if they're not you may need to recreate the ZIP file - Go back to the Function App in the Azure Portal
- Click Functions in the menu
- Confirm
UpdateAvatar
is now in the list, you may need to wait 1 minute and click Refresh - Open
UpdateAvatar
- Click "Code + Test" in the menu
- Click "Test/Run" to trigger a test
- Verify the avatar has updated in Slack
In testing, the "Test/Run" button was not always accessible. You can utilize the manual run method. Here is a sample cURL request:
curl --request POST -H 'x-functions-key:[KEY]' -H "Content-Type:application/json" --data "{}" 'http://[FUNCAPP_NAME].azurewebsites.net/admin/functions/UpdateAvatar'