Last active
June 9, 2020 01:57
-
-
Save etrex/a8c958b506342d462c7ef4dda314ac8b to your computer and use it in GitHub Desktop.
lineNotify.js
This file contains hidden or 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
const querystring = require('querystring'); | |
const axios = require('axios'); | |
function getAuthLink(clientId, redirectUrl, state) { | |
const data = { | |
response_type: 'code', | |
client_id: clientId, | |
redirect_uri: redirectUrl, | |
scope: 'notify', | |
state, | |
}; | |
return `https://notify-bot.line.me/oauth/authorize?${querystring.encode( | |
data | |
)}`; | |
} | |
async function getToken(code, redirectUri, clientId, clientSecret) { | |
const url = 'https://notify-bot.line.me/oauth/token'; | |
const headers = { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}; | |
const formData = { | |
grant_type: 'authorization_code', | |
code, | |
redirect_uri: redirectUri, | |
client_id: clientId, | |
client_secret: clientSecret, | |
}; | |
return await axios.post(url, querystring.encode(formData), { headers }); | |
} | |
async function sendNotify(token, message) { | |
const url = 'https://notify-api.line.me/api/notify'; | |
const headers = { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
Authorization: `Bearer ${token}`, | |
}; | |
const formData = { | |
message, | |
}; | |
return await axios.post(url, querystring.encode(formData), { headers }); | |
} | |
module.exports = { | |
getAuthLink, | |
getToken, | |
sendNotify, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment