- Notifications
You must be signed in to change notification settings - Fork24
Async Telegram Bot API Client implement in@nim-lang
License
ba0f3/telebot.nim
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A powerful, asynchronous Telegram Bot API client implemented in Nim. Create feature-rich Telegram bots with ease using this modern, efficient library.
- ✨Full Telegram Bot API Support: Implements the complete Telegram Bot API, covering all methods and types for creating versatile bots.
- 🚀Asynchronous Design: Built with Nim's
async/await
for non-blocking operations, ensuring high performance and responsiveness for handling multiple bot interactions concurrently. - 💪Type-Safe API: Leverages Nim's strong typing system to provide a robust and predictable API, reducing runtime errors and improving code maintainability.
- 🛠️Rich Set of Utilities: Includes a variety of utility functions and helpers for common bot development tasks, such as keyboard markup generation, input file handling, and more.
- 📦Easy Integration: Designed for straightforward integration into existing Nim projects, with clear and concise API.
- 🔒Secure by Default: Encourages secure bot development practices, with features like proxy support and recommendations for secure API token handling.
- Nim 1.6.2 or higher
- A Telegram Bot API token (Get it from@BotFather)
Install using Nimble:
nimble install telebot
- Create a new bot with@BotFather
- Save your API token in a secure location
- Create your first bot:
This example demonstrates a basic echo bot that replies to text messages and greets users when they send the/start
command.
import telebot, asyncdispatch, logging, options, strutilsvar L=newConsoleLogger(fmtStr="$levelname, [$time]")addHandler(L)# Remember to strip your secret key to avoid HTTP errorsconstAPI_KEY=strip(slurp("secret.key"))procupdateHandler(bot:TeleBot, update:Update):Future[bool] {.async.}=ifnot update.message.isNil:let response= update.messageif response.text.len>0:let text= response.textecho"Received message:"& text&" from:"& response.chat.id.stringdiscardawait bot.sendMessage(response.chat.id,"Echo:"& text, parseMode="markdown", disableNotification=true, replyParameters=ReplyParameters(messageId: response.messageId))returntrueprocstartCommandHandler(bot:TeleBot, command:Command):Future[bool] {.async.}=ifnot command.message.fromUser.isNil:let userName= command.message.fromUser.firstNamediscardawait bot.sendMessage(command.message.chat.id,"Hello"& userName&"! Welcome to the Echo Bot.\\n\\nSend me any text and I will echo it back to you.", parseMode="markdown", disableNotification=true, replyParameters=ReplyParameters(messageId: command.message.messageId))returntruewhenisMainModule:whendefined(local):let bot=newTeleBot(API_KEY,"http://127.0.0.1:8081")# For local API server testingelse:let bot=newTeleBot(API_KEY) bot.setLogLevel(levelDebug)# Enable debug logging for development bot.onUpdate(updateHandler) bot.onCommand("start", startCommandHandler)# Register /start command handlerecho"Bot started. Polling for updates..." bot.poll(timeout=300)
import telebot, asyncdispatchconstAPI_KEY="YOUR_BOT_API_TOKEN"# Replace with your actual API keyprocphotoHandler(bot:TeleBot, command:Command):Future[bool] {.async.}=discardawait bot.sendPhoto(command.message.chat.id,"https://www.telegram-bot-api.com/logo.png", caption="Telegram Bot API Logo")returntruelet bot=newTeleBot(API_KEY)bot.onCommand("photo", photoHandler)bot.poll()
import telebot, asyncdispatchconstAPI_KEY="YOUR_BOT_API_TOKEN"# Replace with your actual API keyprocinlineKeyboardHandler(bot:TeleBot, command:Command):Future[bool] {.async.}=let inlineKeyboard=newInlineKeyboardMarkup(inlineKeyboardRows(inlineKeyboardButtonRow(newInlineKeyboardButtonUrl("Go to Google","https://google.com")) ))discardawait bot.sendMessage(command.message.chat.id,"Click the button below:", replyMarkup= inlineKeyboard)returntruelet bot=newTeleBot(API_KEY)bot.onCommand("keyboard", inlineKeyboardHandler)bot.poll()
import telebot, asyncdispatchconstAPI_KEY="YOUR_BOT_API_TOKEN"# Replace with your actual API keyproccallbackQueryHandler(bot:TeleBot, callbackQuery:CallbackQuery):Future[bool] {.async.}=discardawait bot.answerCallbackQuery(callbackQuery.id, text="Button clicked!")discardawait bot.sendMessage(callbackQuery.message.chat.id,"Callback query data:"& callbackQuery.data)returntrueprocinlineKeyboardCallbackHandler(bot:TeleBot, command:Command):Future[bool] {.async.}=let inlineKeyboard=newInlineKeyboardMarkup(inlineKeyboardRows(inlineKeyboardButtonRow(newInlineKeyboardButtonCallbackData("Click me","button_clicked")) ))discardawait bot.sendMessage(command.message.chat.id,"Click the button below:", replyMarkup= inlineKeyboard) bot.onCallbackQuery(callbackQueryHandler)# Register callback query handlerreturntruelet bot=newTeleBot(API_KEY)bot.onCommand("callback", inlineKeyboardCallbackHandler)bot.poll()
- Timeouts: Customize request timeouts using the
timeout
parameter - Proxy Support: Configure proxy settings for bot connections
- Parse Mode: Set default parse mode for messages
- API Server: Use custom API server if needed
let bot=newTeleBot(API_KEY,timeout=500,# timeout in secondsproxy="http://proxy.example.com:8080",parseMode="markdown")
For production environments, webhooks are recommended over polling:
# Configure webhookawait bot.setWebhook(url="https://your-domain.com/webhook",certificate="path/to/cert.pem"# Optional)# Start webhook serverbot.startWebhook(host="0.0.0.0",port=8443,certificate="path/to/cert.pem",privateKey="path/to/private.key")
Implement robust error handling in your bots:
try:discardawait bot.sendMessage(chatId,"Hello!")exceptTelegramErroras e:error"Failed to send message:", e.messageexceptExceptionas e:error"Unexpected error:", e.msg
- Security:
- Store API tokens securely
- Validate all user input
- Use HTTPS for webhooks
- Performance:
- Implement rate limiting
- Use webhook mode for production
- Handle updates asynchronously
- Reliability:
- Implement proper error handling
- Add logging for debugging
- Use persistent storage for important data
Contributions are welcome! Please read ourContributing Guidelines before submitting your:
- Bug reports 🐛
- Feature requests ✨
- Pull requests 🚀
- Documentation improvements 📝
SeeCHANGELOG.md for a list of changes and migration guides.
This project is licensed under the MIT License - see theLICENSE file for details.
- Report bugs onGitHub Issues
- Star ⭐ the project to show your support!
- Documentation was improved with the help of Cline bot, powered by OpenRouter's Google: Gemini 2.0 Flash Thinking Experimental 01-21 (free).
Note: From version 1.0.0, procs like
newMessage
,newPhoto
, etc. are deprecated. UsesendMessage
,sendDocument
instead.For backward compatibility, importtelebot/compat
.