/** * Open webhook. * Multiple invocations do nothing if webhook is already open. * Rejects returned promise if Polling is being used by this instance. * @return {Promise} */ openWebHook() { if (this.isPolling()) { return Promise.reject(new errors.FatalError('WebHook and Polling are mutually exclusive')); } if (!this._webHook) { this._webHook = new TelegramBotWebHook(this); } return this._webHook.open(); }
/* * Retry the function fn up to max times until it successfully completes * without an error. Pause backoff * retry miliseconds between tries. */ const retry = (fn, {max = 5, backoff = 500} = {}) => Promise.resolve().then(() => { const rec = counter => Promise.try(() => fn(counter).catch(err => { if (counter <= max) { return Promise.delay(backoff * counter).then(() => rec(counter + 1)); } else { return Promise.reject(err); } })); // Init recursive function. return rec(1); })
/* * Ping connection. */ ping() { const client = this.client; return Promise.fromNode(cb => { client.ping(cb); }) .then(success => { if (!success) { return Promise.reject({ status: 502, message: 'Could not reach the elasticsearch instance!', }); } }); }
/** * Send Dice * Use this method to send a dice. * @param {Number|String} chatId Unique identifier for the message recipient * @param {Object} [options] Additional Telegram query options * @return {Promise} * @see https://core.telegram.org/bots/api#senddice */ sendDice(chatId, options = {}) { const opts = { qs: options, }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('dice'); opts.formData = sendData[0]; } catch (ex) { return Promise.reject(ex); } return this._request('sendDice', opts); }
/* * Helper to make requests to pantheon api */ const pantheonRequest = (request, log, verb, pathname, data = {}, options = {}) => { // Log the actual request we are about to make log.verbose('making %s request to %s', verb, `${_.get(request, 'defaults.baseURL')}${pathname.join('/')}`); log.debug('request sent data with %j', options, _.clone(data)); // Attempt the request and retry a few times return Promise.retry(() => request[verb](pathname.join('/'), data, options) .then(response => { log.verbose('response recieved: %s with code %s', response.statusText, response.status); log.silly('response data', response.data); return response.data; }) .catch(err => { const data = getErrorData(err); const msg = [ `${data.method} request to ${data.path} failed with code ${data.code}: ${data.codeText}.`, `The server responded with the message ${data.response}.`, ]; return Promise.reject(new Error(msg.join(' '))); }), {max: 2}); }
/** * Start polling. * Rejects returned promise if a WebHook is being used by this instance. * @param {Object} [options] * @param {Boolean} [options.restart=true] Consecutive calls to this method causes polling to be restarted * @return {Promise} */ startPolling(options = {}) { if (this.hasOpenWebHook()) { return Promise.reject(new errors.FatalError('Polling and WebHook are mutually exclusive')); } options.restart = typeof options.restart === 'undefined' ? true : options.restart; if (!this._polling) { this._polling = new TelegramBotPolling(this); } return this._polling.start(options); }
/** * Send .webp stickers. * @param {Number|String} chatId Unique identifier for the message recipient * @param {String|stream.Stream|Buffer} sticker A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. Stickers are WebP format files. * @param {Object} [options] Additional Telegram query options * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendsticker */ sendSticker(chatId, sticker, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('sticker', sticker, fileOptions); opts.formData = sendData[0]; opts.qs.sticker = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendSticker', opts); }
/* * Ping connection. */ ping() { const client = this.client; return Promise.fromNode(cb => { client.ping(cb); }) .then(success => { if (!success) { return Promise.reject({ status: 502, message: 'Could not reach the elasticsearch instance!', }); } }); }
/** * Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). * @param {Number|String} chatId Unique identifier for the message recipient * @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} [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 = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('video', video, fileOptions); opts.formData = sendData[0]; opts.qs.video = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendVideo', opts); }
/** * Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). * @param {Number|String} chatId Unique identifier for the message recipient * @param {String|stream.Stream|Buffer} animation A file path, Stream or Buffer. * Can also be a `file_id` previously uploaded. * @param {Object} [options] Additional Telegram query options * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#sendanimation * @see https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files */ sendAnimation(chatId, animation, options = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('animation', animation, fileOptions); opts.formData = sendData[0]; opts.qs.document = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendAnimation', opts); }
/** * Send photo * @param {Number|String} chatId Unique identifier for the message recipient * @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} [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 = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('photo', photo, fileOptions); opts.formData = sendData[0]; opts.qs.photo = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendPhoto', opts); }
/** * Send voice * @param {Number|String} chatId Unique identifier for the message recipient * @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} [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 = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('voice', voice, fileOptions); opts.formData = sendData[0]; opts.qs.voice = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendVoice', opts); }
/** * Use this method to upload a .png file with a sticker for later use in *createNewStickerSet* and *addStickerToSet* methods (can be used multiple * times). Returns the uploaded [File](https://core.telegram.org/bots/api#file) on success. * * @param {Number} userId User identifier of sticker file owner * @param {String|stream.Stream|Buffer} pngSticker A file path or a Stream. Can also be a `file_id` previously uploaded. **Png** image with the * sticker, must be up to 512 kilobytes in size, dimensions must not exceed 512px, and either width or height must be exactly 512px. * @param {Object} [options] Additional Telegram query options * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#uploadstickerfile */ uploadStickerFile(userId, pngSticker, options = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.user_id = userId; try { const sendData = this._formatSendData('png_sticker', pngSticker, fileOptions); opts.formData = sendData[0]; opts.qs.png_sticker = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('uploadStickerFile', opts); }
/** * Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. * The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. * Returns True on success. * * @param {Number|String} chatId Unique identifier for the message recipient * @param {stream.Stream|Buffer} photo A file path or a Stream. * @param {Object} [options] Additional Telegram query options * @param {Object} [fileOptions] Optional file related meta-data * @return {Promise} * @see https://core.telegram.org/bots/api#setchatphoto */ setChatPhoto(chatId, photo, options = {}, fileOptions = {}) { const opts = { qs: options, }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('photo', photo, fileOptions); opts.formData = sendData[0]; opts.qs.photo = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('setChatPhoto', opts); }
/** * Send audio * @param {Number|String} chatId Unique identifier for the message recipient * @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} [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 = {}, fileOptions = {}) { const opts = { qs: options }; opts.qs.chat_id = chatId; try { const sendData = this._formatSendData('audio', audio, fileOptions); opts.formData = sendData[0]; opts.qs.audio = sendData[1]; } catch (ex) { return Promise.reject(ex); } return this._request('sendAudio', opts); }