đđ»ââïž Join the Sanity.io community
This repository contains both the studio and the services for Sanityâs community bot. It makes it possible for the Sanity team to mark questions in the community slack with a đ« emoji to create a ticket and track conversations in threads, and the â to resolve it. By tagging tickets, this studio also works as a knowledge base to help community managers to find answers, and get insight to documentation improvements, feature requests, bug reports and so on.
Itâs used in production, but under active development.
Feel free to use this repository for inspiration, and youâre welcome to fork it. Weâre happy to answer questions about it in the community #help-channel, but we can't support your implementation or take feature requests.
- Track messages in Slack as tickets by marking them with the đ« emoji
- Mark tickets as done with the â emoji
- Keeps track of threads, with preview
- Scope ticket creation to an email domain
- Serverless single sign-on for Google Suite (requires enterprise plan, normal sign-on can also be used)
- Personalized desk structure
- Custom ticket tool for easy overview
- Custom input compoment for tags as references
- Serverless deployment on Vercel
This content model will evolve as we add more features.
People who log in are assigned a person
 document.
Fields:
- Name
- Nickname
- Sanity ID
- Github Username
- Image
- User avatar url
- Biography
When you put a đ«-emoji on a Slack message, a new ticket
 document is created and will keep track of the thread.
Fields:
- Permalink to slack message
- Short summary of the question
- Status [Open, Pending, Resolved, Cancelled]
- âNext actionâ: None, improve docs, feature request, or bug report
- Tags: Array of tags sourced from the
tagOption
 type - âSolved withâŠâ, URL to Github/Docs, and short summary
- Agent, reference to the
person
 on the case - Slack channel name
- Author name for the original message author
- âOpened byâ, the person placing the đ« emoji
- Thread: The contents of the slack thread
A title/value for a thematic tag that helps us group tickets for analysis and knowledge base
This app is deployed on Vercel. The easiest way to get up and running is the following
- Install Sanity and Vercel CLIs
npm install --global @sanity/cli vercel
or yarn
yarn global add @sanity/cli vercel
- Install dependencies in the project root folder
npm i
or yarn
yarn
- Initiate a new Sanity project.
sanity init
This will create a new project ID and reconfigure sanity.json
 for you. You should make the dataset Private.
- ENV variables
Open up the configuration for your new project and to to Settings -> API and create a write
token. Save it somewhere, you'll be pasting it into a file shortly.
sanity manage
Copy and edit the example .env file
mv .env.template .env
Set it up with these variable names and values. You can find project id and dataset name in sanity.json
SANITY_PROJECT_ID=<sanity-project-id>
SANITY_DATASET=<sanity-dataset>
SANITY_WRITE_TOKEN=<sanity write-token you just made>
Finally, add the secrets to your Now
account. The keys are located in the now.json
Nowfile.
now secrets add community-bot-sanity-project-id <sanity-project-id>
now secrets add community-bot-sanity-dataset <sanity-dataset>
now secrets add community-bot-sanity-sso-write-token <sanity-sso-write>
now secrets add community-bot-sanity-create-session-token <sanity-create-session>
now secrets add community-bot-google-oauth-secret <google-oauth-secret>
now secrets add community-bot-slack-bot-user-token <slack-bot-user>
now secrets add community-bot-sanity-email-domain <sanity-email-domain>
- Create Vercel project
Go to vercel.com/import and create a new project by selecting your Github repo with this app
You need to override the following Build and Development Settings:
- Build command:
sanity build public -y && cp login.html public/login.html
- Development command:
sanity start --port $PORT
- CORS
Add your Vercel app URL to your Sanity projectâs CORS origins settings. You'll need to allow authenticated requests to have the Studio work.
sanity cors add <app-url>
If you want to be able to login to PR deployments for the Studio, you can add a wildcard CORS origin. Be very careful. You donât want to set a CORS origin for a URL other people can create:
- Don't:
https://*.vercel.app
- Do:
https://*.your-org.vercel.app
orhttps://subdomain.your-domain.tld
To set up the Slack integration you'll need to go to api.slack.com/apps and create a new app there.
Put in some helpful info in the Display information settings.
Go to the OAuth & Permissions settings and give it the following Scopes:
-
Bot Token Scopes:
- channels:history
- channels:join
- channels:read
- emoji:read
- groups:history
- groups:read
- reactions:read
- users:read
- users:read.email
-
User Token Scopes:
- channels:history
- groups:history
-
Add the following environment variables to the
.env
 file: -
SLACK_BOT_USER_TOKEN
- This is the Bot User OAuth Access Token under OAuth & Permissions -
EMAIL_DOMAIN
- The (@)orgname.tld for email addresses in your org (e.g.sanity.io
to restrict ticket creation to the Sanity team)
Go to the Event Subscriptions settings and add the following URL: https://your-app-name.your-org.now.sh/api/events
(if you have a custom domain for your Vercel deployment, itâs https://your-custom-domain.tld/api/events
). The app needs to be deployed in order for Slack to verify this endpoint.
If you want to test the Slack integration when developing locally, you'll need to pipe your http://localhost:3000
 to a public endpoint. You can use ngrok to create a secure tunnel to your localhost. Youâll need to edit the Event Subscriptions URL to https://random-id-or-custom-name.ngrok.io/api/events
while you're testing. This will interupt the service if you have it production.
This studio comes with SSO integration for Google Suite out-of-the-box.
You need to set up OAuth on your Google Cloud account:
- Go to the OAuth consent screen on console.cloud.google.com and add the following:
- Application type: Internal (you probably donât want this to be public)
- Application name: An approriate name
- Application logo: Your orgâs logo
- Scopes for Google APIs:
- profile
- openid
- ../auth/analytics.readonly
- openid
- profile
- Authorized domains:
your-org.now.sh
(and if you have other domains you want to log in from) - Set
Application Homepage link
andApplication Privacy Policy link
to their approriate places if you have them, or just your homepage URL
Then go to the Credentials settings, and add a new OAuth 2.0 Client ID:
- Name: A regonizeable name so you know what it's for
- URIs:
https://your-app-name.your-org.now.sh
http://localhost:3000
- Authorized redirect URIs
https://your-app-name.your-org.now.sh
http://localhost:3000
The you'll need to copy-paste the Client Id and the Client secret and add it to your environment variables (.env
locally):
SANITY_CREATE_SESSION_TOKEN
- For SSO (requires a business or enterprise plan](https://www.sanity.io/contact)) Go to the API settings and create a token with âCreate sessionâ access.CLIENT_ID=<the google OAuth client ID>
(this isn't a secret and will be exposed publicly)CLIENT_SECRET=<the google OAuth client secret>
SANITY_STUDIO_URL=https://your-app-name.your-org.now.sh
Replace the value for client_id:
 in login.html
with your Googleâs OAuth client ID.
You can disable the SSO feature, and use the standard login options. You then need to create the person
 documents manually and add their Sanity ID
 to enable the âMy ticketsâ feature.
- Delete
api/verify-token.ts
(or rename toapi/_verify-token.ts
 to disable it) - Go to
config/@sanity/default-login.json
 and replace it with the following:
{
"providers": {
"mode": "append",
"redirectOnSingle": false,
"entries": []
}
}
To find the projectâs user ids, make sure you're logged in to the project (e.g. in manage.sanity.io), and open these URLs in your browser (it will use the cookieâs authentication token):
https://api.sanity.io/v1/projects/<projectId>
- ids in in themembers
array.- You can look up a member on this endpoint:
https://api.sanity.io/v1/projects/<projectId>/users/<userId>