Last active
October 5, 2019 01:55
-
-
Save PamornT/f5546f43d67d7abaf52182a1120fa8cc to your computer and use it in GitHub Desktop.
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
const REGION = 'asia-northeast1'; | |
const functions = require('firebase-functions'); | |
const request = require('request-promise'); | |
const LINE_MESSAGING_API = 'https://api.line.me/v2/bot/message'; | |
const LINE_HEADER = { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer xxxxx` | |
}; | |
const THAIPOST_TOKEN = 'yyyyy'; | |
exports.zzzzzLineBot = functions.region(REGION).https.onRequest((req, res) => { | |
if (req.body.events[0].message.type !== 'text') { | |
return; | |
} | |
let message = req.body.events[0].message.text; | |
gettrack(req, message); | |
async function gettrack(req, message) | |
{ | |
let promise_token = new Promise(resolve => { | |
var options = { | |
method: 'POST', | |
uri: 'https://trackapi.thailandpost.co.th/post/api/v1/authenticate/token', | |
strictSSL: false, | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Token ' + THAIPOST_TOKEN | |
} | |
}; | |
request(options, function(error, response, body) { | |
resolve(JSON.parse(body)); | |
}); | |
}); | |
let access_token = await promise_token; | |
let params = { | |
"status": "all", | |
"language": "TH", | |
"barcode": [ | |
message | |
] | |
}; | |
let promise_track = new Promise(resolve => { | |
var options = { | |
method: 'POST', | |
uri: 'https://trackapi.thailandpost.co.th/post/api/v1/track', | |
strictSSL: false, | |
body: JSON.stringify(params), | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Token ' + access_token.token | |
} | |
}; | |
request(options, function(error, response, body) { | |
resolve( JSON.parse(body) ); | |
}); | |
}); | |
let tracks = await promise_track; | |
console.log('Track Result', JSON.stringify(tracks) ); | |
reply_text(req, JSON.stringify(tracks) ); | |
} | |
}); | |
const reply_text = (req, message) => { | |
return request({ | |
method: `POST`, | |
uri: `${LINE_MESSAGING_API}/reply`, | |
headers: LINE_HEADER, | |
body: JSON.stringify({ | |
replyToken: req.body.events[0].replyToken, | |
messages: [ | |
{ | |
type: `text`, | |
text: message | |
} | |
] | |
}) | |
}); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment