Skip to content

Instantly share code, notes, and snippets.

@wwarodom
Last active March 21, 2022 09:48
Show Gist options
  • Save wwarodom/1b38400f1fda585807c5c9e9f94dc086 to your computer and use it in GitHub Desktop.
Save wwarodom/1b38400f1fda585807c5c9e9f94dc086 to your computer and use it in GitHub Desktop.
Line Notification Backend (NextJS) pages/api/index.js
// 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