<commit_before>'use strict';

var TelegramBotWebHook = require('./telegramWebHook');
var TelegramBotPolling = require('./telegramPolling');
var debug = require('debug')('node-telegram-bot-api');
var EventEmitter = require('events').EventEmitter;
var fileType = require('file-type');
var Promise = require('bluebird');
var request = require('request');
var stream = require('stream');
var util = require('util');
var mime = require('mime');
var path = require('path');
var URL = require('url');
var fs = require('fs');

var requestPromise = Promise.promisify(request);

/**
 * Both request method to obtain messages are implemented. To use standard polling, set `polling: true`
 * on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate.
 * Emits `message` when a message arrives.
 *
 * @class TelegramBot
 * @constructor
 * @param {String} token Bot Token
 * @param {Object} [options]
 * @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options
 * @param {String|Number} [options.polling.timeout=4] Polling time
 * @param {String|Number} [options.polling.interval=2000] Interval between requests in miliseconds
 * @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options
 * @param {String} [options.webHook.key] PEM private key to webHook server.
 * @param {String} [options.webHook.cert] PEM certificate (public) to webHook server.
 * @see https://core.telegram.org/bots/api
 */
var TelegramBot = function (token, options) {
  options = options || {};
  this.options = options;
  this.token = token;
  this.messageTypes = [
    'text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact',
    'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title',
    'new_chat_photo', 'delete_chat_photo', 'group_chat_created'
  ]; // Telegram message events
  this.textRegexpCallbacks = [];

  this.processUpdate = this._processUpdate.bind(this);

  if (options.polling) {
    this.initPolling();
  }

  if (options.webHook) {
    this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate);
  }
};

util.inherits(TelegramBot, EventEmitter);

TelegramBot.prototype.initPolling = function() {
  if (this._polling) {
    this._polling.abort = true;
    this._polling.lastRequest.cancel('Polling restart');
  }
  this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate);
};

TelegramBot.prototype._processUpdate = function (update) {
  debug('Process Update %j', update);
  var message = update.message;
  debug('Process Update message %j', message);
  if (message) {
    this.emit('message', message);
    var processMessageType = function (messageType) {
      if (message[messageType]) {
        debug('Emtting %s: %j', messageType, message);
        this.emit(messageType, message);
      }
    };
    this.messageTypes.forEach(processMessageType.bind(this));
    if (message.text) {
      debug('Text message');
      this.textRegexpCallbacks.forEach(function (reg) {
        debug('Matching %s whith', message.text, reg.regexp);
        var result = reg.regexp.exec(message.text);
        if (result) {
          debug('Matches', reg.regexp);
          reg.callback(message, result);
        }
      });
    }
  }
};

