Randomize your Slack Avatar
July 13, 2021
By Drew Garrett
Randomize Your Slack Avatar with Azure Functions
I enjoy having some fun with my Slack avatars which are almost never a photo of myself. With inspiration from Phil Cryer's post last year, I decided to implement a random avatar using an Azure Function and images from https://thispersondoesnotexist.com/. The images provided are all AI generated and not real people. In the samples below, you can see there are some malformations that stand out.





Overall Requirements
In order to perform this work, we will need a Slack Workspace and account and an Azure Subscription. The app must be built using a Windows machine for the proof of concept to work. It is entirely possible to switch to another OS supported by .NET 5, however the Azure Function app will need to be modified accordingly. You will also be required to create a new Slack App as a part of this. This permission may be restricted by your Slack Workspace administrators.
Required Slack Permissions
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.
Install and Setup the Application
Get the Code
Our POC code is in the single-token-poc
branch.
git clone https://github.com/ocelotconsulting/randomize-avatar.git
cd randomize-avatar
git checkout single-token-poc
Slack Configuration
Install the Slack App
- 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.
Retrieve the Slack User Access Token
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.
Azure Infrastructure
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.
Setup the Azure Infrastructure
You can utilize the portal to configure your 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 requried
- Click "Review + create"
- Confirm the parameters look correct and review the terms provided
- Click Create
Configure the Function App
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
Deploy the Function App
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'
Slack Example
How does it look in Slack? Here's an example:
What's Next?
If you wish to modify how frequently the function app updates your avatar, edit UpdateAvatar.cs
and modify the TimerTrigger()
attribute to different values. This is a NCRONTAB expression which includes seconds. Only use the NCRONTAB format for now as you cannot use a TimeSpan
with a consumption app service plan.
Example CRONs:
0 0 * * * * - Hourly at 0 minutes
0 0,30 * * * * - Every 30 minutes
0 0 0 * * * - Daily at midnight (Timezone based on Function App, default is UTC)
0 0 * */2 * * - Hourly but only on even numbered days
It would be great to expand this function to accept multiple users in the same workspace with registration via the Slack UI instead of the API website. That may be for a future blog post.
Reminder to Cleanup Resources
As always, these are resources that cost money in Azure. Be sure to clean up the resources to prevent further billing once you're done using them. You should also delete the Slack App, or at a minimum revoke the User OAuth Token
for security purposes.
Part Two
Please check out part two of this series: expanding to multiple users and multiple tenants!