Added File Type

This commit is contained in:
Mahdi Dibaiee
2015-07-03 03:03:22 +04:30
parent 92a43ec84c
commit 47947f5209
3 changed files with 60 additions and 15 deletions

View File

@ -1,5 +1,8 @@
import Base from './Base';
import mime from 'mime';
import fs from 'fs';
import path from 'path';
import restler from 'restler';
const TYPES = ['photo', 'video', 'document', 'audio'];
@ -12,7 +15,7 @@ export default class File extends Base {
* @param {object} properties File properties, as defined by Telegram API
*/
constructor(properties = {}) {
super('sendMessage');
super('sendDocument');
this.properties = properties;
this._keyboard = new Base();
@ -30,26 +33,37 @@ export default class File extends Base {
/**
* Set file of the message
* @param {ReadableStream} stream File Stream
* @param {string} file File path
* @param {string} fileType (optional) if the first argument is a
* file_id string, this option indicates file type
* @return {object} returns the message object
*/
file(stream, fileType) {
if (typeof stream === 'string') {
this.properties[fileType] = stream;
file(file, fileType) {
if (fileType) {
this.properties[fileType] = file;
return this;
}
let type = mime.lookup(stream.path).split('/')[0];
const stat = fs.statSync(file);
const name = path.basename(file);
let [type, extension] = mime.lookup(file).split('/');
if (type === 'image') {
type = 'photo';
}
if (extension === 'gif') {
type = 'document';
}
if (TYPES.indexOf(type) === -1) {
type = 'document';
}
this.properties[type] = stream;
this.properties[type] = restler.file(file, name, stat.size, 'utf-8');
this.method = `send${type[0].toUpperCase() + type.slice(1)}`;
return this;
}