Naltox / telegram-node-bot

Node module for creating Telegram bots.
MIT License
720 stars 144 forks source link

Best practics to use express with telegram-node-bot #170

Open numkms opened 7 years ago

numkms commented 7 years ago

Hello! Im try to use this framework with express framework for notice users via get query from php-app. Well heres my code: `'use strict' const express = require('express'); const app = express(); app.listen(8008);

const Telegram = require('telegram-node-bot');
const TelegramBaseController = Telegram.TelegramBaseController;
const TextCommand = Telegram.TextCommand;
const tg = new Telegram.Telegram('MYTOKEN');`

So i get binded port error cause express execute many times. Some Log


Telegram Worker started at 1804 PID

[log]
Telegram Worker started at 6180 PID

[log]
Telegram Worker started at 5904 PID

[log]
Telegram Worker started at 5300 PID

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: bind EADDRINUSE null:8008
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at cb (net.js:1324:16)
    at shared (cluster.js:619:5)
    at Worker.<anonymous> (cluster.js:592:9)
    at process.<anonymous> (cluster.js:765:8)
    at emitTwo (events.js:111:20)
    at process.emit (events.js:191:7)
    at process.nextTick (internal/child_process.js:744:12)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: bind EADDRINUSE null:8008
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at cb (net.js:1324:16)
    at shared (cluster.js:619:5)
    at Worker.<anonymous> (cluster.js:592:9)
    at process.<anonymous> (cluster.js:765:8)
    at emitTwo (events.js:111:20)
    at process.emit (events.js:191:7)
    at process.nextTick (internal/child_process.js:744:12)
    at _combinedTickCallback (internal/process/next_tick.js:67:7)

What should i do for fix this issue? Thx and sorry for bad english :)

jesusgn90 commented 7 years ago

Set number of workers to 1 maybe works...

const tg = new Telegram.Telegram('YOUR_TOKEN', { workers: 1 })

salishivani commented 7 years ago

how to give url link when clicking on button?

muety commented 7 years ago

I have the same problem. Are there any solution to this?

azevedovadio commented 7 years ago

I have the same problem. Did anyone get the solution?

numkms commented 7 years ago

Yes i have, u need to use express init in $.onMaster callback

numkms commented 7 years ago

`//init modules const express = require('express'); const port = 8888; var listener = express();

const Telegram = require('telegram-node-bot') const TelegramBaseController = Telegram.TelegramBaseController const TextCommand = Telegram.TextCommand

let tg = new Telegram.Telegram('key');

tg.onMaster(() => { listener.listen(port, function (){ console.log('Your app runing on '+port); });

        listener.get('/query',function(req,res){
                res.send('your response');
        });

});`
Like this