Last active
March 21, 2022 09:48
-
-
Save wwarodom/1b38400f1fda585807c5c9e9f94dc086 to your computer and use it in GitHub Desktop.
Line Notification Backend (NextJS) pages/api/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | |
// ./pages/api/index.js | |
// dependency: npm i -s line-notify-nodejs | |
// You can get Token URL here: | |
// https://notify-bot.line.me/my/ | |
// Generate token and copy it | |
const lineNotify = require('line-notify-nodejs')('ap...REPLACE WITH YOUR TOKEN....JY'); | |
export default async function handler(req, res) { | |
const message = "Send test 12"; | |
try { | |
await lineNotify.notify({ message }); | |
} | |
catch (e) { | |
console.error(e); | |
res.status(200).json({ status: 'failed' }) | |
} | |
res.status(200).json({ status: message }) | |
} | |
// ================================================================== | |
// Front end code | |
// ================================================================== | |
const [line, setLine] = useState(''); | |
const fetchData = async () => { | |
const result = await fetch("http://localhost:3000/api/"); | |
const returnStatus = (await result.json()).status; | |
console.log("Result: ", returnStatus); | |
setLine(returnStatus); | |
} | |
<button onClick={fetchData}>Line Noti</button> | |
// ================================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment