Skip to content

Commit d0acc4f

Browse files
authored
add rich menu sample code (#314)
1 parent ccac1b0 commit d0acc4f

File tree

7 files changed

+880
-1
lines changed

7 files changed

+880
-1
lines changed

examples/rich-menu/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# rich menu
2+
3+
[Using rich menus](https://developers.line.biz/en/docs/messaging-api/using-rich-menus/)
4+
5+
## How to use
6+
7+
### Install deps
8+
9+
``` shell
10+
$ npm install
11+
```
12+
13+
### Configuration
14+
15+
``` shell
16+
$ export CHANNEL_SECRET=YOUR_CHANNEL_SECRET
17+
$ export CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
18+
```
19+
20+
### Run
21+
22+
``` shell
23+
$ node .
24+
```

examples/rich-menu/index.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'use strict';
2+
3+
const line = require('@line/bot-sdk');
4+
const { join } = require("path");
5+
const { readFileSync } = require("fs");
6+
7+
// create LINE SDK config from env variables
8+
const config = {
9+
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
10+
channelSecret: process.env.CHANNEL_SECRET,
11+
};
12+
13+
// create LINE SDK client
14+
const client = new line.Client(config);
15+
16+
const richMenuObjectA = () => ({
17+
size: {
18+
width: 2500,
19+
height: 1686
20+
},
21+
selected: false,
22+
name: "richmenu-a",
23+
chatBarText: "Tap to open",
24+
areas: [
25+
{
26+
bounds: {
27+
x: 0,
28+
y: 0,
29+
width: 1250,
30+
height: 1686
31+
},
32+
action: {
33+
type: "uri",
34+
uri: "https://www.line-community.me/"
35+
}
36+
},
37+
{
38+
bounds: {
39+
x: 1251,
40+
y: 0,
41+
width: 1250,
42+
height: 1686
43+
},
44+
action: {
45+
type: "richmenuswitch",
46+
richMenuAliasId: "richmenu-alias-b",
47+
data: "richmenu-changed-to-b"
48+
}
49+
}
50+
]
51+
})
52+
53+
const richMenuObjectB = () => ({
54+
size: {
55+
width: 2500,
56+
height: 1686
57+
},
58+
selected: false,
59+
name: "richmenu-b",
60+
chatBarText: "Tap to open",
61+
areas: [
62+
{
63+
bounds: {
64+
x: 0,
65+
y: 0,
66+
width: 1250,
67+
height: 1686
68+
},
69+
action: {
70+
type: "richmenuswitch",
71+
richMenuAliasId: "richmenu-alias-a",
72+
data: "richmenu-changed-to-a"
73+
}
74+
},
75+
{
76+
bounds: {
77+
x: 1251,
78+
y: 0,
79+
width: 1250,
80+
height: 1686
81+
},
82+
action: {
83+
type: "uri",
84+
uri: "https://www.line-community.me/"
85+
}
86+
}
87+
]
88+
})
89+
90+
const main = async (client) => {
91+
// 2. Create rich menu A (richmenu-a)
92+
const richMenuAId = await client.createRichMenu(
93+
richMenuObjectA()
94+
)
95+
96+
// 3. Upload image to rich menu A
97+
const filepathA = join(__dirname, './public/richmenu-a.png')
98+
const bufferA = readFileSync(filepathA)
99+
100+
await client.setRichMenuImage(richMenuAId, bufferA)
101+
102+
// 4. Create rich menu B (richmenu-b)
103+
const richMenuBId = await client.createRichMenu(richMenuObjectB())
104+
105+
// 5. Upload image to rich menu B
106+
const filepathB = join(__dirname, './public/richmenu-b.png')
107+
const bufferB = readFileSync(filepathB);
108+
109+
await client.setRichMenuImage(richMenuBId, bufferB);
110+
111+
// 6. Set rich menu A as the default rich menu
112+
await client.setDefaultRichMenu(richMenuAId)
113+
114+
// 7. Create rich menu alias A
115+
await client.createRichMenuAlias(richMenuAId, 'richmenu-alias-a')
116+
117+
// 8. Create rich menu alias B
118+
await client.createRichMenuAlias(richMenuBId, 'richmenu-alias-b')
119+
console.log('success')
120+
}
121+
122+
main(client)

0 commit comments

Comments
 (0)