Introduction
This guide covers creating and deploying Telegram bots on Shard Cloud.
Creating a Telegram Bot
Create New Bot
Send /newbot and follow the prompts to name your bot.
Get Token
BotFather will provide your bot token. Save it securely.
Security: Keep your bot token secret. Anyone with it can control your bot.
Creating Your Bot
Setup
npm init -y
npm install node-telegram-bot-api
Basic Bot Code
const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(token, { polling: true });
bot.on('message', (msg) => {
const chatId = msg.chat.id;
if (msg.text === '/start') {
bot.sendMessage(chatId, 'Hello! I am your bot.');
} else if (msg.text === '/ping') {
bot.sendMessage(chatId, 'Pong!');
}
});
console.log('Bot is running...');
Package.json
{
"name": "telegram-bot",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"node-telegram-bot-api": "^0.64.0"
}
}
Shard Cloud Configuration
DISPLAY_NAME=Telegram Bot
DESCRIPTION=My Telegram Bot
MAIN=index.js
MEMORY=256
VERSION=recommended
Setup
pip install python-telegram-bot
Basic Bot Code
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
TOKEN = 'YOUR_BOT_TOKEN'
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am your bot.')
async def ping(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Pong!')
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler('start', start))
app.add_handler(CommandHandler('ping', ping))
print('Bot is running...')
app.run_polling()
if __name__ == '__main__':
main()
Requirements.txt
Shard Cloud Configuration
DISPLAY_NAME=Telegram Bot
DESCRIPTION=My Telegram Bot
MAIN=main.py
MEMORY=256
VERSION=recommended
Deploying
Prepare Your Files
Ensure you have:
- Your bot code file
package.json (Node.js) or requirements.txt (Python)
.shardcloud
Exclude Unnecessary Files
Remove: node_modules/, __pycache__/, venv/
Create ZIP Archive
Compress your project folder.
Testing
After deployment, open Telegram, find your bot, and send /ping. It should reply with “Pong!”.
Additional Resources
Troubleshooting
- Verify your bot token is correct
- Check that polling is enabled
- Review logs in the dashboard
Only one instance of your bot can use polling at a time. Stop other instances.