Liên kết group chat line (ok)
C:\Users\Administrator\sample-app\package.json
C:\Users\Administrator\sample-app\package.json
{
"name": "sample-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1"
}
}
C:\Users\Administrator\sample-app\index.js
const https = require("https")
const express = require("express")
const app = express()
const PORT = process.env.PORT || 3000
const TOKEN = process.env.LINE_ACCESS_TOKEN
app.use(express.json())
app.use(express.urlencoded({
extended: true
}))
app.get("/", (req, res) => {
res.sendStatus(200)
})
app.post("/webhook", function(req, res) {
res.send("HTTP POST request sent to the webhook URL!")
// If the user sends a message to your bot, send a reply message
if (req.body.events[0].type === "message") {
// Message data, must be stringified
const dataString = JSON.stringify({
replyToken: req.body.events[0].replyToken,
messages: [
{
"type": "text",
"text": "Hello, user"
},
{
"type": "text",
"text": "May I help you?"
}
]
})
// Request header
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + TOKEN
}
// Options to pass into the request
const webhookOptions = {
"hostname": "api.line.me",
"path": "/v2/bot/message/reply",
"method": "POST",
"headers": headers,
"body": dataString
}
// Define request
const request = https.request(webhookOptions, (res) => {
res.on("data", (d) => {
process.stdout.write(d)
})
})
// Handle error
request.on("error", (err) => {
console.error(err)
})
// Send data
request.write(dataString)
request.end()
}
})
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
Một ví dụ push_message
C:\Users\Administrator\sample-app\index.js
'use strict';
const line = require('@line/bot-sdk');
const express = require('express');
// create LINE SDK config from env variables
const TOKEN = process.env.LINE_ACCESS_TOKEN
const config = {
channelAccessToken: process.env.LINE_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
};
const { LineClient } = require('messaging-api-line');
// get accessToken and channelSecret from LINE developers website
const client = new LineClient({
accessToken: process.env.LINE_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
});
client.push('U6d9bb0e12d1963fad8871b2c4f12f8f0', [
{
type: 'text',
text: 'Hello!',
},
]);
const port = process.env.PORT || 3000;
C:\Users\Administrator\sample-app\.env
LINE_ACCESS_TOKEN='GS94jxpgJ6OxGbWp8N1leWVyoFpkBi8UsTWjewIB3pj0uGdaowpqH2bGC3Nqdwr6FcysrN+t24ay6+6YQu0xQ00E6PksuOx1h1je0hbwWLq8fm+v+bl8hL6XbvCwAP/JMQCDJZOKOkNpItKSghZ13QdB04t89/1O/w1cDnyilFU='
LINE_CHANNEL_SECRET='3341b29a345074a72c09401c0be23075'
PORT=3000
Một ví dụ tạo form
C:\Users\Administrator\sample-app\form.html
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h2>Please fill in the information</h2>
<form action="/webhook" method="POST">
<div class="form-group mb-3">
<label>User</label>
<input type="text" class="form-control" placeholder="User" name="user">
</div>
<div class="form-group mb-3">
<label>Phone</label>
<input type="text" class="form-control" placeholder="Phone" name="phone">
</div>
<div class="d-grid mt-3">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</form>
</body>
</html>
C:\Users\Administrator\node-pandora\index.js
const express = require('express')
var parseUrl = require('body-parser')
const app = express()
let encodeUrl = parseUrl.urlencoded({ extended: false })
app.get('/', (req, res) => {
res.sendFile(__dirname + '/form.html')
})
app.post('/', encodeUrl, (req, res) => {
console.log('Form request:', req.body)
res.sendStatus(200)
})
app.listen(4000)
Đã hoàn thành cả hai push & reply
C:\Users\Administrator\sample-app\index.js
const https = require("https")
const express = require("express")
const line = require('@line/bot-sdk');
const app = express()
const PORT = process.env.PORT || 3000
const TOKEN = process.env.LINE_ACCESS_TOKEN
const config = {
channelAccessToken: process.env.LINE_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
};
const { LineClient } = require('messaging-api-line');
const client = new LineClient({
accessToken: process.env.LINE_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET,
});
app.use(express.json())
app.use(express.urlencoded({
extended: true
}))
app.get("/webhook", (req, res) => {
res.sendFile(__dirname + '/form.html')
})
app.post("/webhook", function(req, res) {
client.push('U6d9bb0e12d1963fad8871b2c4f12f8f0', [
{
type: 'text',
text: 'Hello!',
},
]);
// If the user sends a message to your bot, send a reply message
console.log(req);
console.log(res);
if (req && req.body && req.body.events && req.body.events[0].type === "message") {
// Message data, must be stringified
const dataString = JSON.stringify({
replyToken: req.body.events[0].replyToken,
messages: [
{
"type": "text",
"text": "Hello, user"
},
{
"type": "text",
"text": "May I help you?"
}
]
})
// Request header
const headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + TOKEN
}
// Options to pass into the request
const webhookOptions = {
"hostname": "api.line.me",
"path": "/v2/bot/message/reply",
"method": "POST",
"headers": headers,
"body": dataString
}
// Define request
const request = https.request(webhookOptions, (res) => {
res.on("data", (d) => {
process.stdout.write(d)
})
})
// Handle error
request.on("error", (err) => {
console.error(err)
})
// Send data
request.write(dataString)
request.end()
}
})
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
Last updated