• API.js

  • ¶

    dependencies

    var _ = require('lomath');
    var fs = require('fs');
  • ¶

    imports

    var req = require(__dirname + '/scraper.js').req;
  • ¶

    The API object prototype

    var API = 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) {
  • ¶

    if it’s string already don’t serialize return _.isString(data) ? data : JSON.stringify(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(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)
    
    }
  • ¶

    //////////////////////// Telegram API methods // //////////////////////// All the methods below return a promise form the q library (see https://github.com/kriskowal/q) for chaining, thus can be called by: samplemethod().then(handlerfunction).then(handler2) You can: pass a single JSON object as the ‘first’ argument (see the Telegram API), or pass multiple parameters in the order listed on Telegram bot API page; the ‘first’ is always the ‘chat_id’ in this case. Note that the response from the HTTP call is always a string (we denote as HTTPres for clarity), thus you might wish to JSON.parse it.

    API.prototype.getUpdates = function(first, limit, timeout) {
        var options = _.isObject(first) ?
            first : {
                offset: first,
                limit: limit,
                timeout: timeout
            };
        return this.reqBot('getUpdates', options)
    }
    API.prototype.setWebhook = function(first) {
        var options = _.isObject(first) ?
            first : {
                url: first
            };
        return this.reqBot('setWebhook', options);
    }
    API.prototype.getMe = function() {
        return this.reqBot('getMe');
    }
    API.prototype.sendMessage = function(first, text, disable_web_page_preview, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                text: text,
                disable_web_page_preview: disable_web_page_preview,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendMessage', options);
    }
    API.prototype.forwardMessage = function(first, from_chat_id, message_id) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                from_chat_id: from_chat_id,
                message_id: message_id
            };
        return this.reqBot('forwardMessage', options);
    }
    API.prototype.sendPhoto = function(first, photo, caption, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                photo: photo,
                caption: caption,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendPhoto', options);
    }
    API.prototype.sendAudio = function(first, audio, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                audio: audio,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendAudio', options);
    }
    API.prototype.sendDocument = function(first, document, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                document: document,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendDocument', options);
    }
    API.prototype.sendSticker = function(first, sticker, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                sticker: sticker,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendSticker', options);
    }
    API.prototype.sendVideo = function(first, video, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                video: video,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendVideo', options);
    }
    API.prototype.sendLocation = function(first, latitude, longitude, reply_to_message_id, reply_markup) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                longitude: longitude,
                latitude: latitude,
                reply_to_message_id: reply_to_message_id,
                reply_markup: JSON.stringify(reply_markup)
            };
        return this.reqBot('sendLocation', options);
    }
    API.prototype.sendChatAction = function(first, action) {
        var options = _.isObject(first) ?
            first : {
                chat_id: first,
                action: action
            };
        return this.reqBot('sendChatAction', options);
    }
    API.prototype.getUserProfilePhotos = function(first, offset, limit) {
        var options = _.isObject(first) ?
            first : {
                user_id: first,
                offset: offset,
                limit: limit
            };
        return this.reqBot('getUserProfilePhotos', options);
    }
    function handle(req, res) {
    }
  • ¶

    export constructor

    module.exports = API;