examples/functions/telegram-notify/README.md
Content authors need to respond quickly to comments on their content.
This Sanity Function sends a Telegram notification when a comment is posted, with a helpful link to open the comment in your Sanity Studio.
comment document type containing:
comment field (string or text) for storing the comment contentThis function expects your schema to include a comment document type with:
comment field - A string or text field containing the comment contentcommentExample schema definition:
import {defineType} from 'sanity'
export const comment = defineType({
name: 'comment',
title: 'Comment',
type: 'document',
fields: [
{
name: 'comment',
title: 'Comment',
type: 'text', // or 'string'
validation: (rule) => rule.required(),
},
// Add other fields as needed
],
})
When a new comment type document is created, the function automatically:
comment type documentscomment field is defined (using filter)Result: Content authors get a notification with a link to the comment in the Sanity Studio.
Visit https://core.telegram.org/bots#how-do-i-create-a-bot for more information.
/newbot to @BotFather on Telegram to register your bot and receive its authentication token.<YOUR_BOT_TOKEN> with your bot tokenhttps://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates
The response will be a JSON object containing your Chat ID.
.env file in the root of your project.env fileTELEGRAM_BOT_TOKEN=<YOUR_BOT_TOKEN>
TELEGRAM_CHAT_ID=<YOUR_CHAT_ID>
STUDIO_URL=http://localhost:3333
Run this if you haven't initialized blueprints:
npx sanity blueprints init
You'll be prompted to select your organization and Sanity studio.
Then run:
npx sanity blueprints add function --example telegram-notify
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
import process from 'node:process'
const {TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID} = process.env
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
throw new Error('TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID must be set')
}
export default defineBlueprint({
// ...all other settings
resources: [
//...all other functions
defineDocumentFunction({
name: 'comment-telegram',
event: {
on: ['create'],
filter: '_type == "comment" && defined(comment)',
projection: '{_id, comment}',
},
env: {TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID},
}),
],
})
npm install
You can test the telegram-notify function locally using the Sanity CLI before deploying:
Test with the included sample document:
npx sanity functions test telegram-notify --file document.json
Start the development server for interactive testing:
npx sanity functions dev
Test with your own document data:
npx sanity functions test telegram-notify --data '{
"_type": "comment",
"_id": "test-comment-123",
"comment": "Loved this article, great read!"
}'
Capture a real document from your dataset:
# Export a real document for testing
npx sanity documents get "your-comment-id" > document.json
# Test with the real document
npx sanity functions test telegram-notify --file document.json
Once you've tested your function locally and are satisfied with its behavior, you can deploy it to production.
Important: Make sure you have the Deploy Studio permission for your Sanity project before attempting to deploy.
From your project root, run:
npx sanity blueprints deploy
This command will:
After deployment, if you're not using a .env file in your projects root, you need to add your Telegram credentials as environment variables:
npx sanity functions env add comment-telegram TELEGRAM_BOT_TOKEN "your-telegram-bot-token-here"
npx sanity functions env add comment-telegram TELEGRAM_CHAT_ID "your-chat-id-here"
npx sanity functions env add comment-telegram STUDIO_URL "https://your-studio.sanity.studio"
You can verify the environment variables were added successfully:
npx sanity functions env list comment-telegram
After deployment, you can verify your function is active by:
npx sanity functions logs comment-telegram