1

I'm trying to set up a bot using the node-telegram-bot-API, I have created the bot instance as follows in Foo.js

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

const bot = new TelegramBot(config.telegram.TELEGRAM_TOKEN, {polling: true});
module.exports = bot;

I want the bot instance to be used with its methods in Bar.js as-

const bot = require('path/to/Foo');
bot.sendMessage(config.telegram.CHAT_ID, info.message);

However, it throws an error saying TypeError: bot.sendMessage is not a function

  • are you exporting anything else from `Foo.js`? – d_shiv Jun 24 '19 at 12:20
  • No, I am not exporting anything else. –  Jun 24 '19 at 12:21
  • Okay. You should check for circular dependency between `Foo.js` and `Bar.js`. Circular dependency would be the scenario where either both files require each other directly or indirectly. Try printing `bot` in `Bar.js`, if its an empty object, there almost certainly a circular dependency issue. – d_shiv Jun 24 '19 at 12:26
  • Yes, there might be circular dependency although there's no direct circular dependency. If this is the case, can you please tell how can I solve this? –  Jun 24 '19 at 12:31
  • Well the right thing to do would be to figure out the exact cycle in dependency graph. However temporarily to validate that this indeed is a circular dependency issue, you can try putting `require('path/to/Foo')` on first line of your `index.js` (the file which is starting point of application). Replace it with `require('path/to/Bar')`, if that doesn't work. Since the exported values are cached by NodeJS, this might just work, however, it seriously depends in nature of circular dependency. Do debug and remove the actual issue after validating. – d_shiv Jun 24 '19 at 12:44
  • I received an empty object upon printing `bot`. I tried putting `require('path/to/Bar')` but still it results the same error. And my **Starting point of application is** is `Foo.js` itself. –  Jun 24 '19 at 13:01
  • 2
    Oh okay. Where is `Bar.js` included from? If `Foo.js` is your starting point, then no matter what your dependency graph is like, when `Bar.js` requires `Foo.js` again, this will become a circular dependency. For example: Node starts `Foo.js`, which requires `FooBar.js` which requires `Bar.js` which requires `Foo.js`, and voila there hasn't been anything exported from `Foo.js` yet because the `require` calls are blocking, and `module.exports = bot` line hasn't executed yet. Move the bot functionality out from `Foo.js` to `Bot.js` then include `Bot.js` in `Bar.js`. It should work then. – d_shiv Jun 24 '19 at 13:08
  • Yes, this solves it. Thank you. –  Jun 24 '19 at 15:46

1 Answers1

0

Export the class, then instantiate it in Bar.js like: var { Bot } = require( "./Foo.js ); var bot = new Bot( x , y );

0xCAP
  • 740
  • 4
  • 15
  • Doesn't that defeat the purpose of using the instantiated `bot`, if I have to instantiate every time I need it somewhere. –  Jun 24 '19 at 13:06
  • Then if that's what you want to specifically do, you gotta look up how to implement a "singleton pattern", exporting an instance instead of a class, which is something that should look roughly like this : `export default new Bot( x , y );` – 0xCAP Jun 24 '19 at 13:30