DOCUMENTATION IS HEREEEEE, YAAAY ✌️ ✌️
+ Small fixes
This commit is contained in:
parent
da12078d73
commit
086614f1e7
83
README.md
83
README.md
@ -10,86 +10,7 @@ npm install telegram-api
|
|||||||
|
|
||||||
If you are cloning this repository, remember to run `npm install` to install dependencies.
|
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.
|
[**Documentation**](https://github.com/mdibaiee/node-telegram-api/wiki)
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif)
|
![@JavaScriptBot](https://github.com/mdibaiee/node-telegram-api/raw/master/demo.gif)
|
||||||
|
|
||||||
@ -99,7 +20,7 @@ This will result in:
|
|||||||
|
|
||||||
# Todo
|
# Todo
|
||||||
|
|
||||||
- [x] Webhook support
|
- [x] Webhook support (not tested, see [#4](https://github.com/mdibaiee/node-telegram-api/issues/4))
|
||||||
- [x] Forward Type
|
- [x] Forward Type
|
||||||
- [x] BulkMessage Type
|
- [x] BulkMessage Type
|
||||||
- [x] File Type
|
- [x] File Type
|
||||||
|
@ -36,10 +36,6 @@ var DEFAULTS = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('uncaughtException', function (err) {
|
|
||||||
console.error(err.stack);
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bot class used to connect to a new bot
|
* Bot class used to connect to a new bot
|
||||||
* Bots have an api property which gives access to all Telegram API methods,
|
* Bots have an api property which gives access to all Telegram API methods,
|
||||||
|
@ -78,20 +78,6 @@ var Forward = (function (_Base) {
|
|||||||
this.properties.message_id = _message;
|
this.properties.message_id = _message;
|
||||||
return this;
|
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
|
// This class inherits Base's send method
|
||||||
|
|
||||||
|
@ -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 _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 }; }
|
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;
|
this.properties.reply_to_message_id = id;
|
||||||
return this;
|
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',
|
key: 'keyboard',
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ var Question = (function (_Message) {
|
|||||||
return message;
|
return message;
|
||||||
} else {
|
} else {
|
||||||
_this.emit('question:invalid', message);
|
_this.emit('question:invalid', message);
|
||||||
throw update;
|
throw message;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -11,10 +11,6 @@ const DEFAULTS = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('uncaughtException', function(err) {
|
|
||||||
console.error(err.stack);
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bot class used to connect to a new bot
|
* Bot class used to connect to a new bot
|
||||||
* Bots have an api property which gives access to all Telegram API methods,
|
* Bots have an api property which gives access to all Telegram API methods,
|
||||||
@ -75,8 +71,6 @@ export default class Bot extends EventEmitter {
|
|||||||
} else {
|
} else {
|
||||||
return poll(this);
|
return poll(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,17 +46,5 @@ export default class Forward extends Base {
|
|||||||
return this;
|
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
|
// This class inherits Base's send method
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,16 @@ export default class Message extends Base {
|
|||||||
return this;
|
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
|
* Sets keyboard of the message
|
||||||
* The value of reply_markup is set to the sanitized keyboard properties
|
* The value of reply_markup is set to the sanitized keyboard properties
|
||||||
|
@ -62,7 +62,7 @@ export default class Question extends Message {
|
|||||||
return message;
|
return message;
|
||||||
} else {
|
} else {
|
||||||
this.emit('question:invalid', message);
|
this.emit('question:invalid', message);
|
||||||
throw update;
|
throw message;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user