Build a Daily Hadith Telegram Bot
Learn how to build a Telegram bot that sends a random hadith every day to subscribers.
What You'll Build
- ✅ Telegram bot with commands
- ✅ Daily automated hadith delivery
- ✅ Multi-language support
- ✅ Subscriber management
Prerequisites
- Node.js 18+ installed
- Telegram account
- Mumin API key (get one here (opens in a new tab))
Create Telegram Bot
- Message @BotFather (opens in a new tab) on Telegram
- Send
/newbotand follow instructions - Save your bot token (looks like
123456:ABC-DEF...)
Setup Project
mkdir hadith-bot
cd hadith-bot
npm init -y
npm install grammy node-cron node-fetchCreate Bot Code
Create bot.js:
javascript
import { Bot } from 'grammy'
import cron from 'node-cron'
import fetch from 'node-fetch'
const bot = new Bot('YOUR_TELEGRAM_BOT_TOKEN')
const MUMIN_API_KEY = 'YOUR_MUMIN_API_KEY'
// Store subscribers
const subscribers = new Set()
// /start command
bot.command('start', async (ctx) => {
subscribers.add(ctx.chat.id)
await ctx.reply(
'🕌 As-salamu alaykum! I will send you a hadith every day at 8 AM. Commands: /today - Get today\'s hadith, /random - Get random hadith, /stop - Unsubscribe'
)
})
// /today command
bot.command('today', async (ctx) => {
const hadith = await getRandomHadith('en')
await ctx.reply(formatHadith(hadith))
})
// Fetch random hadith
async function getRandomHadith(lang) {
lang = lang || 'en'
const response = await fetch(
'https://api.mumin.ink/v1/hadiths/random?lang=' + lang,
{
headers: {
'Authorization': 'Bearer ' + MUMIN_API_KEY
}
}
)
return await response.json()
}
// Format hadith message
function formatHadith(hadith) {
const translation = hadith.translations.en
return (
'📖 _Hadith ' + hadith.number + ' from Book ' + hadith.book + '_' + '\n\n' +
translation.text + '\n\n' +
'_Narrator: ' + translation.narrator + '_' + '\n\n' +
'🕌 Sahih al-Bukhari'
)
}
// Daily cron job (8 AM every day)
cron.schedule('0 8 * * *', async () => {
console.log('Sending daily hadith...')
const hadith = await getRandomHadith('en')
const message = '🌅 *Daily Hadith* ' + formatHadith(hadith)
for (const chatId of subscribers) {
try {
await bot.api.sendMessage(chatId, message, { parse_mode: 'Markdown' })
} catch (error) {
console.error('Failed to send to ' + chatId + ':', error)
}
}
})
// Start bot
bot.start()
console.log('🤖 Bot is running...')Run Your Bot
node bot.jsTest It
- Find your bot on Telegram
- Send
/startto subscribe - Send
/todayto get a hadith immediately
Enhancements
💡 Pro Tips:
- Use a database (MongoDB/PostgreSQL) to store subscribers
- Add language selection per user
- Implement /search command
- Add hadith bookmarking
Multi-Language Support
javascript
// Language per user
const userLanguages = new Map()
bot.command('language', async (ctx) => {
await ctx.reply('Choose language:', {
reply_markup: {
inline_keyboard: [
[{ text: 'English 🇬🇧', callback_data: 'lang_en' }],
[{ text: 'Русский 🇷🇺', callback_data: 'lang_ru' }],
[{ text: 'Oʻzbekcha 🇺🇿', callback_data: 'lang_uz' }],
]
}
})
})
bot.on('callback*query:data', async (ctx) => {
const lang = ctx.callbackQuery.data.split('*')[1]
userLanguages.set(ctx.chat.id, lang)
await ctx.answerCallbackQuery()
await ctx.reply('✅ Language set to ' + lang)
})Deploy to Production
Using Railway
- Push code to GitHub
- Create Railway project
- Add environment variables:
TELEGRAM_BOT_TOKENMUMIN_API_KEY
- Deploy!
Full Source Code
Find the complete code on GitHub (opens in a new tab).