node-telegram-api/lib/api.js

36 lines
984 B
JavaScript
Raw Normal View History

2015-06-26 17:20:54 +00:00
// API methods
import fetch from './fetch';
2015-06-28 22:42:48 +00:00
/**
* API class, has a function for each method of the Telegram API which take
* an object argument, and send request to the API server
*
* Methods: getMe, sendMessage, forwardMessage, sendPhoto, sendAudio,
* sendDocument, sendSticker, sendVideo, sendLocation, sendChatAction,
* getUserProfilePhotos, getUpdates
*/
export default class API {
/**
* Create a new api object with the given token
* @param {string} token
*/
constructor(token) {
this.token = token;
}
2015-06-26 17:20:54 +00:00
}
API.prototype.request = function request(method, data) {
return fetch(this.token + '/' + method, data);
};
const methods = ['getMe', 'sendMessage', 'forwardMessage', 'sendPhoto',
'sendAudio', 'sendDocument', 'sendSticker', 'sendVideo',
'sendLocation', 'sendChatAction', 'getUserProfilePhotos',
2015-06-29 22:20:34 +00:00
'getUpdates', 'setWebhook'];
2015-06-26 17:20:54 +00:00
methods.forEach(method => {
API.prototype[method] = function(data) {
return this.request(method, data);
};
});