Introduction

To develop and host an X (Twitter) bot on Square Cloud, you should follow a specific sequence of configurations and prerequisites. This technical guide covers the entire process, from initial setup to production deployment.

Essential Prerequisites

  • Active X (Twitter) account: Required for authentication and bot operation. If you don’t have an account, sign up at the official X website.
  • X Developer account: Required to access the APIs. Request access through the X Developer Portal.
  • Active Square Cloud account: Hosting platform for your application. Sign up via the signup page using your email.
  • Active paid plan: Ensures dedicated resources and optimized performance. Check our available plans and choose the one that suits your needs.
1

Accessing the Developer Portal

  1. Go to the X Developer Portal.
  2. Log in with your X (Twitter) account.
  3. If this is your first time, complete the developer access request process.
2

Project Creation

  1. In the dashboard, click “Create Project”.
  2. Choose a name for your project (e.g. “Square Cloud Bot”).
  3. Select the most appropriate use case (e.g. “Making a bot”).
  4. Provide a detailed description of your bot.
  5. Confirm project creation.
3

Application Setup

  1. Within the created project, click “Create App”.
  2. Define a unique name for your application.
  3. Confirm application creation.
  4. Note down the generated App ID for future reference.
4

API Keys Generation

  1. Navigate to the “Keys and tokens” section of your application.
  2. In the “Consumer Keys” section, click “Regenerate” to generate:
    • API Key (Consumer Key)
    • API Secret Key (Consumer Secret)
  3. Important: Copy and save these keys immediately, as you won’t be able to view them again.
5

Permissions Configuration

  1. Go to the “App permissions” section.
  2. Click “Edit” to modify permissions.
  3. Select “Read and write” to allow your bot to post tweets.
  4. If necessary, select “Read and write and Direct message” for DM functionality.
  5. Save the changes.
6

Access Tokens Generation

  1. Return to the “Keys and tokens” section.
  2. In the “Access Token and Secret” section, click “Generate”.
  3. Confirm token generation.
  4. Copy and save:
    • Access Token
    • Access Token Secret
  5. Warning: These tokens cannot be viewed again after closing the page.
7

Credentials Verification

  1. Confirm that you have all 4 required credentials:
    • API Key (Consumer Key)
    • API Secret Key (Consumer Secret)
    • Access Token
    • Access Token Secret
  2. Store these credentials in a secure location
  3. Important: Never share or expose these credentials publicly

Development Environment Setup

Node.js Installation and Setup

  1. Node.js check: Verify that Node.js is installed on your system. If not, download it from the official Node.js website.
  2. Project initialization: Create a new Node.js project by running:
Terminal
npm init -y
  1. Install dependencies: Install the libraries required for the bot:
Terminal
npm install twitter-api-v2

X (Twitter) Bot Implementation

  1. Environment variables setup: Create a .env file to store your credentials securely:
.env
API_KEY=your_api_key_here
API_SECRET_KEY=your_api_secret_key_here
ACCESS_TOKEN=your_access_token_here
ACCESS_TOKEN_SECRET=your_access_token_secret_here
  1. Create the main file: Develop the index.js file with the bot’s base structure:
index.js
// Import necessary modules
const { TwitterApi } = require('twitter-api-v2');

// Configure Twitter client with authentication
const client = new TwitterApi({
  appKey: process.env.API_KEY,
  appSecret: process.env.API_SECRET_KEY,
  accessToken: process.env.ACCESS_TOKEN,
  accessSecret: process.env.ACCESS_TOKEN_SECRET,
});

// Client with read and write permissions
const rwClient = client.readWrite;

// Function to check if the bot is working
async function verifyBot() {
  try {
    // Get authenticated user information
    const user = await rwClient.currentUser();
    console.log(`Bot successfully initialized! User: @${user.screen_name}`);
    return true;
  } catch (error) {
    console.error('Error verifying bot:', error);
    return false;
  }
}

// Function to post a tweet
async function postTweet(text) {
  try {
    const tweet = await rwClient.tweet(text);
    console.log(`Tweet posted successfully! ID: ${tweet.data.id}`);
    return tweet;
  } catch (error) {
    console.error('Error posting tweet:', error);
    throw error;
  }
}

// Function to respond to mentions
async function respondToMentions() {
  try {
    // Fetch recent mentions
    const mentions = await rwClient.userMentionTimeline({
      count: 10,
      result_type: 'recent'
    });
    
    for (const tweet of mentions.data) {
      // Check if it's a new mention (implement control logic)
      if (tweet.text.includes('!ping')) {
        // Reply to the mention
        await rwClient.reply(
          'Pong! 🤖 X Bot working correctly!',
          tweet.id
        );
        console.log(`Replied to mention from @${tweet.user.screen_name}`);
      }
    }
  } catch (error) {
    console.error('Error processing mentions:', error);
  }
}

