> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shardcloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Host Telegram Bots

> Learn how to create and host Telegram bots on Shard Cloud with Node.js and Python examples.

## Introduction

This guide covers creating and deploying Telegram bots on Shard Cloud.

## Creating a Telegram Bot

<Steps>
  <Step title="Open BotFather">
    Open Telegram and search for [@BotFather](https://t.me/botfather).
  </Step>

  <Step title="Create New Bot">
    Send `/newbot` and follow the prompts to name your bot.
  </Step>

  <Step title="Get Token">
    BotFather will provide your bot token. **Save it securely**.
  </Step>
</Steps>

<Warning>
  **Security**: Keep your bot token secret. Anyone with it can control your bot.
</Warning>

## Creating Your Bot

<Tabs>
  <Tab title="Node.js (node-telegram-bot-api)">
    ### Setup

    ```bash theme={null}
    npm init -y
    npm install node-telegram-bot-api
    ```

    ### Basic Bot Code

    ```javascript index.js theme={null}
    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

    ```json package.json theme={null}
    {
      "name": "telegram-bot",
      "version": "1.0.0",
      "main": "index.js",
      "dependencies": {
        "node-telegram-bot-api": "^0.64.0"
      }
    }
    ```

    ### Shard Cloud Configuration

    ```systemd .shardcloud theme={null}
    DISPLAY_NAME=Telegram Bot
    DESCRIPTION=My Telegram Bot
    MAIN=index.js
    MEMORY=256
    VERSION=recommended
    ```
  </Tab>

  <Tab title="Python (python-telegram-bot)">
    ### Setup

    ```bash theme={null}
    pip install python-telegram-bot
    ```

    ### Basic Bot Code

    ```python main.py theme={null}
    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

    ```txt requirements.txt theme={null}
    python-telegram-bot
    ```

    ### Shard Cloud Configuration

    ```systemd .shardcloud theme={null}
    DISPLAY_NAME=Telegram Bot
    DESCRIPTION=My Telegram Bot
    MAIN=main.py
    MEMORY=256
    VERSION=recommended
    ```
  </Tab>
</Tabs>

## Deploying

<Steps>
  <Step title="Prepare Your Files">
    Ensure you have:

    * Your bot code file
    * `package.json` (Node.js) or `requirements.txt` (Python)
    * `.shardcloud`
  </Step>

  <Step title="Exclude Unnecessary Files">
    Remove: `node_modules/`, `__pycache__/`, `venv/`
  </Step>

  <Step title="Create ZIP Archive">
    Compress your project folder.
  </Step>

  <Step title="Upload to Shard Cloud">
    Go to [Shard Cloud Dashboard](https://shardcloud.app/dash/applications) and upload.
  </Step>
</Steps>

## Testing

After deployment, open Telegram, find your bot, and send `/ping`. It should reply with "Pong!".

## Additional Resources

* [Telegram Bot API](https://core.telegram.org/bots/api)
* [python-telegram-bot Docs](https://docs.python-telegram-bot.org/)

## Troubleshooting

<AccordionGroup>
  <Accordion title="Bot not responding">
    * Verify your bot token is correct
    * Check that polling is enabled
    * Review logs in the dashboard
  </Accordion>

  <Accordion title="Conflict error">
    Only one instance of your bot can use polling at a time. Stop other instances.
  </Accordion>
</AccordionGroup>
