Skip to content

Commit

Permalink
Features and Enhancement (line#147)
Browse files Browse the repository at this point in the history
Features:
- Get the target limit for additional messages
- Get number of messages sent this month
- Get number of sent broadcast messages

Enhancement:
- Reply message
- Send push message
- Send multicast message
- Send broadcast message
  • Loading branch information
SombreroElGringo authored and xingoxu committed Jun 20, 2019
1 parent 09d95b0 commit c5086af
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 7 deletions.
75 changes: 69 additions & 6 deletions docs/api-reference/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Client {
constructor(config: ClientConfig) {}

// Message
pushMessage(to: string, messages: Message | Message[]): Promise<any>
replyMessage(replyToken: string, messages: Message | Message[]): Promise<any>
multicast(to: string[], messages: Message | Message[]): Promise<any>
pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
broadcast(messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>
getMessageContent(messageId: string): Promise<Readable>

// Profile
Expand Down Expand Up @@ -53,6 +54,9 @@ class Client {
getNumberOfSentReplyMessages(date: string): Promise<NumberOfMessagesSentResponse>
getNumberOfSentPushMessages(date: string): Promise<NumberOfMessagesSentResponse>
getNumberOfSentMulticastMessages(date: string): Promise<NumberOfMessagesSentResponse>
getTargetLimitForAdditionalMessages(): Promise<TargetLimitForAdditionalMessages>
getNumberOfMessagesSentThisMonth(): Promise<NumberOfMessagesSentThisMonth>
getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfSentBroadcastMessages>
}
```

Expand Down Expand Up @@ -84,7 +88,7 @@ in [the Client guide](../guide/client.md).

### Message

#### `pushMessage(to: string, messages: Message | Message[]): Promise<any>`
#### `pushMessage(to: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`

It corresponds to the [Push message](https://developers.line.me/en/docs/messaging-api/reference/#send-push-message) API.

Expand All @@ -97,7 +101,7 @@ client.pushMessage('user_or_group_or_room_id', {
})
```

#### `replyMessage(replyToken: string, messages: Message | Message[]): Promise<any>`
#### `replyMessage(replyToken: string, messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`

It corresponds to the [Reply message](https://developers.line.me/en/docs/messaging-api/reference/#send-reply-message) API.

Expand All @@ -112,7 +116,7 @@ client.replyMessage(event.replyToken, {
})
```

#### `multicast(to: string[], messages: Message | Message[]): Promise<any>`
#### `multicast(to: string[], messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`

It corresponds to the [Multicast](https://developers.line.me/en/docs/messaging-api/reference/#send-multicast-messages) API.

Expand All @@ -126,6 +130,19 @@ client.multicast(['user_id_1', 'user_id_2', 'room_id_1'], {
})
```

#### `broadcast(messages: Message | Message[], notificationDisabled: boolean = false): Promise<any>`

Sends push messages to multiple users at any time.

Note: LINE@ accounts cannot call this API endpoint. Please migrate it to a LINE official account. For more information, see [Migration of LINE@ accounts](https://developers.line.biz/en/docs/messaging-api/migrating-line-at/).

``` js
client.broadcast({
type: 'text',
text: 'hello, world',
})
```

#### `getMessageContent(messageId: string): Promise<Readable>`

It corresponds to the [Content](https://developers.line.me/en/docs/messaging-api/reference/#get-content) API.
Expand Down Expand Up @@ -435,3 +452,49 @@ client.getNumberOfSentMulticastMessages('20191231').then((response) => {
console.log(response);
})
```

#### `getTargetLimitForAdditionalMessages(): Promise<TargetLimitForAdditionalMessages>`

Gets the target limit for additional messages in the current month.

The number of messages retrieved by this operation includes the number of messages sent from LINE Official Account Manager.

Set a target limit with LINE Official Account Manager. For the procedures, refer to the LINE Official Account Manager manual.

Note: LINE@ accounts cannot call this API endpoint.

``` js
client.getTargetLimitForAdditionalMessages().then((response) => {
console.log(response);
})
```

#### `getNumberOfMessagesSentThisMonth(): Promise<NumberOfMessagesSentThisMonth>`

Gets the number of messages sent in the current month.

The number of messages retrieved by this operation includes the number of messages sent from LINE Official Account Manager.

The number of messages retrieved by this operation is approximate. To get the correct number of sent messages, use LINE Official Account Manager or execute API operations for getting the number of sent messages.

Note: LINE@ accounts cannot call this API endpoint.

``` js
client.getNumberOfMessagesSentThisMonth().then((response) => {
console.log(response);
})
```

#### `getNumberOfSentBroadcastMessages(date: string): Promise<NumberOfSentBroadcastMessages>`

Gets the number of messages sent with the `/bot/message/broadcast` endpoint.

The number of messages retrieved by this operation does not include the number of messages sent from LINE Official Account Manager.

Note: LINE@ accounts cannot call this API endpoint. Please migrate it to a LINE official account. For more information, see [Migration of LINE@ accounts](https://developers.line.biz/en/docs/messaging-api/migrating-line-at/).

``` js
client.getNumberOfSentBroadcastMessages('20191231').then((response) => {
console.log(response);
})
```
43 changes: 43 additions & 0 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,46 @@ export default class Client {
public async pushMessage(
to: string,
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<any> {
return this.http.post("/message/push", {
messages: toArray(messages),
to,
notificationDisabled,
});
}

public async replyMessage(
replyToken: string,
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<any> {
return this.http.post("/message/reply", {
messages: toArray(messages),
replyToken,
notificationDisabled,
});
}

public async multicast(
to: string[],
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<any> {
return this.http.post("/message/multicast", {
messages: toArray(messages),
to,
notificationDisabled,
});
}

public async broadcast(
messages: Types.Message | Types.Message[],
notificationDisabled: boolean = false,
): Promise<any> {
return this.http.post("/message/broadcast", {
messages: toArray(messages),
notificationDisabled,
});
}

Expand Down Expand Up @@ -256,4 +272,31 @@ export default class Client {
);
return ensureJSON(res);
}

public async getTargetLimitForAdditionalMessages(): Promise<
Types.TargetLimitForAdditionalMessages
> {
const res = await this.http.get<Types.TargetLimitForAdditionalMessages>(
"/message/quota",
);
return ensureJSON(res);
}

public async getNumberOfMessagesSentThisMonth(): Promise<
Types.NumberOfMessagesSentThisMonth
> {
const res = await this.http.get<Types.NumberOfMessagesSentThisMonth>(
"/message/quota/consumption",
);
return ensureJSON(res);
}

public async getNumberOfSentBroadcastMessages(
date: string,
): Promise<Types.NumberOfSentBroadcastMessages> {
const res = await this.http.get<Types.NumberOfSentBroadcastMessages>(
`/message/delivery/broadcast?date=${date}`,
);
return ensureJSON(res);
}
}
38 changes: 38 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1762,3 +1762,41 @@ export type NumberOfMessagesSentResponse = {
*/
success?: number;
};

export type TargetLimitForAdditionalMessages = {
/**
* One of the following values to indicate whether a target limit is set or not.
* - `none`: This indicates that a target limit is not set.
* - `limited`: This indicates that a target limit is set.
*/
type: "none" | "limited";
/**
* The target limit for additional messages in the current month.
* This property is returned when the `type` property has a value of `limited`.
*/
value?: number;
};

export type NumberOfMessagesSentThisMonth = {
/**
* The number of sent messages in the current month
*/
totalUsage: number;
};

export type NumberOfSentBroadcastMessages = {
/**
* Status of the counting process. One of the following values is returned:
* - `ready`: You can get the number of messages.
* - `unready`: The message counting process for the date specified in date has not been completed yet.
* Retry your request later. Normally, the counting process is completed within the next day.
* - `out_of_service`: The date specified in date is earlier than March 31, 2018,
* when the operation of the counting system started.
*/
status: "ready" | "unready" | "out_of_service";
/**
* The number of messages sent with the Messaging API on the date specified in `date`.
* The response has this property only when the value of `status` is `ready`.
*/
success?: number;
};
37 changes: 36 additions & 1 deletion test/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFileSync } from "fs";
import { join } from "path";
import { deepEqual, equal } from "assert";
import { Readable } from "stream";
import Client from "../lib/client";
import * as Types from "../lib/types";
import { getStreamData } from "./helpers/stream";
Expand Down Expand Up @@ -84,6 +83,16 @@ describe("client", () => {
deepEqual(res, {});
});

it("broadcast", async () => {
const res = await client.broadcast([testMsg, testMsg]);
const req = getRecentReq();
equal(req.headers.authorization, "Bearer test_channel_access_token");
equal(req.path, "/message/broadcast");
equal(req.method, "POST");
deepEqual(req.body.messages, [testMsg, testMsg]);
deepEqual(res, {});
});

it("getProfile", async () => {
const res = await client.getProfile("test_user_id");
const req = getRecentReq();
Expand Down Expand Up @@ -362,4 +371,30 @@ describe("client", () => {
equal(req.query.date, date);
equal(req.method, "GET");
});

it("getTargetLimitForAdditionalMessages", async () => {
await client.getTargetLimitForAdditionalMessages();
const req = getRecentReq();
equal(req.headers.authorization, "Bearer test_channel_access_token");
equal(req.path, "/message/quota");
equal(req.method, "GET");
});

it("getNumberOfMessagesSentThisMonth", async () => {
await client.getNumberOfMessagesSentThisMonth();
const req = getRecentReq();
equal(req.headers.authorization, "Bearer test_channel_access_token");
equal(req.path, "/message/quota/consumption");
equal(req.method, "GET");
});

it("getNumberOfSentBroadcastMessages", async () => {
const date = "20191231";
await client.getNumberOfSentBroadcastMessages(date);
const req = getRecentReq();
equal(req.headers.authorization, "Bearer test_channel_access_token");
equal(req.path, "/message/delivery/broadcast");
equal(req.query.date, date);
equal(req.method, "GET");
});
});

0 comments on commit c5086af

Please sign in to comment.