若要使用library請使用Branch:stable/linebot-library
[
{
"type": "message",
"replyToken": "2dd9d92d08204ab6b5628d95d49abebc",
"source": {
"userId": "Ue1ed1792dfbdbd4ed43f91ad295apple",
"type": "user"
},
"timestamp": 1577346740883,
"mode": "active",
"message": {
"type": "text",
"id": "11148713329452",
"text": "apple"
}
}
]
所需套件
crypto
request
request-promise
fs
需將lib資料夾及.env檔案複製至專案目錄之下,並更改.env(channelSecret、lineBotToken)
將package.json dependencies 所需套件及版本複製至專案內部的package.json
koa2 framework 建立Server 並加入LINE-Bot Middleware
const koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
//使用.env檔的參數
const dotenv = require('dotenv').load();
const linebot = require('./lib/linebot.js');
const app = new koa();
const router = Router();
const channelSecret = process.env.channelSecret;
const lineBotToken = process.env.lineBotToken;
app.use(bodyParser());
router.post('/webhooks', async (ctx, next) => {
});
app
.use(linebot.middleware(channelSecret))
.use(router.routes());
const server = app.listen(process.env.PORT || 8080, function () {
const port = server.address().port;
console.log("App now running on port", port);
});
const responseText = require('./lib/example/responseText.js');
router.post('/webhooks', async (ctx, next) => {
let events = ctx.request.body.events;
data = await responseText.responseText(events, lineBotToken, {
'哈囉': '你好阿',
'晚安': '晚安'
});
ctx.body = data;
});
const sticker = require('./lib/example/sticker.js');
router.post('/webhooks', async (ctx, next) => {
let events = ctx.request.body.events;
data = await sticker.sticker(events, lineBotToken, 11537, 52002734);
ctx.body = data;
});
property | Description |
---|---|
originalContentUrl | https images url Max: 4096 x 4096 Max: 1 MB |
previewImageUrl | https images url Max: 240 x 240 Max: 1 MB |
const image = require('./lib/example/image.js');
router.post('/webhooks', async (ctx, next) => {
let events = ctx.request.body.events;
data = await image.image(events, lineBotToken, "https://i.imgur.com/slkvDvQ.jpg", "https://i.imgur.com/8206ST0.png");
ctx.body = data;
});
const confirm = require('./lib/example/confirm.js');
router.post('/webhooks',async (ctx, next) => {
let events = ctx.request.body.events;
data = await confirm.confirm(events,lineBotToken);
ctx.body = data;
});
const richMenu = require('./lib/example/richMenu.js');
router.get('/create', async (ctx) => {
//create rich menu
let createDefaultRichMenu = await richMenu.createRichMenu(lineBotToken);
let richMenuId = createDefaultRichMenu.richMenuId;
console.log(richMenuId);
// upload rich menu image
let uploadRichMenuImageData = await richMenu.uploadRichMenuImage(richMenuId, `${__dirname}/public/img/test.png`, lineBotToken);
console.log(uploadRichMenuImageData);
// Set default rich menu
let setDefaultRichMenuData = await richMenu.setDefaultRichMenu(richMenuId, lineBotToken);
ctx.body = setDefaultRichMenuData;
});
const url = require('./lib/example/url.js');
router.post('/webhooks', async (ctx, next) => {
let events = ctx.request.body.events;
data = await url.url(events, lineBotToken);
ctx.body = data;
});