'use strict';

const
  fs = require('fs'),
  url = require('url'),
  http = require('http'),
  https = require('https');

// Allowed ports
const PORTS = [443, 80, 88, 8443];

module.exports = (bot, opt) => {

  const token = '/' + bot.token;

  let
    host = opt.host || '0.0.0.0',
    port = opt.port || 443,
    key = opt.key && fs.readFileSync(opt.key),
    cert = opt.cert && fs.readFileSync(opt.cert);

  // Check port
  if (!PORTS.includes(port)) {
    this.event('error', { error });
    console.error(`[bot.error.webhook] allowed ports: ${ PORTS.join(', ') }`);
    return;
  }

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

  // Start server
  server.listen(port, host, x => {
    console.log(`[bot.webhook] started${ key ? ' secure' : ''} server on "${ host }:${ port }"`);
  });

  // Request listener
  function listener(req, res) {
    if (req.url == token && req.method == 'POST') {
      var json = '';
      req.on('data', data => json += data);
      req.on('end', x => {
        bot.receiveUpdates([JSON.parse(json)], true);
      });
    }
    res.end();
  }

};

'use strict';

const
  fs = require('fs'),
  url = require('url'),
  http = require('http'),
  https = require('https');

// Allowed ports
const PORTS = [443, 80, 88, 8443];

module.exports = (bot, opt) => {

  const token = '/' + bot.token;

  let
    host = opt.host || '0.0.0.0',
    port = opt.port || 443,
    key = opt.key && fs.readFileSync(opt.key),
    cert = opt.cert && fs.readFileSync(opt.cert);

  // Check port
  if (!PORTS.includes(port)) {
    let error = `[bot.error.webhook] allowed ports: ${ PORTS.join(', ') }`;
    this.event('error', { error });
    console.error(error);
    return;
  }

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

  // Start server
  server.listen(port, host, x => {
    console.log(`[bot.webhook] started${ key ? ' secure' : ''} server on "${ host }:${ port }"`);
  });

  // Request listener
  function listener(req, res) {
    if (req.url == token && req.method == 'POST') {
      var json = '';
      req.on('data', data => json += data);
      req.on('end', x => {
        bot.receiveUpdates([JSON.parse(json)], true);
      });
    }
    res.end();
  }

};

Fix missing port error