mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-29 13:27:44 +00:00
[telegram] Handle error when formatting formData
Bug: The method TelegramBot#_formatSendData() throws an error if the Buffer file type is NOT supported. We need to handle it, by returning a Promise that is reject with the originally thrown exception. References: * PR trying to achieve the same: https://github.com/yagop/node-telegram-bot-api/pull/66
This commit is contained in:
parent
97c8130d93
commit
6753f7befd
@ -179,6 +179,8 @@ class TelegramBot extends EventEmitter {
|
|||||||
* @return {Array} formatted
|
* @return {Array} formatted
|
||||||
* @return {Object} formatted[0] formData
|
* @return {Object} formatted[0] formData
|
||||||
* @return {String} formatted[1] fileId
|
* @return {String} formatted[1] fileId
|
||||||
|
* @throws Error if Buffer file type is not supported.
|
||||||
|
* @see https://npmjs.com/package/file-type
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
_formatSendData(type, data) {
|
_formatSendData(type, data) {
|
||||||
@ -344,9 +346,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
opts.qs.url = url;
|
opts.qs.url = url;
|
||||||
|
|
||||||
if (cert) {
|
if (cert) {
|
||||||
const [formData, certificate] = this._formatSendData('certificate', cert);
|
try {
|
||||||
opts.formData = formData;
|
const sendData = this._formatSendData('certificate', cert);
|
||||||
opts.qs.certificate = certificate;
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.certificate = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._request('setWebHook', opts)
|
return this._request('setWebHook', opts)
|
||||||
@ -553,9 +559,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options,
|
qs: options,
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('photo', photo);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('photo', photo);
|
||||||
opts.qs.photo = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.photo = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
return this._request('sendPhoto', opts);
|
return this._request('sendPhoto', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -573,9 +583,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options
|
qs: options
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('audio', audio);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('audio', audio);
|
||||||
opts.qs.audio = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.audio = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
return this._request('sendAudio', opts);
|
return this._request('sendAudio', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -594,9 +608,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options
|
qs: options
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('document', doc);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('document', doc);
|
||||||
opts.qs.document = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.document = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
if (opts.formData && Object.keys(fileOpts).length) {
|
if (opts.formData && Object.keys(fileOpts).length) {
|
||||||
opts.formData.document.options = fileOpts;
|
opts.formData.document.options = fileOpts;
|
||||||
}
|
}
|
||||||
@ -617,9 +635,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options
|
qs: options
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('sticker', sticker);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('sticker', sticker);
|
||||||
opts.qs.sticker = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.sticker = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
return this._request('sendSticker', opts);
|
return this._request('sendSticker', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -637,9 +659,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options
|
qs: options
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('video', video);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('video', video);
|
||||||
opts.qs.video = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.video = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
return this._request('sendVideo', opts);
|
return this._request('sendVideo', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -657,9 +683,13 @@ class TelegramBot extends EventEmitter {
|
|||||||
qs: options
|
qs: options
|
||||||
};
|
};
|
||||||
opts.qs.chat_id = chatId;
|
opts.qs.chat_id = chatId;
|
||||||
const content = this._formatSendData('voice', voice);
|
try {
|
||||||
opts.formData = content[0];
|
const sendData = this._formatSendData('voice', voice);
|
||||||
opts.qs.voice = content[1];
|
opts.formData = sendData[0];
|
||||||
|
opts.qs.voice = sendData[1];
|
||||||
|
} catch (ex) {
|
||||||
|
return Promise.reject(ex);
|
||||||
|
}
|
||||||
return this._request('sendVoice', opts);
|
return this._request('sendVoice', opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user