var request = require('request');
module.exports.handler = async function (event, context) {
let response = {};
try {
let body = JSON.parse(event.body);
let integration_public = JSON.parse(body.integration_public);
let integration_private = JSON.parse(body.integration_private);
if(Object.prototype.toString.call(integration_private) === '[object Array]') {
for(let item_user of integration_private){
let token = item_user.token;
let chatId = item_user.chat_id;
try{
for (let key in body.lead) {
if (body.lead.hasOwnProperty(key)) {
integration_public = integration_public.replace(
new RegExp('%'+key+'%', "g"), body.lead[key]
)
}
}
}
catch(e){
console.log('ERR=',e);
}
let data = {
method: 'post',
payload: {
method: 'sendMessage',
chat_id: String(chatId),
text: integration_public,
parse_mode: 'HTML',
reply_markup: JSON.stringify(null)
}
}
request.post(
'https://api.telegram.org/bot' + token + '/sendMessage?text='+encodeURIComponent(integration_public)+'&chat_id='+chatId,
{ json:data },
function (error, response, body) {
console.log(error, body);
}
);
}
}
} catch (e) {
console.log(e);
response = {err:JSON.stringify(e)}
}
return {
statusCode: 200,
body: JSON.stringify(response)
};
};