Introduction

To develop and host a Telegram bot on Square Cloud, you need to follow some essential prerequisites. This guide will present the complete process, from initial setup to final deployment.

Prerequisites

  • Active Telegram account: Essential for testing and interacting with your bot. If you don’t have one, visit the official Telegram website to create your account for free.
  • Square Cloud account: Required to host your application. Sign up through the registration page in just a few minutes.
  • Active paid plan: Ensures dedicated resources and optimized performance for your bot. Check our available plans and choose the one that best fits your needs.

Initial Bot Configuration

Getting Started

Access Telegram Web and search for the official BotFather bot, responsible for creating and managing bots on the platform.

Obtaining the Authentication Token

Start a conversation with BotFather and type /start. Then, select the /newbot option to create a new bot. The system will ask for a name for your bot and, after confirmation, will automatically generate the necessary authentication token.
Critical Security: Keep your bot token absolutely secret. This token grants full control over the bot and should be treated as confidential information.

Bot Development

The choice of library or framework depends on the programming language you intend to use. Below are some of the most popular options:
Environment Setup
  1. Verify that Node.js is installed on your system. Otherwise, download it from the official Node.js website.
  2. Initialize a new Node.js project:
Terminal
npm init -y
  1. Install the official library:
Terminal
npm install node-telegram-bot-api
Basic Implementation
  1. Create the main file (index.js) with the following structure:
index.js
// Import the node-telegram-bot-api library
const TelegramBot = require("node-telegram-bot-api");

// Authentication token configuration
const token = "YOUR_TOKEN_HERE";

// Bot initialization with polling enabled
const bot = new TelegramBot(token, { polling: true });

// Get bot information
bot.getMe().then((botInfo) => {
  // Extract bot name
  const botName = botInfo.username;

  // Define handler for non-command messages
  bot.on("message", (msg) => {
    // Extract chat ID
    const chatId = msg.chat.id;
    // Capture user message
    const userMsg = msg.text;
    // Format response
    const responseMsg = `${botName} responds: ${userMsg}`;
    // Send response message
    bot.sendMessage(chatId, responseMsg);
  });

  // Initialization confirmation log
  console.log(`Bot ${botName} started successfully!`);
});

Square Cloud Configuration File

Complete Documentation: Square Cloud Configuration File

The squarecloud.app file is the central configuration component of your application, defining essential parameters such as main file, system resources, versions, and project metadata.

Advanced START Field Configuration

Warning: Use the START field only if you have advanced technical knowledge about custom initialization scripts.
The START field in the Square Cloud configuration file is optional and should be used exclusively when custom initialization scripts need to be executed. For the examples presented in this tutorial, this field is not necessary.

Deployment to Square Cloud

After completely preparing your project files, proceed with the upload to Square Cloud following one of the methodologies below:
Access the Square Cloud Dashboard and upload your project files.

Testing and Validation

After successful deployment, locate your bot on Telegram and send a test message. The bot should respond by echoing your message, confirming the correct implementation functionality.

Additional Resources

To deepen your knowledge about Telegram bot development, consult the official telebot documentation. The documentation offers detailed guides, advanced tutorials, and complete API reference. If you continue facing technical difficulties, our specialized support team is available to assist you. Contact us and we'll be happy to help you resolve any issue.