Skip to content

Commit faba233

Browse files
dlacktyxingoxu
andauthored
Webhook APIs (#264)
* Implement getBotInfo API (#261) * Add getBotInfo API support * Update doc for getBotInfo Co-authored-by: xingo xu <xingoxu@users.noreply.github.com> * Implement testWebhookEndpoint API * Implement setWebhookEndpointUrl API Co-authored-by: xingo xu <xingoxu@users.noreply.github.com>
1 parent 6ea9e48 commit faba233

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

docs/api-reference/client.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Client {
5454
linkRichMenuToUser(userId: string, richMenuId: string): Promise<any>
5555
unlinkRichMenuFromUser(userId: string, richMenuId: string): Promise<any>
5656
linkRichMenuToMultipleUsers(richMenuId: string, userIds: string[]): Promise<any>
57-
unlinkRichMenusFromMultipleUsers(userIds: string[]): Promise<any>
57+
unlinkRichMenusFromMultipleUsers(userIds: string[]): Promise<any>
5858
getRichMenuImage(richMenuId: string): Promise<Readable>
5959
setRichMenuImage(richMenuId: string, data: Buffer | Readable, contentType?: string): Promise<any>
6060
getRichMenuList(): Promise<Array<RichMenuResponse>>
@@ -175,6 +175,20 @@ class Client {
175175

176176
// Bot
177177
getBotInfo(): Promise<BotInfoResponse>
178+
179+
// Webhook
180+
setWebhookEndpointUrl(endpoint: string): Promise<{}>
181+
getWebhookEndpointInfo(): Promise<{
182+
endpoint: string;
183+
active: boolean;
184+
}>
185+
testWebhookEndpoint(endpoint?: string): Promise<{
186+
success: boolean;
187+
timestamp: string;
188+
statusCode: number;
189+
reason: string;
190+
detail: string;
191+
}>
178192
}
179193
```
180194

@@ -676,3 +690,18 @@ It corresponds to the [Get friend demographics](https://developers.line.biz/en/r
676690
#### `getBotInfo(): Promise<BotInfoResponse>`
677691

678692
It corresponds to the [Get bot info](https://developers.line.biz/en/reference/messaging-api/#get-bot-info) API.
693+
694+
### Webhook
695+
696+
#### `setWebhookEndpointUrl(endpoint: string): Promise<{}}>`
697+
698+
It corresponds to the [Set webhook endpoint URL](https://developers.line.biz/en/reference/messaging-api/#set-webhook-endpoint-url) API.
699+
700+
701+
#### `getWebhookEndpointInfo(): Promise<Types.WebhookEndpointInfoResponse>`
702+
703+
It corresponds to the [Get webhook endpoint information](https://developers.line.biz/en/reference/messaging-api/#get-webhook-endpoint-information) API.
704+
705+
#### `testWebhookEndpoint(endpoint?: string): Promise<Types.TestWebhookEndpointResponse>`
706+
707+
It corresponds to the [Test webhook endpoint](https://developers.line.biz/en/reference/messaging-api/#test-webhook-endpoint) API.

lib/client.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,28 @@ export default class Client {
637637
);
638638
return ensureJSON(res);
639639
}
640+
641+
public async setWebhookEndpointUrl(endpoint: string) {
642+
return this.http.put<{}>(
643+
`${MESSAGING_API_PREFIX}/channel/webhook/endpoint`,
644+
{ endpoint },
645+
);
646+
}
647+
648+
public async getWebhookEndpointInfo() {
649+
const res = await this.http.get<Types.WebhookEndpointInfoResponse>(
650+
`${MESSAGING_API_PREFIX}/channel/webhook/endpoint`,
651+
);
652+
return ensureJSON(res);
653+
}
654+
655+
public async testWebhookEndpoint(endpoint?: string) {
656+
const res = await this.http.post<Types.TestWebhookEndpointResponse>(
657+
`${MESSAGING_API_PREFIX}/channel/webhook/test`,
658+
{ endpoint },
659+
);
660+
return ensureJSON(res);
661+
}
640662
}
641663

642664
export class OAuth {

lib/types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,3 +2602,26 @@ export type BotInfoResponse = {
26022602
chatMode: "chat" | "bot";
26032603
markAsReadMode: "auto" | "manual";
26042604
};
2605+
2606+
/**
2607+
* Response body of get webhook endpoint info.
2608+
*
2609+
* @see [Get get webhook endpoint info](https://developers.line.biz/en/reference/messaging-api/#get-webhook-endpoint-information)
2610+
*/
2611+
export type WebhookEndpointInfoResponse = {
2612+
endpoint: string;
2613+
active: boolean;
2614+
};
2615+
2616+
/**
2617+
* Response body of test webhook endpoint.
2618+
*
2619+
* @see [Test webhook endpoint](https://developers.line.biz/en/reference/messaging-api/#test-webhook-endpoint)
2620+
*/
2621+
export type TestWebhookEndpointResponse = {
2622+
success: boolean;
2623+
timestamp: string;
2624+
statusCode: number;
2625+
reason: string;
2626+
detail: string;
2627+
};

test/client.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,33 @@ describe("client", () => {
904904
equal(scope.isDone(), true);
905905
});
906906

907+
it("setWebhookEndpointUrl", async () => {
908+
const endpoint = "https://developers.line.biz/";
909+
const scope = mockPut(MESSAGING_API_PREFIX, `/channel/webhook/endpoint`, {
910+
endpoint,
911+
});
912+
913+
await client.setWebhookEndpointUrl(endpoint);
914+
equal(scope.isDone(), true);
915+
});
916+
917+
it("getWebhookEndpointInfo", async () => {
918+
const scope = mockGet(MESSAGING_API_PREFIX, `/channel/webhook/endpoint`);
919+
920+
await client.getWebhookEndpointInfo();
921+
equal(scope.isDone(), true);
922+
});
923+
924+
it("testWebhookEndpoint", async () => {
925+
const endpoint = "https://developers.line.biz/";
926+
const scope = mockPost(MESSAGING_API_PREFIX, `/channel/webhook/test`, {
927+
endpoint,
928+
});
929+
930+
await client.testWebhookEndpoint(endpoint);
931+
equal(scope.isDone(), true);
932+
});
933+
907934
it("set option once and clear option", async () => {
908935
const expectedBody = {
909936
messages: [testMsg],

0 commit comments

Comments
 (0)