Telegram Bot does not have the ability to be constructed in TypeScript

Why am I encountering this error message:

TypeError: node_telegram_bot_api_1.default is not a constructor

This is the TypeScript code snippet I have written:

import * as dotenv from 'dotenv';
dotenv.config({ path: __dirname + '/.env'})
console.log('Hello TypeScript')
import TelegramBot from 'node-telegram-bot-api';    
const bot = new TelegramBot(process.env.BOT_TOKEN, {polling: true});

Upon compiling, here is how my code output looks like:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = require("dotenv");
dotenv.config({ path: __dirname + '/.env' });
console.log('Hello TypeScript');
const node_telegram_bot_api_1 = require("node-telegram-bot-api");
const bot = new node_telegram_bot_api_1.default(process.env.BOT_TOKEN, { polling: true });

https://i.sstatic.net/PB7ro.jpg

Answer №1

After encountering a similar issue, I managed to resolve it by making the following adjustment:

    const TelegramBot = require('telegram-bot-api')

was swapped out for

    const { TelegramBot } = require('telegram-bot-api')

Answer №2

It appears that the import process was not done correctly. The documentation for node-telegram-bot-api specifies that the import should be carried out in the following manner:

const TelegramBot = require('node-telegram-bot-api');

This indicates that the entire module is being imported, which can be equivalent to an ES6 import like this:

import * as TelegramBot from 'node-telegram-bot-api';

If you need information on different import syntax and semantics, please visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering the issue of "TypeError: Cannot read property 'file' of undefined" when trying to upload a video with Multer

The objective is to select a video file from the desktop and upload it to the platform. The uploaded video file will then be automatically posted to a specified Youtube channel using GCD. An error message I encountered is:"react-dom.development.js:40 ...

Unusual occurrences when making several ajax requests to a single URL

I've encountered a peculiar scenario while working on a CherryPy server, and I'm seeking assistance in understanding the intricacies behind it. Here's the content of server.py: import cherrypy import os import threading class Root(object): ...

When attempting to utilize vue-resource, I encountered the error message: "Uncaught TypeError: window.Vue.use is not defined."

As a newcomer to Vue.js, I recently attempted to incorporate vue-resource into my project only to encounter the following error: Uncaught TypeError: window.Vue.use is not a function at vue-resource.js:1469 at vue-resource.js:10 at vue-resource.js: ...

Interacting with Node JS by submitting a request and obtaining a response

Hey there, I'm just starting out with Node JS and trying to grasp how client-server communication works. Here is the file on the server (express.js): app.post('/action', (req, res) => { const status = action.doAction(req); }); ...

Guide to displaying JSON.stringify data in PHP

I am attempting to retrieve my Google contact list using the Contact API. I have successfully retrieved the result and it is displaying in the console of both Chrome and Firefox browsers. However, I want to print this data using PHP on the same page. < ...

What techniques can I employ in Angular to develop engaging widgets using plain HTML, without any Angular-specific elements?

I am currently developing a cutting-edge Angular application that serves as a training platform to offer online courses to users. Each course is essentially a set of "slides," which are HTML partials through which the user can navigate in order. These sli ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

What strategies can I employ to address this historical issue?

Encountered TypeError: (0 , history_history__WEBPACK_IMPORTED_MODULE_6_.default) is not a function This issue arises in my history.js file import { createBrowserHistory } from 'history'; export default createBrowserHistory({ forceRefresh: tr ...

Clicking on ajax will disable the submit button and then re-enable it

Hey everyone, I've got this function that's working great but I'm encountering a small problem. $(document).ready(function(){ $("input[type='submit']").attr("disabled", false); $("form").submit(function(){ $("input[type='s ...

What is the best way to dynamically populate a dropdown menu in JavaScript?

I am currently working on populating a dropdown list. Initially, I had manually entered each variable into the array, but now I would like to use a loop to add items to the array dynamically. As a beginner in javascript, any help or pointers would be grea ...

Prevent displaying page confirmation prompts when a user clicks on hyperlinks

One way to display a confirmation message when a user tries to leave the current page is by using this method: window.addEventListener("beforeunload", function (e) { var confirmationMessage = "Are you sure you want to leave?"; ...

Display hyperlink depending on spinner value

I stumbled upon this JavaScript spinner that displays a countdown feature, which I find quite interesting. The spinner counts down from 15 seconds. I'm curious if it's feasible to customize it so that if you land on a specific category like geogr ...

Is there a way to showcase a Bootstrap popover using HTML content sourced from a GridView?

I've spent the last couple of hours experimenting with this, trying different things. I can successfully display a popover with basic title and content, but I'm struggling to make it work with HTML and Eval() as the content. Here's my curren ...

Using ES6 proxy to intercept ES6 getter functions

I have implemented a proxy to track all property access on instances of a class, demonstrated in the code snippet below: class Person { public ageNow: number; public constructor(ageNow: number) { this.ageNow = ageNow; const proxy = new Proxy( ...

Unable to visualize object in three.js

Looking to generate cubes in a random location on the page using a for loop, but currently experiencing issues where the cubes are not appearing as expected. **Note: When I check the cube with console log, this is the output: ** -cube.js:24 Mesh {uuid ...

Managing multiple to-do lists within a React application using parent-child relationships

ReactJS has a flaw that I've come across while trying to create a Todo application using Redux for state management. The issue arises when dealing with nested JSON data, such as having parent and child nodes in the response from a database query. Red ...

Streaming HTTP content on a domain secured with HTTPS using HTML5

Is it possible to stream HTTP on an HTTPS domain without triggering browser security errors? By default, the browser tends to block such requests. I rely on the hls.js library for desktop support with .m3u8 files. When I play content directly (on mobile o ...

Attempting to set up an Ajax webform with various outputs, but encountering issues with functionality

I'm encountering an issue while creating interactive exercises. I want to display correct or incorrect answers immediately after submission using JSON for retrieving responses, as suggested in a forum. However, my AJAX code isn't working at all. ...

What causes $(this) to stop functioning post successful execution?

Here are the code snippets I'm currently working with: $(this).removeClass('page larger').addClass('current'); $(this).siblings().removeClass('current').addClass('page larger'); Interestingly, when I try to pl ...

"Exploring the power of Node.js Virtual Machines and the magic of

I'm currently facing a challenge with reading and extracting data from a dynamically generated HTML file that contains a commented out JavaScript object. My goal is to retrieve this object as a string and execute it using VM's runInNewContext(). ...