diff --git a/README.md b/README.md index 47f9d5e..d83fa44 100644 --- a/README.md +++ b/README.md @@ -10,86 +10,7 @@ npm install telegram-api If you are cloning this repository, remember to run `npm install` to install dependencies. -The code is well documented. I'm trying to integrate JSDoc / ESDoc into our repository for an easy to access documentation. - ---- - -If you are using `babel/polyfill` and you get an error requiring this modules, try: - -```javascript -require('telegram-api/build'); -``` - -This will bypass the `babel/polyfill` required by the module. - ---- - -All Telegram API methods are accessible through `api` property of bots. - -```javascript -bot.api.sendPhoto({ - chat_id: 99999, - photo: 'some_id', - caption: 'dancing monkey' -}); -``` - -Some API methods have been simplified using what we call *types*. See the example below. - -# Example -[@JavaScriptBot](https://telegram.me/JavaScriptBot) runs on `demo.js`, you can test it. - -```javascript -var Bot = require('telegram-api'); - -// only require the message types you need, more coming soon! -var Message = require('telegram-api/types/Message'); -var Question = require('telegram-api/types/Question'); - -var bot = new Bot({ - token: 'YOUR_KEY' -}); - -bot.start().catch(err => { - console.error(err, '\n', err.stack); -}); - -// polling -bot.on('update', update => { - console.log('Polled\n', update); -}); - -const question = new Question({ - text: 'How should I greet you?', - answers: [['Hey'], ['Hello, Sir'], ['Yo bro']] -}); - -bot.get(/Hi\sBot/, message => { - const id = message.chat.id; - - question.to(id).reply(message.message_id); - - bot.send(question).then(answer => { - const msg = new Message().to(id).text('Your answer: ' + answer.text); - bot.send(msg); - }, () => { - const msg = new Message().to(id).text('Invalid answer'); - bot.send(msg); - }); -}); - -const hello = new Message().text('Hello'); -bot.command('start', message => { - bot.send(hello.to(message.chat.id)); -}); - -const test = new Message().text('Test Command'); -bot.command('test', message => { - bot.send(test.to(message.chat.id)); -}); -``` - -This will result in: +[**Documentation**](https://github.com/mdibaiee/node-telegram-api/wiki) ![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif) @@ -99,7 +20,7 @@ This will result in: # Todo -- [x] Webhook support +- [x] Webhook support (not tested, see [#4](https://github.com/mdibaiee/node-telegram-api/issues/4)) - [x] Forward Type - [x] BulkMessage Type - [x] File Type diff --git a/build/index.js b/build/index.js index fea9e9a..cd367c4 100644 --- a/build/index.js +++ b/build/index.js @@ -36,10 +36,6 @@ var DEFAULTS = { } }; -process.on('uncaughtException', function (err) { - console.error(err.stack); -}); - /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, diff --git a/build/types/Forward.js b/build/types/Forward.js index 0dade9d..44871c3 100644 --- a/build/types/Forward.js +++ b/build/types/Forward.js @@ -78,20 +78,6 @@ var Forward = (function (_Base) { this.properties.message_id = _message; return this; } - }, { - key: 'keyboard', - - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - value: function keyboard(kb) { - this._keyboard = kb; - return this; - } // This class inherits Base's send method diff --git a/build/types/Message.js b/build/types/Message.js index 11ace9c..1f18b09 100644 --- a/build/types/Message.js +++ b/build/types/Message.js @@ -6,7 +6,7 @@ Object.defineProperty(exports, '__esModule', { var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); -var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x2 = parent; _x3 = property; _x4 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; +var _get = function get(_x3, _x4, _x5) { var _again = true; _function: while (_again) { var object = _x3, property = _x4, receiver = _x5; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x3 = parent; _x4 = property; _x5 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } }; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } @@ -77,6 +77,20 @@ var Message = (function (_Base) { this.properties.reply_to_message_id = id; return this; } + }, { + key: 'preview', + + /** + * Set disable_web_page_preview of the message + * @param {boolean} enable + * @return {object} returns the message object + */ + value: function preview() { + var enable = arguments[0] === undefined ? true : arguments[0]; + + this.properties.disable_web_page_preview = !enable; + return this; + } }, { key: 'keyboard', diff --git a/build/types/Question.js b/build/types/Question.js index a909a8f..824ad40 100644 --- a/build/types/Question.js +++ b/build/types/Question.js @@ -98,7 +98,7 @@ var Question = (function (_Message) { return message; } else { _this.emit('question:invalid', message); - throw update; + throw message; } }); } diff --git a/lib/index.js b/lib/index.js index 134ddf7..fe752a2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,10 +11,6 @@ const DEFAULTS = { } }; -process.on('uncaughtException', function(err) { - console.error(err.stack); -}); - /** * Bot class used to connect to a new bot * Bots have an api property which gives access to all Telegram API methods, @@ -75,8 +71,6 @@ export default class Bot extends EventEmitter { } else { return poll(this); } - - }); } diff --git a/lib/types/Forward.js b/lib/types/Forward.js index 26d3bef..af78ed3 100644 --- a/lib/types/Forward.js +++ b/lib/types/Forward.js @@ -46,17 +46,5 @@ export default class Forward extends Base { return this; } - /** - * Sets keyboard of the message - * The value of reply_markup is set to the sanitized keyboard properties - * i.e. reply_markup = JSON.stringify(kb.getProperties()) - * @param {object} kb A Keyboard instance - * @return {object} returns the message object - */ - keyboard(kb) { - this._keyboard = kb; - return this; - } - // This class inherits Base's send method } diff --git a/lib/types/Message.js b/lib/types/Message.js index 15e5b63..ff55df4 100644 --- a/lib/types/Message.js +++ b/lib/types/Message.js @@ -45,6 +45,16 @@ export default class Message extends Base { return this; } + /** + * Set disable_web_page_preview of the message + * @param {boolean} enable + * @return {object} returns the message object + */ + preview(enable = true) { + this.properties.disable_web_page_preview = !enable; + return this; + } + /** * Sets keyboard of the message * The value of reply_markup is set to the sanitized keyboard properties diff --git a/lib/types/Question.js b/lib/types/Question.js index 1673a8c..ece9b46 100644 --- a/lib/types/Question.js +++ b/lib/types/Question.js @@ -62,7 +62,7 @@ export default class Question extends Message { return message; } else { this.emit('question:invalid', message); - throw update; + throw message; } }); }