Simeon Griggs
Customer Education at Sanity
Get instant Telegram notifications when new comments are posted, with direct links to your Sanity Studio.
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
],
})import {env} from 'node:process'
import {documentEventHandler} from '@sanity/functions'
const {TELEGRAM_BOT_TOKEN = '', TELEGRAM_CHAT_ID = '', STUDIO_URL = 'http://localhost:3333'} = env
export const handler = documentEventHandler(async ({event}) => {
const {_id, comment} = event.data
if (!comment) {
console.log('No comment in event data')
return
} else if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID) {
console.log('Environment variables not set')
return
}
try {
const message = `New comment received: ${comment}`
const studioUrl = `${STUDIO_URL}/structure/comment;${_id}`
const response = await fetch(`https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_id: TELEGRAM_CHAT_ID,
text: message,
reply_markup: {
inline_keyboard: [[{text: '📝 Open in Sanity Studio', url: studioUrl}]],
},
}),
})
if (!response.ok) {
throw new Error(`Telegram API error: ${response.status} ${response.statusText}`)
}
const result = await response.json()
console.log('Message sent successfully:', result)
} catch (error) {
console.error('Failed to send Telegram notification:', error)
throw error
}
})
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},
}),
],
})The Problem
Content authors need to respond quickly to comments on their content, but checking the Studio regularly for new comments creates workflow interruptions and delays response times.
The Solution
This Sanity Function sends a Telegram notification when a comment is posted, with a helpful link to open the comment in your Sanity Studio.
Quick Start
View the complete example and source code.
Initialize blueprints if you haven't already: npx sanity blueprints init
Then: npx sanity blueprints add function --example telegram-notify
Then deploy: npx sanity blueprints deploy
Telegram Setup More detailed instructions available in the GitHub README
/newbot to @BotFather on Telegram → Give it a name → Copy the bot tokenhttps://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates → Copy your chat ID from the JSON responseTELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, and STUDIO_URL to your .env fileVisit https://core.telegram.org/bots for more information.
How It Works When a content editor publishes a new comment, the function automatically:
Key Benefits
Perfect For
The function works with any Sanity project containing a comment document type and can be easily customized to target different chats, include additional comment metadata, or format messages differently.
Customer Education at Sanity
Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.
Go to First Published Timestamp FunctionAI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.
Go to Automatically tag blog postsOn document publish, send a Slack notification so your team is informed
Go to Notify your team via Slack on publishCreate, preview, and send Klaviyo campaigns without ever leaving Sanity Studio"
Go to Klaviyo campaigns without leaving your Studio