ability to send file via sendMessage

This commit is contained in:
abalabahaha
2016-04-15 20:10:30 -07:00
parent 5f1e1b989b
commit dd71bb9e27
4 changed files with 106 additions and 26 deletions

View File

@@ -251,6 +251,11 @@ var Client = (function (_EventEmitter) {
callback = options;
options = {};
}
if (typeof content === "object" && content.file) {
// content has file
options = content;
content = "";
}
return this.internal.sendMessage(destination, content, options).then(dataCallback(callback), errorCallback(callback));
};

View File

@@ -142,7 +142,9 @@ var InternalClient = (function () {
ret.attach("file", file.file, file.name);
if (data) {
for (var i in data) {
ret.field(i, data[i]);
if (data[i] !== undefined) {
ret.field(i, data[i]);
}
}
}
} else if (data) {
@@ -701,16 +703,47 @@ var InternalClient = (function () {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return this.resolver.resolveChannel(where).then(function (destination) {
//var destination;
var content = _this16.resolver.resolveString(_content);
if (options.file) {
if (typeof options.file !== "object") {
options.file = {
file: options.file
};
}
if (!options.file.name) {
if (options.file.file instanceof String || typeof options.file.file === "string") {
options.file.name = require("path").basename(options.file.file);
} else if (options.file.file.path) {
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
options.file.name = require("path").basename(options.file.file.path);
} else {
options.file.name = "default.png"; // Just have to go with default filenames.
}
}
}
return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content,
tts: options.tts
}).then(function (res) {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client));
});
return this.resolver.resolveChannel(where).then(function (destination) {
if (options.file) {
return _this16.resolver.resolveFile(options.file.file).then(function (file) {
return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: _content,
tts: options.tts
}, {
name: options.file.name,
file: file
}).then(function (res) {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client));
});
});
} else {
var content = _this16.resolver.resolveString(_content);
return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content,
tts: options.tts
}).then(function (res) {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client));
});
}
});
};
@@ -730,11 +763,15 @@ var InternalClient = (function () {
}
}
if (content) {
content = {
content: content
};
}
return this.resolver.resolveChannel(where).then(function (channel) {
return _this17.resolver.resolveFile(_file).then(function (file) {
return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content: content
}, {
return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, content, {
name: name,
file: file
}).then(function (res) {

View File

@@ -364,6 +364,11 @@ export default class Client extends EventEmitter {
callback = options;
options = {};
}
if (typeof content === "object" && content.file) {
// content has file
options = content;
content = "";
}
return this.internal.sendMessage(destination, content, options)
.then(dataCallback(callback), errorCallback(callback));

View File

@@ -72,7 +72,9 @@ export default class InternalClient {
ret.attach("file", file.file, file.name);
if (data) {
for (var i in data) {
ret.field(i, data[i]);
if (data[i] !== undefined) {
ret.field(i, data[i]);
}
}
}
} else if (data) {
@@ -565,19 +567,46 @@ export default class InternalClient {
// def sendMessage
sendMessage(where, _content, options = {}) {
if (options.file) {
if (typeof options.file !== "object") {
options.file = {
file: options.file
};
}
if (!options.file.name) {
if (options.file.file instanceof String || typeof options.file.file === "string") {
options.file.name = require("path").basename(options.file.file);
} else if (options.file.file.path) {
// fs.createReadStream()'s have .path that give the path. Not sure about other streams though.
options.file.name = require("path").basename(options.file.file.path);
} else {
options.file.name = "default.png"; // Just have to go with default filenames.
}
}
}
return this.resolver.resolveChannel(where)
.then(destination => {
//var destination;
var content = this.resolver.resolveString(_content);
if (options.file) {
return this.resolver.resolveFile(options.file.file)
.then(file =>
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: _content,
tts: options.tts
}, {
name: options.file.name,
file: file
}).then(res => destination.messages.add(new Message(res, destination, this.client)))
)
} else {
var content = this.resolver.resolveString(_content);
return this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content,
tts: options.tts
})
.then(res =>
destination.messages.add(new Message(res, destination, this.client))
);
return this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content,
tts: options.tts
})
.then(res => destination.messages.add(new Message(res, destination, this.client)));
}
});
}
@@ -595,13 +624,17 @@ export default class InternalClient {
}
}
if(content) {
content = {
content
};
}
return this.resolver.resolveChannel(where)
.then(channel =>
this.resolver.resolveFile(_file)
.then(file =>
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, {
content
}, {
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, content, {
name,
file
}).then(res => channel.messages.add(new Message(res, channel, this.client)))