2015-07-02 21:49:36 +00:00
|
|
|
import Base from './Base';
|
|
|
|
import mime from 'mime';
|
|
|
|
|
|
|
|
const TYPES = ['photo', 'video', 'document', 'audio'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File class, used to send pictures/movies/audios/documents to chat
|
|
|
|
*/
|
|
|
|
export default class File extends Base {
|
|
|
|
/**
|
|
|
|
* Create a new file instance
|
|
|
|
* @param {object} properties File properties, as defined by Telegram API
|
|
|
|
*/
|
|
|
|
constructor(properties = {}) {
|
2015-07-02 22:33:22 +00:00
|
|
|
super('sendDocument');
|
2015-07-02 21:49:36 +00:00
|
|
|
|
|
|
|
this.properties = properties;
|
|
|
|
this._keyboard = new Base();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set chat_id of the message
|
|
|
|
* @param {number} chat
|
|
|
|
* @return {object} returns the message object
|
|
|
|
*/
|
|
|
|
to(chat) {
|
|
|
|
this.properties.chat_id = chat;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set file of the message
|
2015-07-02 22:33:22 +00:00
|
|
|
* @param {string} file File path
|
2015-07-02 21:49:36 +00:00
|
|
|
* @param {string} fileType (optional) if the first argument is a
|
|
|
|
* file_id string, this option indicates file type
|
|
|
|
* @return {object} returns the message object
|
|
|
|
*/
|
2015-07-02 22:33:22 +00:00
|
|
|
file(file, fileType) {
|
|
|
|
if (fileType) {
|
2015-07-06 00:05:25 +00:00
|
|
|
this.properties[fileType] = {file: file};
|
2015-07-02 22:33:22 +00:00
|
|
|
|
|
|
|
return this;
|
2015-07-02 21:49:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 22:33:22 +00:00
|
|
|
let [type, extension] = mime.lookup(file).split('/');
|
2015-07-02 21:49:36 +00:00
|
|
|
if (type === 'image') {
|
|
|
|
type = 'photo';
|
|
|
|
}
|
|
|
|
|
2015-07-02 22:33:22 +00:00
|
|
|
if (extension === 'gif') {
|
|
|
|
type = 'document';
|
|
|
|
}
|
|
|
|
|
2015-07-02 21:49:36 +00:00
|
|
|
if (TYPES.indexOf(type) === -1) {
|
|
|
|
type = 'document';
|
|
|
|
}
|
|
|
|
|
2015-07-06 00:05:25 +00:00
|
|
|
this.properties[type] = {file: file};
|
2015-07-02 22:33:22 +00:00
|
|
|
|
|
|
|
this.method = `send${type[0].toUpperCase() + type.slice(1)}`;
|
2015-07-02 21:49:36 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set caption for photos
|
|
|
|
* @param {string} text caption's text
|
|
|
|
* @return {object} returns the message object
|
|
|
|
*/
|
|
|
|
caption(text) {
|
|
|
|
this.properties.caption = text;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set reply_to_message_id of the message
|
|
|
|
* @param {number} id message_id of the message to reply to
|
|
|
|
* @return {object} returns the message object
|
|
|
|
*/
|
|
|
|
reply(id) {
|
|
|
|
this.properties.reply_to_message_id = id;
|
|
|
|
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
|
|
|
|
}
|