TelegramBot.prototype._request = function (path, options) {
  if (!this.token) {
    throw new Error('Telegram Bot Token not provided!');
  }
  options = options || {};
  if (options.form) {
    var replyMarkup = options.form.reply_markup;
    if (replyMarkup && typeof replyMarkup !== 'string') {
      // reply_markup must be passed as JSON stringified to Telegram
      options.form.reply_markup = JSON.stringify(replyMarkup);
    }
  }
  options.url = this._buildURL(path);
  debug('HTTP request: %j', options);
  return requestPromise(options)
    .then(function (resp) {
      if (resp[0].statusCode !== 200) {
        throw new Error(resp[0].statusCode+' '+resp[0].body);
      }
      var data = JSON.parse(resp[0].body);
      if (data.ok) {
        return data.result;
      } else {
        throw new Error(data.error_code+' '+data.description);
      }<commit_msg>Added support for listening to inline requests<commit_after>'use strict';

var TelegramBotWebHook = require('./telegramWebHook');
var TelegramBotPolling = require('./telegramPolling');
var debug = require('debug')('node-telegram-bot-api');
var EventEmitter = require('events').EventEmitter;
var fileType = require('file-type');
var Promise = require('bluebird');
var request = require('request');
var stream = require('stream');
var util = require('util');
var mime = require('mime');
var path = require('path');
var URL = require('url');
var fs = require('fs');

var requestPromise = Promise.promisify(request);

/**
 * Both request method to obtain messages are implemented. To use standard polling, set `polling: true`
 * on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a SSL certificate.
 * Emits `message` when a message arrives.
 *
 * @class TelegramBot
 * @constructor
 * @param {String} token Bot Token
 * @param {Object} [options]
 * @param {Boolean|Object} [options.polling=false] Set true to enable polling or set options
 * @param {String|Number} [options.polling.timeout=4] Polling time
 * @param {String|Number} [options.polling.interval=2000] Interval between requests in miliseconds
 * @param {Boolean|Object} [options.webHook=false] Set true to enable WebHook or set options
 * @param {String} [options.webHook.key] PEM private key to webHook server.
 * @param {String} [options.webHook.cert] PEM certificate (public) to webHook server.
 * @see https://core.telegram.org/bots/api
 */
var TelegramBot = function (token, options) {
  options = options || {};
  this.options = options;
  this.token = token;
  this.messageTypes = [
    'text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact',
    'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title',
    'new_chat_photo', 'delete_chat_photo', 'group_chat_created'
  ]; // Telegram message events
  this.textRegexpCallbacks = [];

  this.processUpdate = this._processUpdate.bind(this);

  if (options.polling) {
    this.initPolling();
  }

  if (options.webHook) {
    this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate);
  }
};

util.inherits(TelegramBot, EventEmitter);

TelegramBot.prototype.initPolling = function() {
  if (this._polling) {
    this._polling.abort = true;
    this._polling.lastRequest.cancel('Polling restart');
  }
  this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate);
};

TelegramBot.prototype._processUpdate = function (update) {
  debug('Process Update %j', update);
  var message = update.message;
  var inline_query = update.inline_query;
  var chosen_inline_result = update.chosen_inline_result;
  
  if (message) {
    debug('Process Update message %j', message);
    this.emit('message', message);
    var processMessageType = function (messageType) {
      if (message[messageType]) {
        debug('Emtting %s: %j', messageType, message);
        this.emit(messageType, message);
      }
    };
    this.messageTypes.forEach(processMessageType.bind(this));
    if (message.text) {
      debug('Text message');
      this.textRegexpCallbacks.forEach(function (reg) {
        debug('Matching %s whith', message.text, reg.regexp);
        var result = reg.regexp.exec(message.text);
        if (result) {
          debug('Matches', reg.regexp);
          reg.callback(message, result);
        }
      });
    }
  } else if(inline_query) {
    debug('Process Update inline_query %j', inline_query);
    this.emit('inline_query', inline_query);
  } else if(chosen_inline_result) {
    debug('Process Update chosen_inline_result %j', chosen_inline_result);
    this.emit('chosen_inline_result', chosen_inline_result);
  }
};

TelegramBot.prototype._request = function (path, options) {
  if (!this.token) {
    throw new Error('Telegram Bot Token not provided!');
  }
  options = options || {};
  if (options.form) {
    var replyMarkup = options.form.reply_markup;
    if (replyMarkup && typeof replyMarkup !== 'string') {
      // reply_markup must be passed as JSON stringified to Telegram
      options.form.reply_markup = JSON.stringify(replyMarkup);
    }
  }
  options.url = this._buildURL(path);
  debug('HTTP request: %j', options);
  return requestPromise(options)
    .then(function (resp) {
      if (resp[0].statusCode !== 200) {
        throw new Error(resp[0].statusCode+' '+resp[0].body);
      }
      var data = JSON.parse(resp[0].body);
      if (data.ok) {
        return data.result;
      } else {
        throw new Error(data.error_code+' '+data.description);
      }
{'fraction_non_alphanumeric': 0.14234071396294623, 'fraction_numerical': 0.0027112516945323093, 'mean_word_length': 4.001694915254237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173474', 'n_tokens_mistral': 2738, 'n_tokens_neox': 2704, 'n_words': 664}