// Function to search and interact with specific tweets
async function searchAndInteract(query) {
  try {
    // Search tweets with a specific query
    const tweets = await rwClient.search(query, {
      count: 5,
      result_type: 'recent'
    });
    
    for (const tweet of tweets.statuses) {
      // Like the tweet
      await rwClient.like(tweet.id_str);
      console.log(`Liked tweet from @${tweet.user.screen_name}`);
      
      // Wait a bit between actions to avoid rate limiting
      await new Promise(resolve => setTimeout(resolve, 2000));
    }
  } catch (error) {
    console.error('Error searching and interacting:', error);
  }
}

// Main bot function
async function runBot() {
  console.log('Starting X bot...');
  
  // Check if the bot is configured correctly
  const botOk = await verifyBot();
  if (!botOk) {
    console.error('Bot initialization failed');
    return;
  }
  
  // Example: Post an initialization tweet
  try {
    await postTweet('🤖 X Bot initialized and running on Square Cloud!');
  } catch (error) {
    console.log('Initialization tweet failed, but bot continues running');
  }
  
  // Main bot loop
  setInterval(async () => {
    try {
      // Check and respond to mentions every 5 minutes
      await respondToMentions();
      
      // Example: Search and interact with tweets about a specific topic
      // await searchAndInteract('#SquareCloud');
      
    } catch (error) {
      console.error('Error in main loop:', error);
    }
  }, 5 * 60 * 1000); // 5 minutes
  
  console.log('Bot running. Press Ctrl+C to stop.');
}

// Signal handling for graceful shutdown
process.on('SIGINT', () => {
  console.log('\nShutting down X bot...');
  process.exit(0);
});

process.on('SIGTERM', () => {
  console.log('\nShutting down X bot...');
  process.exit(0);
});

// Initialize the bot
runBot();

Square Cloud Configuration File

Technical documentation: Square Cloud configuration file

The squarecloud.app file is the core configuration of the application, defining key parameters such as the main file, resource allocation, runtime versions and important project metadata.

Configuration Example

Create the squarecloud.app file in your project root:
squarecloud.app
MAIN=index.js
MEMORY=512
VERSION=recommended
DISPLAY_NAME=X Bot
DESCRIPTION=Automated bot for X (Twitter) interactions

Advanced START Field Configuration

Technical warning: Use the START field only if you have advanced knowledge about custom startup scripts and their implications.
The START field in the Square Cloud configuration file is optional and should be used only when custom startup scripts are required. For the standard implementation shown in this tutorial, this field is not necessary.

Environment Variables Configuration on Square Cloud

Security: Never include your API credentials directly in code. Always use environment variables on Square Cloud.
On Square Cloud, configure the following environment variables via the control panel:
  • API_KEY: Your X API key
  • API_SECRET_KEY: Your X API secret key
  • ACCESS_TOKEN: Your access token
  • ACCESS_TOKEN_SECRET: Your access token secret

Deploy and Hosting on Square Cloud

After preparing your project files, proceed with the upload using one of the available methods:
Go to the Square Cloud Dashboard and upload your project files through the web interface.

Advanced Bot Features

Hashtag Monitoring

// Function to monitor specific hashtags
async function monitorHashtags(hashtags) {
  for (const hashtag of hashtags) {
    try {
      const tweets = await rwClient.search(`#${hashtag}`, {
        count: 5,
        result_type: 'recent'
      });
      
      // Process found tweets
      for (const tweet of tweets.statuses) {
        console.log(`Tweet found with #${hashtag}: ${tweet.text}`);
        // Implement interaction logic
      }
    } catch (error) {
      console.error(`Error monitoring #${hashtag}:`, error);
    }
  }
}

Scheduled Posts

// Function to schedule posts
function schedulePost(text, delay) {
  setTimeout(async () => {
    try {
      await postTweet(text);
      console.log('Scheduled post published successfully!');
    } catch (error) {
      console.error('Error publishing scheduled post:', error);
    }
  }, delay);
}

// Example: Schedule a post for 1 hour
schedulePost('🤖 Scheduled post by the bot!', 60 * 60 * 1000);

Best Practices and Considerations

Rate Limiting

X (Twitter) has strict rate limits. Implement controls to avoid exceeding these limits:
// Rate limiting control
const rateLimiter = {
  lastRequest: 0,
  minInterval: 1000, // 1 second between requests
  
  async wait() {
    const now = Date.now();
    const timeSinceLastRequest = now - this.lastRequest;
    
    if (timeSinceLastRequest < this.minInterval) {
      const waitTime = this.minInterval - timeSinceLastRequest;
      await new Promise(resolve => setTimeout(resolve, waitTime));
    }
    
    this.lastRequest = Date.now();
  }
};

Error Handling

// Helper function to retry an operation on failure
async function retryOperation(operation, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await operation();
    } catch (error) {
      console.error(`Attempt ${i + 1} failed:`, error.message);
      
      if (i === maxRetries - 1) {
        throw error;
      }
      
      // Wait before retrying
      await new Promise(resolve => setTimeout(resolve, 2000 * (i + 1)));
    }
  }
}

Additional Technical Resources

To deepen your knowledge about developing X bots using twitter-api-v2, check the official twitter-api-v2 library documentation. The documentation provides detailed guides, advanced tutorials and full API references to maximize your implementation. Also see: 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.