-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Kaze edited this page Dec 27, 2025
·
2 revisions
const TelegramBot = require('teh-bot');
const bot = new TelegramBot('YOUR_BOT_TOKEN', { polling: true });{
polling: true, // Enable long polling
pollingInterval: 1000, // Polling interval in ms
pollingTimeout: 30, // Long polling timeout in seconds
webhook: false, // Enable webhook mode
webhookPort: 3000, // Webhook server port
webhookPath: '/webhook', // Webhook URL path
requestTimeout: 30000, // HTTP request timeout in ms
allowedUpdates: [] // Array of update types to receive
}Modern Unified Messaging (Baileys Style) Implementation
The sendMessage method is now unified. You can send text or media by passing an object.
// Send text
await bot.sendMessage(chatId, 'Hello world!')
// Send Image
await bot.sendMessage(chatId, {
image: './photo.jpg',
caption: 'Check this out!'
})
// Send Video
await bot.sendMessage(chatId, {
video: fs.createReadStream('./clip.mp4'),
caption: 'A cool video'
})
// Send Buffer
await bot.sendMessage(chatId, {
document: Buffer.from('hello'),
filename: 'test.txt'
})await bot.getMe()-
Text:
await bot.sendMessage(chatId, 'Hello!', { parse_mode: 'HTML' })Implementation -
Photo:
await bot.sendPhoto(chatId, photo, { caption: 'Photo' })Implementation -
Document:
await bot.sendDocument(chatId, document)Implementation -
Audio:
await bot.sendAudio(chatId, audio)Implementation -
Video:
await bot.sendVideo(chatId, video)Implementation -
Animation:
await bot.sendAnimation(chatId, gif)Implementation -
Voice:
await bot.sendVoice(chatId, voice)Implementation -
Video Note:
await bot.sendVideoNote(chatId, videoNote)Implementation -
Sticker:
await bot.sendSticker(chatId, sticker)Implementation -
Location:
await bot.sendLocation(chatId, lat, lon)Implementation -
Venue:
await bot.sendVenue(chatId, lat, lon, title, address)Implementation -
Contact:
await bot.sendContact(chatId, phone, firstName)Implementation -
Poll:
await bot.sendPoll(chatId, question, ['A', 'B', 'C'])Implementation -
Dice:
await bot.sendDice(chatId, { emoji: '🎲' })Implementation -
Chat Action:
await bot.sendChatAction(chatId, 'typing')Implementation
-
Edit Text:
await bot.editMessageText(text, { chat_id, message_id })Implementation -
Edit Caption:
await bot.editMessageCaption({ chat_id, message_id, caption })Implementation -
Edit Markup:
await bot.editMessageReplyMarkup({ chat_id, message_id, reply_markup })Implementation -
Delete:
await bot.deleteMessage(chatId, messageId)Implementation -
Forward:
await bot.forwardMessage(toChatId, fromChatId, messageId)Implementation -
Copy:
await bot.copyMessage(toChatId, fromChatId, messageId)Implementation
-
Get Chat:
await bot.getChat(chatId)Implementation -
Get Admins:
await bot.getChatAdministrators(chatId)Implementation -
Member Count:
await bot.getChatMemberCount(chatId)Implementation -
Get Member:
await bot.getChatMember(chatId, userId)Implementation -
Set Title:
await bot.setChatTitle(chatId, title)Implementation -
Set Description:
await bot.setChatDescription(chatId, description)Implementation -
Pin Message:
await bot.pinChatMessage(chatId, messageId)Implementation -
Unpin Message:
await bot.unpinChatMessage(chatId)Implementation -
Unpin All:
await bot.unpinAllChatMessages(chatId)Implementation -
Leave Chat:
await bot.leaveChat(chatId)Implementation -
Ban Member:
await bot.banChatMember(chatId, userId)Implementation -
Unban Member:
await bot.unbanChatMember(chatId, userId)Implementation -
Restrict Member:
await bot.restrictChatMember(chatId, userId, permissions)Implementation -
Promote Member:
await bot.promoteChatMember(chatId, userId, { can_manage_chat: true })Implementation
-
Get File:
const file = await bot.getFile(fileId)Implementation -
Download File:
await bot.downloadFile(fileId, './path/to/save.ext')Implementation
-
Answer Callback:
await bot.answerCallbackQuery(callbackQueryId, { text: 'Done!' })Implementation -
Answer Inline:
await bot.answerInlineQuery(inlineQueryId, results)Implementation
-
Start Polling:
await bot.startPolling()Implementation -
Stop Polling:
await bot.stopPolling()Implementation -
Set Webhook:
await bot.setWebhook('https://your-domain.com/webhook')Implementation -
Delete Webhook:
await bot.deleteWebhook()Implementation -
Webhook Info:
await bot.getWebhookInfo()Implementation
bot.command('start', async (ctx) => {
await ctx.reply('Hello!')
})
// Multiple aliases
bot.command(['help', 'info'], async (ctx) => {
await ctx.reply('Help text')
})// All updates
bot.on('update', (update) => {})
// Messages
bot.on('message', (message, ctx) => {})
bot.on('text', (message, ctx) => {})
bot.on('photo', (message, ctx) => {})
bot.on('document', (message, ctx) => {})
bot.on('video', (message, ctx) => {})
bot.on('audio', (message, ctx) => {})
bot.on('voice', (message, ctx) => {})
bot.on('sticker', (message, ctx) => {})
bot.on('location', (message, ctx) => {})
bot.on('contact', (message, ctx) => {})
// Edits
bot.on('edited_message', (message, ctx) => {})
// Channels
bot.on('channel_post', (message, ctx) => {})
bot.on('edited_channel_post', (message, ctx) => {})
// Queries
bot.on('callback_query', (query, ctx) => {})
bot.on('inline_query', (query, ctx) => {})
bot.on('chosen_inline_result', (result, ctx) => {})
// Polls
bot.on('poll', (poll, ctx) => {})
bot.on('poll_answer', (answer, ctx) => {})
// Chat members
bot.on('my_chat_member', (member, ctx) => {})
bot.on('chat_member', (member, ctx) => {})
// System events
bot.on('polling_start', () => {})
bot.on('polling_stop', () => {})
bot.on('polling_error', (error) => {})
bot.on('webhook_start', (port) => {})
bot.on('webhook_stop', () => {})
bot.on('webhook_error', (error) => {})
bot.on('error', (error) => {})bot.use(async (ctx, next) => {
console.log('Before')
await next()
console.log('After')
})
// Auth middleware
bot.use(async (ctx, next) => {
if (authorizedUsers.includes(ctx.from?.id)) {
await next()
} else {
await ctx.reply('Not authorized')
}
})-
Context Creation:
bot._createContextImplementation
{
update, // Full update object
bot, // Bot instance
message, // Message object
callbackQuery, // Callback query
inlineQuery, // Inline query
chat, // Chat object
from, // User object
chatId, // Chat ID
// Helper methods
send: async (content, options) => {}, // Unified sender (Baileys style)
reply: async (text, options) => {}, // Automatic reply-to-message
replyWithPhoto: async (photo, options) => {},
editMessageText: async (text, options) => {},
answerCallbackQuery: async (options) => {},
deleteMessage: async () => {}
}const keyboard = TelegramBot.InlineKeyboard()
.text('Button 1', 'callback_1')
.text('Button 2', 'callback_2')
.row()
.url('Website', 'https://example.com')
.row()
.switchInline('Search', 'query')
.build()
await bot.sendMessage(chatId, 'Choose:', { reply_markup: keyboard })-
.text(text, callbackData)- Regular button -
.url(text, url)- URL button -
.login(text, loginUrl)- Login button -
.switchInline(text, query)- Switch to inline mode -
.switchInlineCurrent(text, query)- Switch to inline in current chat -
.game(text)- Game button -
.pay(text)- Payment button -
.row()- Start new row -
.build()- Build keyboard
const keyboard = TelegramBot.ReplyKeyboard()
.text('Option 1')
.text('Option 2')
.row()
.requestContact('Share Contact')
.row()
.requestLocation('Share Location')
.resize()
.oneTime()
.build()
await bot.sendMessage(chatId, 'Choose:', { reply_markup: keyboard })-
.text(text)- Regular button -
.requestContact(text)- Request contact -
.requestLocation(text)- Request location -
.requestPoll(text, type)- Request poll -
.row()- Start new row -
.resize(bool)- Resize keyboard -
.oneTime(bool)- One-time keyboard -
.selective(bool)- Selective keyboard -
.placeholder(text)- Input placeholder -
.build()- Build keyboard
await bot.sendMessage(chatId, 'Removed', {
reply_markup: TelegramBot.RemoveKeyboard()
})await bot.sendMessage(chatId, 'Reply to this:', {
reply_markup: TelegramBot.ForceReply()
}){ parse_mode: 'Markdown' }
{ parse_mode: 'MarkdownV2' }
{ parse_mode: 'HTML' }<b>bold</b>
<i>italic</i>
<u>underline</u>
<s>strikethrough</s>
<code>code</code>
<pre>preformatted</pre>
<a href="url">link</a>*bold*
_italic_
`code`
```preformatted```
[link](url)
{
parse_mode: 'HTML',
disable_web_page_preview: true,
disable_notification: true,
protect_content: true,
reply_to_message_id: messageId,
allow_sending_without_reply: true,
reply_markup: keyboard
}await bot.sendChatAction(chatId, action)Available actions:
typingupload_photorecord_videoupload_videorecord_voiceupload_voiceupload_documentchoose_stickerfind_locationrecord_video_noteupload_video_note
bot.on('error', (error) => {
console.error('Bot error:', error)
})
try {
await bot.sendMessage(chatId, 'Hello')
} catch (error) {
if (error.code === 403) {
console.log('Bot blocked by user')
} else if (error.code === 429) {
console.log('Rate limited')
}
}await bot.sendPhoto(chatId, 'AgACAgIAAxkBAAI...')await bot.sendPhoto(chatId, 'https://example.com/image.jpg')await bot.sendPhoto(chatId, '/path/to/image.jpg')await bot.sendPhoto(chatId, buffer)The bot automatically handles rate limits with smart retry logic. No manual intervention needed.
process.on('SIGINT', () => {
bot.stopPolling()
process.exit(0)
})import TelegramBot, { Context, Message } from 'teh-bot'
const bot = new TelegramBot(token, { polling: true })
bot.on('text', (message: Message, ctx: Context) => {
ctx.reply('Hello!')
})- Use webhooks in production (more efficient than polling)
- Batch operations when possible
- Use middleware for common tasks
- Enable connection pooling (automatic)
- Set appropriate timeouts
- Always handle errors
- Validate user input
- Use environment variables for tokens
- Implement graceful shutdown
- Use TypeScript for better type safety
- Log errors and important events
- Use middleware for authentication
- Keep bot logic modular
- Test with real Telegram accounts
- Monitor rate limits
const TelegramBot = require('teh-bot')
const bot = new TelegramBot(process.env.BOT_TOKEN, { polling: true })
// Middleware
bot.use(async (ctx, next) => {
console.log(`[${ctx.from?.username}] ${ctx.message?.text}`)
await next()
})
// Commands
bot.command('start', async (ctx) => {
const keyboard = TelegramBot.InlineKeyboard()
.text('Help', 'help')
.text('About', 'about')
.build()
await ctx.reply('Welcome!', { reply_markup: keyboard })
})
// Events
bot.on('text', async (message, ctx) => {
await ctx.reply(`You said: ${message.text}`)
})
bot.on('callback_query', async (query, ctx) => {
await ctx.answerCallbackQuery({ text: 'Received!' })
})
// Error handling
bot.on('error', (error) => {
console.error('Error:', error)
})
// Graceful shutdown
process.on('SIGINT', () => {
bot.stopPolling()
process.exit(0)
})