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

Added an option to skip httpServer creation #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const bot = new TeleBot({
proxy: 'http://username:[email protected]:8080' // Optional. An HTTP proxy to be used.
},
webhook: { // Optional. Use webhook instead of polling.
createServer: true, //Optional. Specifies whether httpServer or httpsServer instance should be created or not. Defaults to true. When set to false, bot.start() returns a promise which resolves with a listener function to be attached to the server manually.
key: 'key.pem', // Optional. Private key for server.
cert: 'cert.pem', // Optional. Public key.
url: 'https://....', // HTTPS url to send updates to.
Expand Down Expand Up @@ -313,7 +314,7 @@ Creates inlineKeyboard object for answerList articles.

##### `start()`

Start polling updates.
Start polling updates. If createServer is set to false in webhook options, then the method returns a promise resolving to a listener function suitable for usage with httpServer/httpsServer createServer method.

##### `stop(<message>)`

Expand Down
16 changes: 16 additions & 0 deletions examples/webhook-custom-http-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const TeleBot = require('../');

const bot = new TeleBot({
token: 'TELEGRAM_BOT_TOKEN',
webhook: {
createServer: false,
url: 'https://....'
}
});

bot.on('text', msg => bot.sendMessage(msg.from.id, msg.text));

bot.start().then(listener => {
const server = http.createServer(listener);
server.listen(3200, '0.0.0.0');
});
30 changes: 18 additions & 12 deletions lib/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ module.exports = (bot, opt) => {
const path = url.parse(opt.url).pathname;
const key = opt.key && fs.readFileSync(opt.key);
const cert = opt.cert && fs.readFileSync(opt.cert);

// Create server
const server = key && cert ?
https.createServer({key, cert}, listener) :
http.createServer(listener);

// Start server
server.listen(port, host, () => {
if (bot.logging) {
console.log(`[bot.webhook] started${ key ? ' secure' : ''} server on "${ host }:${ port }"`);
}
});
const createServer = opt.createServer === false ? false : true;

if (createServer) {
// Create server
const server = key && cert ?
https.createServer({key, cert}, listener) :
http.createServer(listener);

// Start server
server.listen(port, host, () => {
if (bot.logging) {
console.log(`[bot.webhook] started${ key ? ' secure' : ''} server on "${ host }:${ port }"`);
}
});
}else{
// Server will be created by consumer, just return the listener
return listener;
}

// Request listener
function listener(req, res) {
Expand Down