Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// If this is test, we will not actually issue call out.
if (process.env.BOT_EXPRESS_ENV == "test"){
debug("This is test so we skip the actual call out.");
return Promise.resolve();
}
let headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this._channel_access_token
};
let body = {
to: to,
messages: messages
}
let url = 'https://api.line.me/v2/bot/message/multicast';
return request.postAsync({
url: url,
headers: headers,
body: body,
json: true
}).then(
(response) => {
if (response.statusCode != 200){
debug("line.send() failed");
if (response.body && response.body.message && response.body.details && response.body.details.length > 0){
let error_message = "Error code is " + response.statusCode + ". " + response.body.message + ".";
for (let detail of response.body.details){
error_message += " " + detail.message;
}
return Promise.reject(new Error(error_message));
} else if (response.body && response.body.message){
return Promise.reject(new Error(response.body.message));
required_params.map((param) => {
if (!options[param]){
throw new Error(`Required parameter ${param} is missing.`);
}
})
// Check if configured parameters are all valid.
Object.keys(options).map((param) => {
if (!required_params.includes(param) && !optional_params.includes(param)){
throw new Error(`${param} is not a valid parameter.`);
}
})
let url = `https://${this.apiHostname}/${api_version}/payments/preapprovedPay/${options.regKey}/expire`;
debug(`Going to expire of preapproved payment for regKey: ${options.regKey}...`);
return request.postAsync({
url: url,
headers: this.headers,
json: true
}).then((response) => {
if (response.body.returnCode && response.body.returnCode == "0000"){
debug(`Completed expiring preapproved payment.`);
debug(response.body);
return response.body;
} else {
debug(`Failed to expire preapprove payment.`);
debug(response.body);
return Promise.reject(new Error(response.body));
}
})
}
})
// Check if configured parameters are all valid.
Object.keys(options).map((param) => {
if (!required_params.includes(param) && !optional_params.includes(param)){
throw new Error(`${param} is not a valid parameter.`);
}
})
let url = `https://${this.apiHostname}/${api_version}/payments/${options.transactionId}/confirm`;
let body = JSON.stringify({
amount: options.amount,
currency: options.currency
})
debug(`Going to confirm payment of transaction: ${options.transactionId}...`);
return request.postAsync({
url: url,
headers: this.headers,
body: body
}).then((response) => {
let body = lossless_json.parse(response.body, this._lossless_converter);
if (body.returnCode && body.returnCode == "0000"){
debug(`Completed confirming payment.`);
debug(body);
return body;
} else {
debug(`Failed to confirm payment.`);
debug(body);
return Promise.reject(new Error(body));
}
})
if (!options[param]){
throw new Error(`Required parameter ${param} is missing.`);
}
})
// Check if configured parameters are all valid.
Object.keys(options).map((param) => {
if (!required_params.includes(param) && !optional_params.includes(param)){
throw new Error(`${param} is not a valid parameter.`);
}
})
let url = `https://${this.apiHostname}/${api_version}/payments/request`;
let body = JSON.stringify(options);
debug(`Going to reserve payment...`);
return request.postAsync({
url: url,
headers: this.headers,
body: body
}).then((response) => {
let body = lossless_json.parse(response.body, this._lossless_converter);
if (body.returnCode && body.returnCode == "0000"){
debug(`Completed reserving payment.`);
debug(body);
return body;
} else {
debug(`Failed to reserve payment.`);
debug(body);
return Promise.reject(new Error(body));
}
})
static turn_off(){
// If this is test, we will not actually issue call out.
if (process.env.BOT_EXPRESS_ENV == "test" || process.env.BOT_EXPRESS_ENV == "development"){
debug("This is test so we skip the actual call out.");
return Promise.resolve();
}
debug("Going to turn off the light.");
let url = MAKER_URL_PREFIX + 'wfc_turn_off_light/with/key/' + MAKER_KEY;
return request.postAsync({
url: url,
json: true
});
}
}
async get_access_token(client_id, channel_secret){
const url = `https://api.line.me/v2/oauth/accessToken`;
const form = {
grant_type: "client_credentials",
client_id: client_id,
client_secret: channel_secret
}
const response = await request.postAsync({
url: url,
form: form
})
if (response.statusCode != 200){
throw new Error(`Failed to retrieve token for LINE Messaging API. Status code: ${response.statusCode}.`);
}
const body = JSON.parse(response.body);
if (!body.access_token){
throw new Error(`access_token not found in response.`);
}
return body.access_token;
}
query.AWSAccessKeyId = this.accessKeyId;
if (this.authToken) {
query.MWSAuthToken = this.authToken;
}
query[api.legacy ? 'Merchant' : 'SellerId'] = this.merchantId;
const signedQuery = this.sign(api.path, _.omit(query, body));
requestOpts[api.upload ? 'qs' : 'form'] = signedQuery;
requestOpts.headers = headers;
requestOpts.encoding = 'binary';
return request.postAsync(uri, requestOpts);
}
issue_access_token(code){
const url = `https://api.${this.endpoint}/oauth2/${api_version}/token`;
const form = {
grant_type: "authorization_code",
code: code,
redirect_uri: this.callback_url,
client_id: this.channel_id,
client_secret: this.channel_secret
}
return request.postAsync({
url: url,
form: form
}).then((response) => {
if (response.statusCode == 200){
return JSON.parse(response.body);
}
return Promise.reject(new Error(response.statusMessage));
});
}
static create_answer(answer){
debug("create_answer() started.");
let url = "https://" + process.env.RN_USER + ":" + process.env.RN_PASSWORD + "@" + process.env.RN_HOSTNAME + "/services/rest/connect/v1.3/answers";
let headers = {
"Content-Type": "application/json"
};
return request.postAsync({
url: url,
headers: headers,
body: answer,
json: true
});
}