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; callback = options;
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)); 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); ret.attach("file", file.file, file.name);
if (data) { if (data) {
for (var i in data) { for (var i in data) {
ret.field(i, data[i]); if (data[i] !== undefined) {
ret.field(i, data[i]);
}
} }
} }
} else if (data) { } else if (data) {
@@ -701,16 +703,47 @@ var InternalClient = (function () {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
return this.resolver.resolveChannel(where).then(function (destination) { if (options.file) {
//var destination; if (typeof options.file !== "object") {
var content = _this16.resolver.resolveString(_content); 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, { return this.resolver.resolveChannel(where).then(function (destination) {
content: content, if (options.file) {
tts: options.tts return _this16.resolver.resolveFile(options.file.file).then(function (file) {
}).then(function (res) { return _this16.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(destination.id), true, {
return destination.messages.add(new _StructuresMessage2["default"](res, destination, _this16.client)); 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 this.resolver.resolveChannel(where).then(function (channel) {
return _this17.resolver.resolveFile(_file).then(function (file) { return _this17.resolver.resolveFile(_file).then(function (file) {
return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, { return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, content, {
content: content
}, {
name: name, name: name,
file: file file: file
}).then(function (res) { }).then(function (res) {

View File

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

View File

@@ -72,7 +72,9 @@ export default class InternalClient {
ret.attach("file", file.file, file.name); ret.attach("file", file.file, file.name);
if (data) { if (data) {
for (var i in data) { for (var i in data) {
ret.field(i, data[i]); if (data[i] !== undefined) {
ret.field(i, data[i]);
}
} }
} }
} else if (data) { } else if (data) {
@@ -565,19 +567,46 @@ export default class InternalClient {
// def sendMessage // def sendMessage
sendMessage(where, _content, options = {}) { 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) return this.resolver.resolveChannel(where)
.then(destination => { .then(destination => {
//var destination; if (options.file) {
var content = this.resolver.resolveString(_content); 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, { return this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(destination.id), true, {
content: content, content: content,
tts: options.tts tts: options.tts
}) })
.then(res => .then(res => destination.messages.add(new Message(res, destination, this.client)));
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) return this.resolver.resolveChannel(where)
.then(channel => .then(channel =>
this.resolver.resolveFile(_file) this.resolver.resolveFile(_file)
.then(file => .then(file =>
this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, { this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, content, {
content
}, {
name, name,
file file
}).then(res => channel.messages.add(new Message(res, channel, this.client))) }).then(res => channel.messages.add(new Message(res, channel, this.client)))