Skip to content

Commit db2fa70

Browse files
committed
Merge branch 'next'
2 parents 0693e2e + 88eaeaa commit db2fa70

File tree

12 files changed

+21079
-4083
lines changed

12 files changed

+21079
-4083
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 7.3.0 (26 Apr 2021)
2+
3+
### Feature
4+
* Support Flex Message Update 2 (#271)
5+
* Messaging API - January 2021 update (#277)
6+
7+
### Misc
8+
* Add TypeScript Example (#270)
9+
* Update dependencies (#270)(#272)(#283)
10+
111
## 7.2.0 (18 Sep 2020)
212

313
### Feature

examples/echo-bot-ts/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Built files.
5+
dist/

examples/echo-bot-ts/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# LINE Echo Bot with TypeScript
2+
3+
An example LINE bot to echo message with TypeScript. The bot is coded according to TypeScript's best practices.
4+
5+
## Prerequisite
6+
7+
- Git
8+
- Node.js version 10 and up
9+
- Heroku CLI (optional)
10+
- LINE Developers Account for the bot
11+
12+
## Installation
13+
14+
- Clone the repository.
15+
16+
```bash
17+
git clone https://github.com/line/line-bot-sdk-nodejs.git
18+
```
19+
20+
- Change directory to the example.
21+
22+
```bash
23+
cd line-bot-sdk-nodejs/examples/echo-bot-ts
24+
```
25+
26+
- Install all dependencies.
27+
28+
```bash
29+
npm install
30+
```
31+
32+
- Configure all of the environment variables.
33+
34+
```bash
35+
export CHANNEL_ACCESS_TOKEN=<YOUR_CHANNEL_ACCESS_TOKEN>
36+
export CHANNEL_SECRET=<YOUR_CHANNEL_SECRET>
37+
export PORT=<YOUR_PORT>
38+
```
39+
40+
- Setup your webhook URL in your LINE Official Account to be in the following format. Don't forget to disable the greeting messages and auto-response messages for convenience.
41+
42+
```bash
43+
https://example-url.com/webhook
44+
```
45+
46+
- Compile the TypeScript files.
47+
48+
```bash
49+
npm run build
50+
```
51+
52+
- Run the application.
53+
54+
```bash
55+
npm start
56+
```
57+
58+
## Alternative Installation
59+
60+
If you want to deploy it via Heroku, it is also possible and is even easier for testing purposes.
61+
62+
- Clone the repository.
63+
64+
```bash
65+
git clone https://github.com/line/line-bot-sdk-nodejs.git
66+
```
67+
68+
- Change directory to the example.
69+
70+
```bash
71+
cd line-bot-sdk-nodejs/examples/echo-bot-ts
72+
```
73+
74+
- Create a Heroku application.
75+
76+
```bash
77+
git init
78+
heroku create <YOUR_APP_NAME> # Do not specify for a random name
79+
```
80+
81+
- Setup the environment variables, and don't forget to setup your webhook URL (from the Heroku application that you have just created) in your LINE Offical Account. The webhook URL will still accept the following format: `https://example-url.com.herokuapp.com/webhook`.
82+
83+
```bash
84+
heroku config:set CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
85+
heroku config:set CHANNEL_SECRET=YOUR_CHANNEL_SECRET
86+
```
87+
88+
- Push the application to the server.
89+
90+
```bash
91+
git add .
92+
git commit -m "Initial commit for Heroku testing"
93+
git push heroku master
94+
```
95+
96+
- Open your application.
97+
98+
```bash
99+
heroku open
100+
```
101+
102+
- Done!

examples/echo-bot-ts/environment.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
declare global {
2+
namespace NodeJS {
3+
interface ProcessEnv {
4+
CHANNEL_ACCESS_TOKEN: string;
5+
CHANNEL_SECRET: string;
6+
PORT: string;
7+
}
8+
}
9+
}
10+
11+
export {};

examples/echo-bot-ts/index.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Import all dependencies, mostly using destructuring for better view.
2+
import { ClientConfig, Client, middleware, MiddlewareConfig, WebhookEvent, TextMessage, MessageAPIResponseBase } from '@line/bot-sdk';
3+
import express, { Application, Request, Response } from 'express';
4+
5+
// Setup all LINE client and Express configurations.
6+
const clientConfig: ClientConfig = {
7+
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN || '',
8+
channelSecret: process.env.CHANNEL_SECRET,
9+
};
10+
11+
const middlewareConfig: MiddlewareConfig = {
12+
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
13+
channelSecret: process.env.CHANNEL_SECRET || '',
14+
};
15+
16+
const PORT = process.env.PORT || 3000;
17+
18+
// Create a new LINE SDK client.
19+
const client = new Client(clientConfig);
20+
21+
// Create a new Express application.
22+
const app: Application = express();
23+
24+
// Function handler to receive the text.
25+
const textEventHandler = async (event: WebhookEvent): Promise<MessageAPIResponseBase | undefined> => {
26+
// Process all variables here.
27+
if (event.type !== 'message' || event.message.type !== 'text') {
28+
return;
29+
}
30+
31+
// Process all message related variables here.
32+
const { replyToken } = event;
33+
const { text } = event.message;
34+
35+
// Create a new message.
36+
const response: TextMessage = {
37+
type: 'text',
38+
text,
39+
};
40+
41+
// Reply to the user.
42+
await client.replyMessage(replyToken, response);
43+
};
44+
45+
// Register the LINE middleware.
46+
// As an alternative, you could also pass the middleware in the route handler, which is what is used here.
47+
// app.use(middleware(middlewareConfig));
48+
49+
// Route handler to receive webhook events.
50+
// This route is used to receive connection tests.
51+
app.get(
52+
'/',
53+
async (_: Request, res: Response): Promise<Response> => {
54+
return res.status(200).json({
55+
status: 'success',
56+
message: 'Connected successfully!',
57+
});
58+
}
59+
);
60+
61+
// This route is used for the Webhook.
62+
app.post(
63+
'/webhook',
64+
middleware(middlewareConfig),
65+
async (req: Request, res: Response): Promise<Response> => {
66+
const events: WebhookEvent[] = req.body.events;
67+
68+
// Process all of the received events asynchronously.
69+
const results = await Promise.all(
70+
events.map(async (event: WebhookEvent) => {
71+
try {
72+
await textEventHandler(event);
73+
} catch (err: unknown) {
74+
if (err instanceof Error) {
75+
console.error(err);
76+
}
77+
78+
// Return an error message.
79+
return res.status(500).json({
80+
status: 'error',
81+
});
82+
}
83+
})
84+
);
85+
86+
// Return a successfull message.
87+
return res.status(200).json({
88+
status: 'success',
89+
results,
90+
});
91+
}
92+
);
93+
94+
// Create a server and listen to it.
95+
app.listen(PORT, () => {
96+
console.log(`Application is live and listening on port ${PORT}`);
97+
});

0 commit comments

Comments
 (0)