Skip to content

Instantly share code, notes, and snippets.

@PamornT
Last active October 20, 2019 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PamornT/33e7cc7739a3ae79565b068d89f3f52a to your computer and use it in GitHub Desktop.
Save PamornT/33e7cc7739a3ae79565b068d89f3f52a to your computer and use it in GitHub Desktop.
const region = 'asia-northeast1';
const runtimeOpts = {
timeoutSeconds: 10,
memory: "2GB"
};
const functions = require('firebase-functions');
const request = require("request-promise");
const querystring = require('querystring');
const admin = require('firebase-admin');
let LINE_HEADER;
const LINE_OAUTH_API = "https://api.line.me/v2/oauth";
const LINE_MESSAGING_API = "https://api.line.me/v2/bot/message";
const CHANNEL_ID = "YOUR-CHANNEL-ID";
const CHANNEL_SECRET = "YOUR-CHANNEL-SECRET";
admin.initializeApp();
exports.AccessTokenBot = functions.region(region).runWith(runtimeOpts).https.onRequest(async (req, res) => {
let event = req.body.events[0]
switch (event.type) {
case 'message':
if (event.message.type === 'text') {
if(event.message.text === 'register') {
let access_token = await issue_access_token();
access_token = JSON.parse(access_token);
await initialLINE(access_token['token_type'], access_token['access_token']);
await admin.database().ref('token').child(event.source.userId).set(access_token);
reply(event.replyToken, {
type: 'text',
text: '\uDBC0\uDC7F ลงทะเบียน token เรียบร้อยแล้ว เอาไปใช้ได้เลย\n\n' + JSON.stringify(access_token)
});
} else if(event.message.text === 'revoke') {
let token = await admin.database().ref('token').child(event.source.userId).once('value');
await initialLINE(token.val().token_type, token.val().access_token );
await admin.database().ref('token').child(event.source.userId).remove();
await reply(event.replyToken, {
type: 'text',
text: '\uDBC0\uDC9C ลบ token เรียบร้อยแล้ว จะใช้มาขอใหม่น้าา'
});
revoke_access_token(token.val().access_token);
}
}
break;
}
res.status(200).send("ok").end();
});
const initialLINE = async(token_type, token) => {
LINE_HEADER = {
"Content-Type": "application/json",
"Authorization": `${token_type} ${token}`
};
}
const issue_access_token = async() => {
let params = {
grant_type: 'client_credentials',
client_id: CHANNEL_ID,
client_secret: CHANNEL_SECRET
};
let formData = querystring.stringify(params);
let contentLength = formData.length;
let promise = new Promise(resolve => {
request.post({
url: `${LINE_OAUTH_API}/accessToken`,
headers: {
'Content-Length': contentLength,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData
}, (error, response, body) => {
if(!error)
resolve(body);
else
resolve(error);
})
});
let result = await promise;
return result;
};
const revoke_access_token = (access_token) => {
let params = {
access_token: access_token
};
let formData = querystring.stringify(params);
return request.post({
uri: `${LINE_OAUTH_API}/revoke`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: formData
})
};
const reply = (token, payload) => {
return request.post({
uri: `${LINE_MESSAGING_API}/reply`,
headers: LINE_HEADER,
body: JSON.stringify({
replyToken: token,
messages: [payload]
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment