From dd71bb9e2736ef67e3f481ffc2c6cd9aceeef171 Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Fri, 15 Apr 2016 20:10:30 -0700 Subject: [PATCH] ability to send file via sendMessage --- lib/Client/Client.js | 5 +++ lib/Client/InternalClient.js | 63 ++++++++++++++++++++++++++++-------- src/Client/Client.js | 5 +++ src/Client/InternalClient.js | 59 +++++++++++++++++++++++++-------- 4 files changed, 106 insertions(+), 26 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 5a014cb4e..b05b509de 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -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)); }; diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 730150d95..0ea309834 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -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) { diff --git a/src/Client/Client.js b/src/Client/Client.js index b827ecedf..5658404a9 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -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)); diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index b84b074b3..4c9913591 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -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)))