Skip to content

Commit c0c2044

Browse files
author
Hyunje Jun
authored
Refactor kitchensink (#99)
* [kitchensink] Use local version of SDK * [kitchensink] Add ngrok to deps * [kitchensink] Add GET /callback to show helper text * [kitchensink] Handle webhook test requests The test requests are with a specific format of replyToken, such as '000000...'. * [kitchensink] Add build-sdk npm script SDK should be prepared before being installed from the example. * [kitchensink] Use ngrok when BASE_URL is not set * [kitchensink] Update README
1 parent 6e8f755 commit c0c2044

File tree

4 files changed

+14009
-105
lines changed

4 files changed

+14009
-105
lines changed

examples/kitchensink/README.md

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,75 @@
22

33
A kitchen-sink LINE bot example
44

5-
## How to use
5+
## Requirements
66

7-
### Install deps
7+
Install npm dependencies:
88

9-
``` shell
9+
```bash
10+
npm run build-sdk # build SDK installed from local directory
1011
npm install
1112
```
1213

1314
Also, FFmpeg and ImageMagick should be installed to test image and video
1415
echoing.
1516

16-
### Configuration
17+
### About local dependencies
1718

18-
``` shell
19+
Currently, [`@line/bot-sdk`](package.json) is installed from local directory.
20+
21+
```json
22+
{
23+
"@line/bot-sdk": "../../"
24+
}
25+
```
26+
27+
To install `@line/bot-sdk` from npm, please update the line with the following:
28+
29+
```json
30+
{
31+
"@line/bot-sdk": "*"
32+
}
33+
```
34+
35+
In the case, `npm run build-sdk` needn't be run before `npm install`.
36+
37+
## Configuration
38+
39+
Configuration can be done via environment variables.
40+
41+
```bash
1942
export CHANNEL_SECRET=YOUR_CHANNEL_SECRET
2043
export CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
2144
export BASE_URL=https://your.base.url # for static file serving
2245
export PORT=1234
2346
```
2447

25-
## Run
48+
The code above is an example of Bash. It may differ in other shells.
49+
50+
## Run webhook server
2651

27-
``` shell
28-
node .
52+
```bash
53+
npm start
2954
```
3055

31-
## Webhook URL
56+
With the configuration above, the webhook listens on `https://your.base.url:1234/callback`.
57+
58+
## ngrok usage
59+
60+
[ngrok](https://ngrok.com/) tunnels extenral requests to localhost, helps
61+
debugging local webhooks.
62+
63+
This example includes ngrok inside, and it just works if no `BASE_URL` is
64+
set. Make sure that other configurations are set correctly.
3265

3366
```
34-
https://your.base.url/callback
67+
❯ npm start
68+
69+
...
70+
71+
It seems that BASE_URL is not set. Connecting to ngrok...
72+
listening on https://ffffffff.ngrok.io/callback
3573
```
74+
75+
The URL can be directly registered as the webhook URL in LINE Developers
76+
console.

examples/kitchensink/index.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const express = require('express');
55
const fs = require('fs');
66
const path = require('path');
77
const cp = require('child_process');
8+
const ngrok = require('ngrok');
89

910
// create LINE SDK config from env variables
1011
const config = {
@@ -13,7 +14,7 @@ const config = {
1314
};
1415

1516
// base URL for webhook server
16-
const baseURL = process.env.BASE_URL;
17+
let baseURL = process.env.BASE_URL;
1718

1819
// create LINE SDK client
1920
const client = new line.Client(config);
@@ -26,6 +27,8 @@ const app = express();
2627
app.use('/static', express.static('static'));
2728
app.use('/downloaded', express.static('downloaded'));
2829

30+
app.get('/callback', (req, res) => res.end(`I'm listening. Please access with POST.`));
31+
2932
// webhook callback
3033
app.post('/callback', line.middleware(config), (req, res) => {
3134
// req.body.events should be an array of events
@@ -53,6 +56,10 @@ const replyText = (token, texts) => {
5356

5457
// callback function to handle a single event
5558
function handleEvent(event) {
59+
if (event.replyToken.match(/^(.)\1*$/)) {
60+
return console.log("Test hook recieved: " + JSON.stringify(event.message));
61+
}
62+
5663
switch (event.type) {
5764
case 'message':
5865
const message = event.message;
@@ -370,5 +377,15 @@ function handleSticker(message, replyToken) {
370377
// listen on port
371378
const port = process.env.PORT || 3000;
372379
app.listen(port, () => {
373-
console.log(`listening on ${port}`);
380+
if (baseURL) {
381+
console.log(`listening on ${baseURL}:${port}/callback`);
382+
} else {
383+
console.log("It seems that BASE_URL is not set. Connecting to ngrok...")
384+
ngrok.connect(port, (err, url) => {
385+
if (err) throw err;
386+
387+
baseURL = url;
388+
console.log(`listening on ${baseURL}/callback`);
389+
});
390+
}
374391
});

0 commit comments

Comments
 (0)