0.1.8 • Published 6 months ago
hono-linebot-middleware v0.1.8
hono-linebot-middleware
LINE Bot SDK middleware for Hono - Simple webhook handler for LINE Bot.
Installation
npm i @hono/node-server @line/bot-sdk
npm install hono-linebot-middleware
Usage
import { Hono } from 'hono'
import * as line from '@line/bot-sdk'
import { lineBotMiddleware } from 'hono-linebot-middleware'
const app = new Hono()
// LINE SDK client
const client = new line.messagingApi.MessagingApiClient({
channelAccessToken: 'YOUR_CHANNEL_ACCESS_TOKEN'
})
// Message handler
async function handleEvent(event: line.WebhookEvent) {
if (event.type !== 'message' || event.message.type !== 'text') return
await client.replyMessage({
replyToken: event.replyToken,
messages: [{
type: 'text',
text: `Echo: ${event.message.text}`
}]
})
}
// Webhook endpoint
app.post('/callback', lineBotMiddleware({
channelSecret: 'YOUR_CHANNEL_SECRET'
}), async (c) => {
try {
const { events } = c.get('body')
await Promise.all(events.map(handleEvent))
return c.json({ status: 'success' })
} catch (err) {
console.error(err)
return c.json({ status: 'error' }, 500)
}
})
Requirements
Node.js v18 or later Hono v4.0.0 or later @line/bot-sdk v9.5.0 or later
Configuration
The middleware requires the following LINE Bot credentials:
channelSecret
: Your LINE Bot channel secretchannelAccessToken
: Your LINE Bot channel access token
You can get these credentials from the LINE Developers Console.
License
MIT