11# Basic Usage
22
33It can be imported with [ CommonJS] ( https://nodejs.org/docs/latest/api/modules.html ) ,
4- [ ES2015 modules] ( https://babeljs.io/learn-es2015/#ecmascript-2015-features -modules ) ,
4+ [ ECMAScript modules(ES modules) ] ( https://tc39.es/ecma262/#sec -modules ) ,
55and preferably [ TypeScript] ( https://www.typescriptlang.org/ ) .
66
77The library is written in TypeScript and includes TypeScript definitions by
88default. Nevertheless, it can surely be used with plain JavaScript too.
99
1010``` js
11+ // ES Modules or TypeScript
12+ import * as line from ' @line/bot-sdk' ;
13+
1114// CommonJS
1215const line = require (' @line/bot-sdk' );
13-
14- // ES2015 modules or TypeScript
15- import * as line from ' @line/bot-sdk' ;
1616```
1717
1818## Configuration
@@ -26,7 +26,6 @@ new line.messagingApi.MessagingApiClient({
2626 channelAccessToken: ' YOUR_CHANNEL_ACCESS_TOKEN' ,
2727});
2828line .middleware ({
29- channelAccessToken: ' YOUR_CHANNEL_ACCESS_TOKEN' ,
3029 channelSecret: ' YOUR_CHANNEL_SECRET'
3130});
3231```
@@ -36,39 +35,57 @@ line.middleware({
3635Here is a synopsis of echoing webhook server with [ Express] ( https://expressjs.com/ ) :
3736
3837``` js
39- const express = require ( ' express ' );
40- const line = require ( ' @line/bot-sdk ' );
38+ import * as line from ' @line/bot-sdk '
39+ import express from ' express '
4140
41+ // create LINE SDK config from env variables
4242const config = {
43- channelAccessToken: ' YOUR_CHANNEL_ACCESS_TOKEN' ,
44- channelSecret: ' YOUR_CHANNEL_SECRET'
43+ channelSecret: process .env .CHANNEL_SECRET ,
4544};
4645
46+ // create LINE SDK client
47+ const client = new line.messagingApi.MessagingApiClient ({
48+ channelAccessToken: process .env .CHANNEL_ACCESS_TOKEN
49+ });
50+
51+ // create Express app
52+ // about Express itself: https://expressjs.com/
4753const app = express ();
48- app .post (' /webhook' , line .middleware (config), (req , res ) => {
54+
55+ // register a webhook handler with middleware
56+ // about the middleware, please refer to doc
57+ app .post (' /callback' , line .middleware (config), (req , res ) => {
4958 Promise
5059 .all (req .body .events .map (handleEvent))
51- .then ((result ) => res .json (result));
60+ .then ((result ) => res .json (result))
61+ .catch ((err ) => {
62+ console .error (err);
63+ res .status (500 ).end ();
64+ });
5265});
5366
54- const client = new line.messagingApi.MessagingApiClient ({
55- channelAccessToken: ' YOUR_CHANNEL_ACCESS_TOKEN' ,
56- });
67+ // event handler
5768function handleEvent (event ) {
5869 if (event .type !== ' message' || event .message .type !== ' text' ) {
70+ // ignore non-text-message event
5971 return Promise .resolve (null );
6072 }
6173
74+ // create an echoing text message
75+ const echo = { type: ' text' , text: event .message .text };
76+
77+ // use reply API
6278 return client .replyMessage ({
6379 replyToken: event .replyToken ,
64- messages: [{
65- type: ' text' ,
66- text: event .message .text
67- }],
80+ messages: [echo],
6881 });
6982}
7083
71- app .listen (3000 );
84+ // listen on port
85+ const port = process .env .PORT || 3000 ;
86+ app .listen (port, () => {
87+ console .log (` listening on ${ port} ` );
88+ });
7289```
7390
7491The full examples with comments can be found
0 commit comments