diff --git a/doc/api.md b/doc/api.md index 44807ff..47c3d6e 100644 --- a/doc/api.md +++ b/doc/api.md @@ -62,6 +62,9 @@ TelegramBot * [.setGameScore(userId, score, [options])](#TelegramBot+setGameScore) ⇒ Promise * [.getGameHighScores(userId, [options])](#TelegramBot+getGameHighScores) ⇒ Promise * [.deleteMessage(chatId, messageId, [options])](#TelegramBot+deleteMessage) ⇒ Promise + * [.sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, [options])](#TelegramBot+sendInvoice) ⇒ Promise + * [.answerShippingQuery(shippingQueryId, ok, [options])](#TelegramBot+answerShippingQuery) ⇒ Promise + * [.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options])](#TelegramBot+answerPreCheckoutQuery) ⇒ Promise @@ -775,6 +778,57 @@ Use this method to delete a message. | messageId | String | Unique identifier of the target message | | [options] | Object | Additional Telegram query options | + + +### telegramBot.sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, [options]) ⇒ Promise +Send invoice. +Use this method to send an invoice. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#sendinvoice + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number | String | Unique identifier for the message recipient | +| title | String | Product name | +| description | String | product description | +| payload | String | Bot defined invoice payload | +| providerToken | String | Payments provider token | +| startParameter | String | Deep-linking parameter | +| currency | String | Three-letter ISO 4217 currency code | +| prices | Array | Breakdown of prices | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.answerShippingQuery(shippingQueryId, ok, [options]) ⇒ Promise +Answer shipping query.. +Use this method to reply to shipping queries. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#answershippingquery + +| Param | Type | Description | +| --- | --- | --- | +| shippingQueryId | String | Unique identifier for the query to be answered | +| ok | Boolean | Specify if delivery of the product is possible | +| [options] | Object | Additional Telegram query options | + + + +### telegramBot.answerPreCheckoutQuery(preCheckoutQueryId, ok, [options]) ⇒ Promise +Answer pre-checkout query. +Use this method to confirm shipping of a product. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#answerprecheckoutquery + +| Param | Type | Description | +| --- | --- | --- | +| preCheckoutQueryId | String | Unique identifier for the query to be answered | +| ok | Boolean | Specify if every order details are ok | +| [options] | Object | Additional Telegram query options | + * * * diff --git a/src/telegram.js b/src/telegram.js index a204a7c..e9095d5 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -1227,6 +1227,66 @@ class TelegramBot extends EventEmitter { form.message_id = messageId; return this._request('deleteMessage', { form }); } + + /** + * Send invoice. + * Use this method to send an invoice. + * + * @param {Number|String} chatId Unique identifier for the message recipient + * @param {String} title Product name + * @param {String} description product description + * @param {String} payload Bot defined invoice payload + * @param {String} providerToken Payments provider token + * @param {String} startParameter Deep-linking parameter + * @param {String} currency Three-letter ISO 4217 currency code + * @param {Array} prices Breakdown of prices + * @param {Object} [options] Additional Telegram query options + * @return {Promise} + * @see https://core.telegram.org/bots/api#sendinvoice + */ + sendInvoice(chatId, title, description, payload, providerToken, startParameter, currency, prices, form = {}) { + form.chat_id = chatId; + form.title = title; + form.description = description; + form.payload = payload; + form.provider_token = providerToken; + form.start_parameter = startParameter; + form.currency = currency; + form.prices = JSON.stringify(prices); + return this._request('sendInvoice', { form }); + } + + /** + * Answer shipping query.. + * Use this method to reply to shipping queries. + * + * @param {String} shippingQueryId Unique identifier for the query to be answered + * @param {Boolean} ok Specify if delivery of the product is possible + * @param {Object} [options] Additional Telegram query options + * @return {Promise} + * @see https://core.telegram.org/bots/api#answershippingquery + */ + answerShippingQuery(shippingQueryId, ok, form = {}) { + form.shipping_query_id = shippingQueryId; + form.ok = ok; + return this._request('answerShippingQuery', { form }); + } + + /** + * Answer pre-checkout query. + * Use this method to confirm shipping of a product. + * + * @param {String} preCheckoutQueryId Unique identifier for the query to be answered + * @param {Boolean} ok Specify if every order details are ok + * @param {Object} [options] Additional Telegram query options + * @return {Promise} + * @see https://core.telegram.org/bots/api#answerprecheckoutquery + */ + answerPreCheckoutQuery(preCheckoutQueryId, ok, form = {}) { + form.pre_checkout_query_id = preCheckoutQueryId; + form.ok = ok; + return this._request('answerPreCheckoutQuery', { form }); + } } module.exports = TelegramBot; diff --git a/test/README.md b/test/README.md index 382e502..db6a07c 100644 --- a/test/README.md +++ b/test/README.md @@ -13,6 +13,9 @@ export TEST_GROUP_ID= # Game short name which to use in some of the tests, e.g. TelegramBot#sendGame() export TEST_GAME_SHORT_NAME= +# Payment provider token to be used +export TEST_PROVIDER_TOKEN= + # Run ALL tests npm run test diff --git a/test/telegram.js b/test/telegram.js index 92e0451..5f445ab 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -16,6 +16,11 @@ if (!TOKEN) { throw new Error('Bot token not provided'); } +const PROVIDER_TOKEN = process.env.TEST_PROVIDER_TOKEN; +if (!PROVIDER_TOKEN) { + throw new Error('Provider token not supplied'); +} + // Telegram service if not User Id const USERID = process.env.TEST_USER_ID || 777000; const GROUPID = process.env.TEST_GROUP_ID || -1001075450562; @@ -1206,4 +1211,28 @@ describe('TelegramBot', function telegramSuite() { return bot.sendPhoto(USERID, stream); }); }); + + describe('#sendInvoice', function sendInvoiceSuite() { + before(function before() { + utils.handleRatelimit(bot, 'sendInvoice', this); + }); + it('should send an invoice', function test() { + const title = 'Demo product'; + const description = 'our test product'; + const payload = 'sku-p001'; + const providerToken = PROVIDER_TOKEN; + const startParameter = 'pay'; + const currency = 'KES'; + const prices = [{ label: 'product', amount: 11000 }, { label: 'tax', amount: 11000 }]; + return bot.sendInvoice(USERID, title, description, payload, providerToken, startParameter, currency, prices).then(resp => { + assert.ok(is.object(resp)); + assert.ok(is.object(resp.invoice)); + assert.ok(is.number(resp.invoice.total_amount)); + }); + }); + }); + + describe.skip('#answerShippingQuery', function answerShippingQuerySuite() {}); + + describe.skip('#answerPreCheckoutQuery', function answerPreCheckoutQuerySuite() {}); }); // End Telegram