Skip to content

Commit

Permalink
Merge pull request #153 from Yoctol/telegram-payments-api
Browse files Browse the repository at this point in the history
add Telegram payments api
  • Loading branch information
tw0517tw authored Sep 28, 2017
2 parents f702a55 + e779e09 commit 57e66e4
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 3 deletions.
72 changes: 72 additions & 0 deletions packages/messaging-api-telegram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* [Send API](#send-api)
* [Get API](#get-api)
* [Updating API](#updating-api)
* [Payments API](#payments-api)
* [Others](#others)

## Installation
Expand Down Expand Up @@ -739,6 +740,77 @@ client.leaveChat(CHAT_ID);

<br />

### Payments API

## `sendInvoice(chatId, product [, options])` - [Official Docs](https://core.telegram.org/bots/api/#sendinvoice)

Sends invoice.

Param | Type | Description
----------------------- | --------------------------------- | -----------
chatId | <code>Number &#124; String</code> | Unique identifier for the target chat or username of the target channel.
product | `Object` | Object of the product.
product.title | `String` | Product name.
product.description | `String` | Product description.
product.payload | `String` | Bot defined invoice payload.
product.provider_token | `String` | Payments provider token.
product.start_parameter | `String` | Deep-linking parameter.
product.currency | `String` | Three-letter ISO 4217 currency code.
product.prices | `Array<Object>` | Breakdown of prices.
options | `Object` | Additional Telegram query options.

Example:
```js
client.sendInvoice(CHAT_ID, {
title: 'product name',
description: 'product description',
payload: 'bot-defined invoice payload',
provider_token: 'PROVIDER_TOKEN',
start_parameter: 'pay',
currency: 'USD',
prices: [
{ label: 'product', amount: 11000 },
{ label: 'tax', amount: 11000 }
]
});
```

<br />

## `answerShippingQuery(shippingQueryId, ok [, options])` - [Official Docs](https://core.telegram.org/bots/api/#answershippingquery)

Reply to shipping queries.

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.

Example:
```js
client.answerShippingQuery('UNIQUE_ID', true);
```

<br />

## `answerPreCheckoutQuery(preCheckoutQueryId, ok [, options])` - [Official Docs](https://core.telegram.org/bots/api/#answerprecheckoutquery)

Respond to such pre-checkout queries.

Param | Type | Description
------------------ | --------- | -----------
preCheckoutQueryId | `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.

Example:
```js
client.answerPreCheckoutQuery('UNIQUE_ID', true);
```

<br />

### Others

## `forwardMessage(chatId, fromChatId, messageId [, options])` - [Official Docs](https://core.telegram.org/bots/api/#forwardmessage)
Expand Down
64 changes: 61 additions & 3 deletions packages/messaging-api-telegram/src/TelegramClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export default class TelegramClient {
*/
sendLocation = (
chatId: string,
{ latitude, longitude }: { latitude: number, longitude: number },
{ latitude, longitude }: {| latitude: number, longitude: number |},
options?: Object
) =>
this._request('/sendLocation', {
Expand All @@ -220,7 +220,12 @@ export default class TelegramClient {
longitude,
title,
address,
}: { latitude: number, longitude: number, title: string, address: string },
}: {|
latitude: number,
longitude: number,
title: string,
address: string,
|},
options?: Object
) =>
this._request('/sendVenue', {
Expand All @@ -237,7 +242,10 @@ export default class TelegramClient {
*/
sendContact = (
chatId: string,
{ phone_number, first_name }: { phone_number: string, first_name: string },
{
phone_number,
first_name,
}: {| phone_number: string, first_name: string |},
options?: Object
) =>
this._request('/sendContact', {
Expand Down Expand Up @@ -415,4 +423,54 @@ export default class TelegramClient {
message_id: messageId,
...options,
});

/**
* https://core.telegram.org/bots/api#sendinvoice
*/
sendInvoice = (
chatId: string,
product: {|
title: string,
description: string,
payload: string,
provider_token: string,
start_parameter: string,
currency: string,
prices: Array<Object>,
|},
options?: Object
) =>
this._request('/sendInvoice', {
chat_id: chatId,
...product,
...options,
});

/**
* https://core.telegram.org/bots/api#answershippingquery
*/
answerShippingQuery = (
shippingQueryId: string,
ok: boolean,
options?: Object
) =>
this._request('/answerShippingQuery', {
shipping_query_id: shippingQueryId,
ok,
...options,
});

/**
* https://core.telegram.org/bots/api#answerprecheckoutquery
*/
answerPreCheckoutQuery = (
preCheckoutQueryId: string,
ok: boolean,
options?: Object
) =>
this._request('/answerPreCheckoutQuery', {
pre_checkout_query_id: preCheckoutQueryId,
ok,
...options,
});
}
104 changes: 104 additions & 0 deletions packages/messaging-api-telegram/src/__tests__/TelegramClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1346,6 +1346,110 @@ describe('group api', () => {
});
});

describe('payment api', () => {
describe('#sendInvoice', () => {
it('should send invoice message to user', async () => {
const { client, mock } = createMock();
const reply = {
ok: true,
result: {
message_id: 1,
from: {
id: 313534466,
first_name: 'first',
username: 'a_bot',
},
chat: {
id: 427770117,
first_name: 'first',
last_name: 'last',
type: 'private',
},
date: 1499403678,
invoice: {
title: 'product name',
description: 'product description',
start_parameter: 'pay',
currency: 'USD',
total_count: 22000,
},
},
};

mock
.onPost('/sendInvoice', {
chat_id: 427770117,
title: 'product name',
description: 'product description',
payload: 'bot-defined invoice payload',
provider_token: 'PROVIDER_TOKEN',
start_parameter: 'pay',
currency: 'USD',
prices: [
{ label: 'product', amount: 11000 },
{ label: 'tax', amount: 11000 },
],
})
.reply(200, reply);

const res = await client.sendInvoice(427770117, {
title: 'product name',
description: 'product description',
payload: 'bot-defined invoice payload',
provider_token: 'PROVIDER_TOKEN',
start_parameter: 'pay',
currency: 'USD',
prices: [
{ label: 'product', amount: 11000 },
{ label: 'tax', amount: 11000 },
],
});

expect(res).toEqual(reply);
});
});

describe('#answerShippingQuery', () => {
it('should export chat invite link', async () => {
const { client, mock } = createMock();
const reply = {
ok: true,
result: true,
};

mock
.onPost('/answerShippingQuery', {
shipping_query_id: 'UNIQUE_ID',
ok: true,
})
.reply(200, reply);

const res = await client.answerShippingQuery('UNIQUE_ID', true);
expect(res).toEqual(reply);
});
});

describe('#answerPreCheckoutQuery', () => {
it('should respond to such pre-checkout queries', async () => {
const { client, mock } = createMock();
const reply = {
ok: true,
result: true,
};

mock
.onPost('/answerPreCheckoutQuery', {
pre_checkout_query_id: 'UNIQUE_ID',
ok: true,
})
.reply(200, reply);

const res = await client.answerPreCheckoutQuery('UNIQUE_ID', true);
expect(res).toEqual(reply);
});
});
});

describe('other api', () => {
describe('#forwardMessage', () => {
it('should forward messages of any kind', async () => {
Expand Down

0 comments on commit 57e66e4

Please sign in to comment.