Added File Type
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user