API.js
function(token) {
    this.token = token;
    this.baseUrl = 'https://api.telegram.org/bot' + this.token;

    // A sample Telegram bot JSON data, for req options
    this.formData = {
        chat_id: "87654321",
        text: "Hello there"
    };

    // template options for req the Telegram Bot API
    this.baseoptions = {
        method: 'POST',
        baseUrl: this.baseUrl,
        url: "sendMessage",
        formData: this.formData
    };

    ///////////////////////////////
    // Internal, private methods //
    ///////////////////////////////

    // Convert an data to format proper for HTTP methods
    this.toHTTPProper = function(data) {
        // currently serialize Array now, leave stream/string
        return _.isArray(data) ? JSON.stringify(data) : data;
        // return data;
    }

    // serialize a whole object proper for HTTP methods
    // discard key-value pair if value is undefined
    // only returns non-empty JSON
    this.serialize = function(obj) {
        // var ser = _.omit(
        //     _.mapValues(_.flattenJSON(obj), this.toHTTPProper)
        //     , _.isUndefined);
        var ser = _.omit(
            _.mapValues(obj, this.toHTTPProper)
            , _.isUndefined);
        if (!_.isEmpty(ser)) return ser;
    }

    // properly set req as its method
    this.req = req;

    // Extends the options for req for the Telegram Bot.
    // @param  {string} botMethod Telegram bot API method.
    // @param  {JSON} JSONdata A JSON object with the required fields (see API).
    // @param  {string} [HTTPMethod=POST] Optionally change method if need to.
    // @returns {JSON} option The extended option.
    // 
    // @example
    // extOpt('sendMessage', {chat_id: 123456, text: 'hello world'})
    // → {
    // method: 'POST',
    // baseUrl: 'https://api.telegram.org/bot...',
    // url: 'sendMessage',
    // formData: {chat_id: 123456, text: 'hello world'} }
    this.extOpt = function(botMethod, JSONdata, HTTPMethod) {
        var opt = _.clone({
            method: 'POST',
            baseUrl: this.baseUrl,
            url: "sendMessage",
            formData: this.formData
        });
        _.assign(opt, {
            url: botMethod
        })
        if (JSONdata) _.assign(opt, {
            formData: this.serialize(JSONdata)
        })
        if (HTTPMethod) _.assign(opt, {
            botMethod: HTTPMethod
        });

        return opt;
    }

    // shorthand composition: bot's HTTP request
    this.reqBot = _.flow(this.extOpt, this.req)

}
function(token) {
    this.token = token;
    this.baseUrl = 'https://api.telegram.org/bot' + this.token;

    // A sample Telegram bot JSON data, for req options
    this.formData = {
        chat_id: "87654321",
        text: "Hello there"
    };

    // template options for req the Telegram Bot API
    this.baseoptions = {
        method: 'POST',
        baseUrl: this.baseUrl,
        url: "sendMessage",
        formData: this.formData
    };

    ///////////////////////////////
    // Internal, private methods //
    ///////////////////////////////

    // Convert an data to format proper for HTTP methods
    this.toHTTPProper = function(data) {
        // currently serialize Array now, leave stream/string
        return _.isArray(data) ? JSON.stringify(data) : data;
        // return data;
    }

    // serialize a whole object proper for HTTP methods
    // discard key-value pair if value is undefined
    // only returns non-empty JSON
    this.serialize = function(obj) {
        // var ser = _.omit(
        //     _.mapValues(_.flattenJSON(obj), this.toHTTPProper)
        //     , _.isUndefined);
        var ser = _.omit(
            _.mapValues(obj, this.toHTTPProper)
            , _.isUndefined);
        if (!_.isEmpty(ser)) return ser;
    }

    // properly set req as its method
    this.req = req;

    // Extends the options for req for the Telegram Bot.
    // @param  {string} botMethod Telegram bot API method.
    // @param  {JSON} JSONdata A JSON object with the required fields (see API).
    // @param  {string} [HTTPMethod=POST] Optionally change method if need to.
    // @returns {JSON} option The extended option.
    // 
    // @example
    // extOpt('sendMessage', {chat_id: 123456, text: 'hello world'})
    // → {
    // method: 'POST',
    // baseUrl: 'https://api.telegram.org/bot...',
    // url: 'sendMessage',
    // formData: {chat_id: 123456, text: 'hello world'} }
    this.extOpt = function(botMethod, JSONdata, HTTPMethod) {
        var opt = _.clone({
            method: 'POST',
            baseUrl: this.baseUrl,
            url: "sendMessage",
            formData: this.formData
        });
        _.assign(opt, {
            url: botMethod
        })
        if (JSONdata) _.assign(opt, {
            formData: this.serialize(JSONdata)
        })
        if (HTTPMethod) _.assign(opt, {
            botMethod: HTTPMethod
        });

        return opt;
    }

    // shorthand composition: bot's HTTP request
    this.reqBot = _.flow(this.extOpt, this.req)

}
function ( token ) { this . token = token ; this . baseUrl = 'https://api.telegram.org/bot' + this . token ; // A sample Telegram bot JSON data, for req options this . formData = { chat_id : "87654321" , text : "Hello there" } ; // template options for req the Telegram Bot API this . baseoptions = { method : 'POST' , baseUrl : this . baseUrl , url : "sendMessage" , formData : this . formData } ; /////////////////////////////// // Internal, private methods // /////////////////////////////// // Convert an data to format proper for HTTP methods this . toHTTPProper = function ( data ) { // currently serialize Array now, leave stream/string return _ . isArray ( data ) ? JSON . stringify ( data ) : data ; // return data; } // serialize a whole object proper for HTTP methods // discard key-value pair if value is undefined // only returns non-empty JSON this . serialize = function ( obj ) { // var ser = _.omit( //     _.mapValues(_.flattenJSON(obj), this.toHTTPProper) //     , _.isUndefined); var ser = _ . omit ( _ . mapValues ( obj , this . toHTTPProper ) , _ . isUndefined ) ; if ( ! _ . isEmpty ( ser ) ) return ser ; } // properly set req as its method this . req = req ; // Extends the options for req for the Telegram Bot. // @param  {string} botMethod Telegram bot API method. // @param  {JSON} JSONdata A JSON object with the required fields (see API). // @param  {string} [HTTPMethod=POST] Optionally change method if need to. // @returns {JSON} option The extended option. //  // @example // extOpt('sendMessage', {chat_id: 123456, text: 'hello world'}) // → { // method: 'POST', // baseUrl: 'https://api.telegram.org/bot...', // url: 'sendMessage', // formData: {chat_id: 123456, text: 'hello world'} } this . extOpt = function ( botMethod , JSONdata , HTTPMethod ) { var opt = _ . clone ( { method : 'POST' , baseUrl : this . baseUrl , url : "sendMessage" , formData : this . formData } ) ; _ . assign ( opt , { url : botMethod } ) if ( JSONdata ) _ . assign ( opt , { formData : this . serialize ( JSONdata ) } ) if ( HTTPMethod ) _ . assign ( opt , { botMethod : HTTPMethod } ) ; return opt ; } // shorthand composition: bot's HTTP request this . reqBot = _ . flow ( this . extOpt , this . req ) }
The API object prototype 
The API bot constructor.

@category Telegram API
@param {string} token Your Telegram bot token.
@returns {Bot} The bot able to call methods.

@example

var fs = require('fs');
var bot = require('telegram-bot-bootstrap');
var Alice = new bot(your_bot_token);

Alice.sendMessage(user_chat_id, 'Hey wanna see some cool art?');

Alice.sendPhoto(user_chat_id, fs.createReadStream(__dirname+'/alexiuss.jpg'),  * 'Chronoscape by Alexiuss').then(console.log)

var kb = {
keyboard: [
['one'],
['two', 'three'],
['four', 'five', 'six']
],
one_time_keyboard: true
};
Alice.sendMessage(user_chat_id, "Choose a lucky number", undefined, undefined,  * kb)

// → The messages and photos are sent to user.
The API object prototype The API bot constructor .