diff --git a/CHANGELOG.md b/CHANGELOG.md index 08deb09..54d0a5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,13 @@ Changed: 1. Update *TelegramBot#answerCallbackQuery()* signature (by @GochoMugo) 1. Improve default error logging of `polling_error` and `webhook_error` +Deprecated: + +1. Sending files: *(See [usage guide][usage-sending-file])* (by @hufan-akari, @GochoMugo) + * Error will **not** be thrown if `Buffer` is used and file-type could **not** be detected. + * Filename will **not** be set to `data.${ext}` if `Buffer` is used + * Content type will **not** default to `null` or `undefined` + Fixed: 1. Fix game example (by @MCSH) @@ -184,6 +191,8 @@ Fixed: 1. Fix typos (by oflisback) +[usage-sending-files]:https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files-options + [0.25.0]:https://github.com/yagop/node-telegram-bot-api/releases/tag/v0.25.0 [0.26.0]:https://github.com/yagop/node-telegram-bot-api/releases/tag/v0.26.0 [0.27.0]:https://github.com/yagop/node-telegram-bot-api/releases/tag/v0.27.0 diff --git a/doc/api.md b/doc/api.md index 031db25..7834fbe 100644 --- a/doc/api.md +++ b/doc/api.md @@ -30,13 +30,13 @@ TelegramBot * [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ Promise * [.answerInlineQuery(inlineQueryId, results, [options])](#TelegramBot+answerInlineQuery) ⇒ Promise * [.forwardMessage(chatId, fromChatId, messageId, [options])](#TelegramBot+forwardMessage) ⇒ Promise - * [.sendPhoto(chatId, photo, [options], [fileOpts])](#TelegramBot+sendPhoto) ⇒ Promise - * [.sendAudio(chatId, audio, [options], [fileOpts])](#TelegramBot+sendAudio) ⇒ Promise - * [.sendDocument(chatId, doc, [options], [fileOpts])](#TelegramBot+sendDocument) ⇒ Promise + * [.sendPhoto(chatId, photo, [options], [fileOptions])](#TelegramBot+sendPhoto) ⇒ Promise + * [.sendAudio(chatId, audio, [options], [fileOptions])](#TelegramBot+sendAudio) ⇒ Promise + * [.sendDocument(chatId, doc, [options], [fileOptions])](#TelegramBot+sendDocument) ⇒ Promise * [.sendSticker(chatId, sticker, [options])](#TelegramBot+sendSticker) ⇒ Promise - * [.sendVideo(chatId, video, [options], [fileOpts])](#TelegramBot+sendVideo) ⇒ Promise - * [.sendVideoNote(chatId, videoNote, [options], [fileOpts])](#TelegramBot+sendVideoNote) ⇒ Promise - * [.sendVoice(chatId, voice, [options], [fileOpts])](#TelegramBot+sendVoice) ⇒ Promise + * [.sendVideo(chatId, video, [options], [fileOptions])](#TelegramBot+sendVideo) ⇒ Promise + * [.sendVideoNote(chatId, videoNote, [options], [fileOptions])](#TelegramBot+sendVideoNote) ⇒ Promise + * [.sendVoice(chatId, voice, [options], [fileOptions])](#TelegramBot+sendVoice) ⇒ Promise * [.sendChatAction(chatId, action, [options])](#TelegramBot+sendChatAction) ⇒ Promise * [.kickChatMember(chatId, userId, [options])](#TelegramBot+kickChatMember) ⇒ Promise * [.unbanChatMember(chatId, userId, [options])](#TelegramBot+unbanChatMember) ⇒ Promise @@ -339,7 +339,7 @@ Forward messages of any kind. -### telegramBot.sendPhoto(chatId, photo, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendPhoto(chatId, photo, [options], [fileOptions]) ⇒ Promise Send photo **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -354,11 +354,11 @@ Send photo | chatId | Number | String | Unique identifier for the message recipient | | photo | String | stream.Stream | Buffer | A file path or a Stream. Can also be a `file_id` previously uploaded | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.sendAudio(chatId, audio, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendAudio(chatId, audio, [options], [fileOptions]) ⇒ Promise Send audio **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -373,11 +373,11 @@ Send audio | chatId | Number | String | Unique identifier for the message recipient | | audio | String | stream.Stream | Buffer | A file path, Stream or Buffer. Can also be a `file_id` previously uploaded. | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.sendDocument(chatId, doc, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendDocument(chatId, doc, [options], [fileOptions]) ⇒ Promise Send Document **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -392,7 +392,7 @@ Send Document | chatId | Number | String | Unique identifier for the message recipient | | doc | String | stream.Stream | Buffer | A file path, Stream or Buffer. Can also be a `file_id` previously uploaded. | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | @@ -410,7 +410,7 @@ Send .webp stickers. -### telegramBot.sendVideo(chatId, video, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendVideo(chatId, video, [options], [fileOptions]) ⇒ Promise Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -425,11 +425,11 @@ Use this method to send video files, Telegram clients support mp4 videos (other | chatId | Number | String | Unique identifier for the message recipient | | video | String | stream.Stream | Buffer | A file path or Stream. Can also be a `file_id` previously uploaded. | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.sendVideoNote(chatId, videoNote, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendVideoNote(chatId, videoNote, [options], [fileOptions]) ⇒ Promise Use this method to send rounded square videos of upto 1 minute long. **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -445,11 +445,11 @@ Use this method to send rounded square videos of upto 1 minute long. | chatId | Number | String | Unique identifier for the message recipient | | videoNote | String | stream.Stream | Buffer | A file path or Stream. Can also be a `file_id` previously uploaded. | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | -### telegramBot.sendVoice(chatId, voice, [options], [fileOpts]) ⇒ Promise +### telegramBot.sendVoice(chatId, voice, [options], [fileOptions]) ⇒ Promise Send voice **Kind**: instance method of [TelegramBot](#TelegramBot) @@ -464,7 +464,7 @@ Send voice | chatId | Number | String | Unique identifier for the message recipient | | voice | String | stream.Stream | Buffer | A file path, Stream or Buffer. Can also be a `file_id` previously uploaded. | | [options] | Object | Additional Telegram query options | -| [fileOpts] | Object | Optional file related meta-data | +| [fileOptions] | Object | Optional file related meta-data | diff --git a/doc/usage.md b/doc/usage.md index e3499d3..d6247b1 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -129,7 +129,7 @@ const fileOpts = { bot.sendAudio(chatId, data, {}, fileOpts); ``` - + ### File Options (metadata) When sending files, the library automatically resolves diff --git a/src/telegram.js b/src/telegram.js index 3d011bd..426737c 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -284,9 +284,9 @@ class TelegramBot extends EventEmitter { * Format data to be uploaded; handles file paths, streams and buffers * @param {String} type * @param {String|stream.Stream|Buffer} data - * @param {Object} fileOpts File options - * @param {String} [fileOpts.filename] File name - * @param {String} [fileOpts.contentType] Content type (i.e. MIME) + * @param {Object} fileOptions File options + * @param {String} [fileOptions.filename] File name + * @param {String} [fileOptions.contentType] Content type (i.e. MIME) * @return {Array} formatted * @return {Object} formatted[0] formData * @return {String} formatted[1] fileId @@ -294,17 +294,19 @@ class TelegramBot extends EventEmitter { * @see https://npmjs.com/package/file-type * @private */ - _formatSendData(type, data, fileOpts = {}) { + _formatSendData(type, data, fileOptions = {}) { let filedata = data; - let filename = fileOpts.filename; - let contentType = fileOpts.contentType; + let filename = fileOptions.filename; + let contentType = fileOptions.contentType; if (data instanceof stream.Stream) { if (!filename && data.path) { // Will be 'null' if could not be parsed. // For example, 'data.path' === '/?id=123' from 'request("https://example.com/?id=123")' const url = URL.parse(path.basename(data.path.toString())); - filename = qs.unescape(url.pathname); + if (url.pathname) { + filename = qs.unescape(url.pathname); + } } } else if (Buffer.isBuffer(data)) { if (!filename && !process.env.NTBA_FIX_350) { @@ -701,18 +703,18 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} photo A file path or a Stream. Can * also be a `file_id` previously uploaded * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendphoto * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendPhoto(chatId, photo, options = {}, fileOpts = {}) { + sendPhoto(chatId, photo, options = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('photo', photo, fileOpts); + const sendData = this._formatSendData('photo', photo, fileOptions); opts.formData = sendData[0]; opts.qs.photo = sendData[1]; } catch (ex) { @@ -727,18 +729,18 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} audio A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendaudio * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendAudio(chatId, audio, options = {}, fileOpts = {}) { + sendAudio(chatId, audio, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('audio', audio, fileOpts); + const sendData = this._formatSendData('audio', audio, fileOptions); opts.formData = sendData[0]; opts.qs.audio = sendData[1]; } catch (ex) { @@ -753,18 +755,18 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} doc A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendDocument * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendDocument(chatId, doc, options = {}, fileOpts = {}) { + sendDocument(chatId, doc, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('document', doc, fileOpts); + const sendData = this._formatSendData('document', doc, fileOptions); opts.formData = sendData[0]; opts.qs.document = sendData[1]; } catch (ex) { @@ -803,18 +805,18 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} video A file path or Stream. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendvideo * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendVideo(chatId, video, options = {}, fileOpts = {}) { + sendVideo(chatId, video, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('video', video, fileOpts); + const sendData = this._formatSendData('video', video, fileOptions); opts.formData = sendData[0]; opts.qs.video = sendData[1]; } catch (ex) { @@ -829,19 +831,19 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} videoNote A file path or Stream. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @info The length parameter is actually optional. However, the API (at time of writing) requires you to always provide it until it is fixed. * @see https://core.telegram.org/bots/api#sendvideonote * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendVideoNote(chatId, videoNote, options = {}, fileOpts = {}) { + sendVideoNote(chatId, videoNote, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('video_note', videoNote, fileOpts); + const sendData = this._formatSendData('video_note', videoNote, fileOptions); opts.formData = sendData[0]; opts.qs.video_note = sendData[1]; } catch (ex) { @@ -856,18 +858,18 @@ class TelegramBot extends EventEmitter { * @param {String|stream.Stream|Buffer} voice A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options - * @param {Object} [fileOpts] Optional file related meta-data + * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendvoice * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ - sendVoice(chatId, voice, options = {}, fileOpts = {}) { + sendVoice(chatId, voice, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { - const sendData = this._formatSendData('voice', voice, fileOpts); + const sendData = this._formatSendData('voice', voice, fileOptions); opts.formData = sendData[0]; opts.qs.voice = sendData[1]; } catch (ex) { diff --git a/test/test.sendfile.js b/test/test.sendfile.js index 864d900..41fa94c 100644 --- a/test/test.sendfile.js +++ b/test/test.sendfile.js @@ -4,13 +4,13 @@ const path = require('path'); const TelegramBot = require('..'); const paths = { - audio: path.join(__dirname, "data/audio.mp3"), + audio: path.join(__dirname, 'data/audio.mp3'), }; // TODO:Enable all other tests describe.only('sending files', function sendfileSuite() { - const bot = new TelegramBot("token"); + const bot = new TelegramBot('token '); before(function beforeSuite() { process.env.NTBA_FIX_350 = 1; @@ -26,7 +26,6 @@ describe.only('sending files', function sendfileSuite() { const buffer = fs.readFileSync(paths.audio); const nonDetectableBuffer = fs.readFileSync(__filename); const filepath = paths.audio; - const fileId = 'fileId'; const files = [stream, nonPathStream, buffer, nonDetectableBuffer, filepath]; delete nonPathStream.path; @@ -42,7 +41,8 @@ describe.only('sending files', function sendfileSuite() { it('(2) Stream#path', function test() { if (!stream.path) { - return this.skip('Stream#path unsupported'); + this.skip('Stream#path unsupported'); + return; } const [{ [type]: data }] = bot._formatSendData(type, stream); assert.equal(data.options.filename, path.basename(paths.audio)); @@ -72,7 +72,8 @@ describe.only('sending files', function sendfileSuite() { it('(2) Stream#path', function test() { if (!stream.path) { - return this.skip('Stream#path unsupported'); + this.skip('Stream#path unsupported'); + return; } const [{ [type]: data }] = bot._formatSendData(type, stream); assert.equal(data.options.contentType, 'audio/mpeg'); @@ -104,6 +105,5 @@ describe.only('sending files', function sendfileSuite() { }); }); }); - }); });