Help needed: My Telegram bot refuses to start

Hey everyone! I’m new to coding and I’m trying to create my first Telegram bot using Node.js. I’ve set up a basic project, but I’m running into some issues when trying to start it up. Here’s what I’ve done so far:

  1. Created a new project and ran npm init -y
  2. Installed telegraf (version 3.39) and nodemon
  3. Set up a simple package.json file
  4. Created an index.js file with some basic bot code

Here’s my package.json:

{
  "projectName": "myFirstTelegramBot",
  "version": "0.1.0",
  "main": "bot.js",
  "scripts": {
    "dev": "nodemon bot.js"
  },
  "dependencies": {
    "nodemon": "^3.0.3",
    "telegraf": "3.39"
  }
}

And here’s my bot.js:

const { TelegramBot } = require('telegraf');
const myBot = new TelegramBot('bot_token_here');

myBot.command('hello', (ctx) => {
    console.log(ctx);
});

myBot.start();

When I try to run it using npm run dev, I get an error. I’ve tried using different Node.js versions (16.10.0 and 20.11.0), but no luck. Any ideas what might be causing this? Thanks in advance for your help!

I’ve encountered similar issues when starting out with Telegram bots. From what I can see, there are a couple of things that might be causing your problem.

First, as Liam mentioned, the import statement needs to be corrected. Instead of ‘TelegramBot’, you should use ‘Telegraf’. Also, the method to create a new bot instance is slightly different.

Here’s how I’d modify your bot.js:

const { Telegraf } = require('telegraf');
const bot = new Telegraf('YOUR_BOT_TOKEN');

bot.command('hello', (ctx) => {
    console.log(ctx);
});

bot.launch();

Make sure to replace ‘YOUR_BOT_TOKEN’ with the actual token you received from BotFather.

Another thing to check is your package.json. The ‘main’ field should point to ‘bot.js’ instead of ‘index.js’.

If you’re still encountering issues after these changes, it might be helpful to share the specific error message you’re receiving. That would give us more insight into what might be going wrong.

I noticed a potential issue with your setup. The ‘main’ field in your package.json points to ‘bot.js’, but your script is trying to run ‘index.js’. This mismatch could cause problems. Try changing your ‘dev’ script to ‘nodemon bot.js’ to match your file name.

Also, as others have mentioned, your import statement needs adjustment. The correct syntax is:

const { Telegraf } = require(‘telegraf’);
const bot = new Telegraf(‘YOUR_BOT_TOKEN’);

Lastly, ensure you’re using the latest version of Telegraf. Version 3.39 is quite old. Consider updating to the latest stable release for better compatibility and features.

If you’re still facing issues after these changes, could you share the specific error message you’re encountering? That would help in diagnosing the problem further.

hey there! looks like ur using the wrong import. try changing ‘TelegramBot’ to ‘Telegraf’ in ur bot.js file. also, make sure u replace ‘bot_token_here’ with ur actual token from botfather. hope this helps! lmk if u need more help :slight_smile: