Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance of Bot #54

Closed
XDARKZEROX opened this issue Apr 27, 2017 · 1 comment
Closed

Instance of Bot #54

XDARKZEROX opened this issue Apr 27, 2017 · 1 comment

Comments

@XDARKZEROX
Copy link

Can u explain me how can I call the connection of the bot in multiple js files?
I have two routes, telegram.js and ask.js that uses the telebot library
I have a connection to the bot in a file:

const TeleBot = require('telebot');
const telegramConstants = require("../../config/config");
const bot = new TeleBot({
    token: telegramConstants.botToken,
    polling: { // Optional. Use polling.
        interval: 60, // Optional. How often check updates (in ms).
        timeout: 0, // Optional. Update polling timeout (0 - short polling).
        limit: 100, // Optional. Limits the number of updates to be retrieved.
        retryTimeout: 1000, // Optional. Reconnecting timeout (in ms).
        allowedUpdates: [] // Optional. List the types of updates you want your bot to receive. Specify an empty list to receive all updates regardless of type.
    }
});

bot.connect();
module.exports = bot;

And I call it in my two js files but it creates a new instance for every call.

sin titulo

Its possible to use one instance of the telebot in my two files?

@kosmodrey
Copy link
Contributor

kosmodrey commented Apr 27, 2017

Just call exported bot interface once: bot.connect() or use modules:

myBotModule.js

module.exports = (bot, cfg) => {
  bot.on('/hello', msg => {
    return bot.sendMessage(msg.from.id, 'Hello!');
  });
};

telegram.js

const TeleBot = require('telebot');
const myBotModule = require('./myBotModule.js');

const bot = new TeleBot('***');

bot.use(myBotModule); // use our module (exports interface and config)

bot.on('/start', msg => {
  return bot.sendMessage(msg.from.id, 'Start!');
});

bot.connect();

@mullwar mullwar closed this as completed May 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants