Last active
October 9, 2016 02:00
-
-
Save abdshomad/024bd28d873fe850ca6b 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
module['exports'] = function daftarinBot (hook) { | |
/* | |
TODO: HIGH PRIORITY | |
[ ] Save the data to Firebase or Google Spreadsheet | |
TODO: MEDIUM PRIORITY | |
[ ] Invite developers to collaborate | |
*/ | |
// Version Information and Debug Flag | |
var VERSION = '0.0.49'; | |
console.log('Begin Bot Version ' + VERSION); | |
var debug_flag = true, | |
debug_message = '\n\nDEBUG (ABAIKAN): '; | |
// Required modules | |
var request = require('request'); | |
var S = require('string'); | |
// simple parsing of received text | |
var text_version = 'Versi ' + VERSION, | |
text_message = hook.params.message.text, | |
text_command = text_message.split(' ', 1)[0] + '', | |
text_content = text_message.replace(text_command, '').trim(); | |
if(text_command.indexOf('@') > 1) text_command = text_message.split('@')[0]; // remove @botname_bot | |
// Message Template Constant | |
var PENDAFTARAN_BEGIN = '/data \n\nSalin (copy-paste) keseluruhan teks ini (termasuk teks /data di atas) dan lengkapilah data berikut selengkap-lengkapnya: \n\n------- begin data ------- \n\n', | |
SELAMAT_DATANG = 'Bismillaah. \n\nSelamat datang di Bot Pendaftaran ' + text_version + '. \nGunakan/klik perintah-perintah di bawah ini untuk melanjutkan proses pendaftaran Anda: \n\n/daftar : Memulai proses pendaftaran. \n/data_usaha : Meng-update informasi usaha/bisnis Anda \n/data_sosmed : Meng-update informasi sosial media. \n/update : Meng-update informasi lain. Bisa dilakukan kapan saja (Fitur belum ada). \n\n/bantuan : Menampilkan teks bantuan ini.\n\nAlhamdulillaah.', | |
PENDAFTARAN_END = '\n------- end data -------\n\nAlhamdulillaah.', | |
FORM_DATA_DIRI = '#biodata \n\nNama Lengkap : \nNomor HP : \nE-mail : \nIkut Kajian Di : \nPekerjaan : \nKeahlian : \nTempat Lahir : \nTanggal Lahir : \nAlamat Lengkap : \n\n', | |
FORM_DATA_USAHA = '#data_usaha \n\nNama Usaha : \nJenis Usaha : \nAlamat Lengkap Usaha : \n\n', | |
FORM_DATA_SOSMED = '#data_sosmed \n\nWebsite : \nLinked In : \nFacebook : \nTwitter : \nGoogle : \nInstagram : \nTelegram : \nBBM Channel : \nLINE : \n\n', | |
PENDAFTARAN_PRIBADI = PENDAFTARAN_BEGIN + FORM_DATA_DIRI + PENDAFTARAN_END, | |
PENDAFTARAN_SOSMED = PENDAFTARAN_BEGIN + FORM_DATA_SOSMED + PENDAFTARAN_END, | |
PENDAFTARAN_USAHA = PENDAFTARAN_BEGIN + FORM_DATA_USAHA + PENDAFTARAN_END, | |
PENDAFTARAN_LENGKAP = PENDAFTARAN_BEGIN + FORM_DATA_DIRI + FORM_DATA_USAHA + FORM_DATA_SOSMED + PENDAFTARAN_END, | |
TERIMA_KASIH = 'Alhamdulillaah. Terima kasih. Informasi Anda telah kami simpan. \nTerima kasih telah menggunakan layanan Bot Pendaftaran ' + text_version + '.'; | |
console.log('Begin create JSON data'); | |
// Begin create JSON data | |
var lines = text_message.split(/\r\n|\n|\r/), | |
json_data = {}, | |
field_name = '', | |
start_flag = false, | |
finish_flag = false; | |
for (var i = 0, segments; i < lines.length; i++) { | |
segments = lines[i].trim().split(/\s*[=:]\s*/); | |
// start_flag==true && finish_flag==false is here to prevent | |
// non-data lines to be included inside json_data | |
if(segments.length==2 && start_flag==true && finish_flag==false) { | |
field_name = S(S(segments[0]).slugify().s).replaceAll('-', '_').s; | |
json_data[field_name] = segments[1]; | |
} else { | |
// segments.length == 1 | |
// meaning : This line does not have field name). | |
// meaning : This line is still belongs to previous line. | |
// action : combine this line with previous line(s). | |
if(segments[0]=='—---— end data —---—') finish_flag = true; | |
if(field_name!='' && start_flag==true && finish_flag==false) { | |
json_data[field_name] += segments[0] + '\n'; | |
} | |
if(segments[0]=='—---— begin data —---—') start_flag = true; | |
} | |
} | |
console.log('End create JSON data'); | |
debug_message += '\nJSON.stringify(json_data) = ' + JSON.stringify(json_data); | |
console.log('Begin Firebase'); | |
var firebase = require("firebase"); | |
/* | |
firebase.initializeApp({ | |
serviceAccount: { | |
projectId: "learn-anything-19d86", | |
clientEmail: "learn-firebase-nodejs@learn-anything-19d86.iam.gserviceaccount.com", | |
privateKey: "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDHfuYgOZLZjMc\n2YfkYLuIqtX6NoOvgGANo/lU9iC4sLBLsDDXdKcLqeV+f/F4OiqERH5LKz072Re6\nsv2StrGdpEC5s5WTek157chltHcadii0OdXNmPUHzrug+pKq+N/t+yQxg2J4QL9Y\nVG/2Xsgxsr5WLDW7BVEgrQZG70FQ6p4R5hBOed7wQyGJ5IE+Sdqs+KuOIS3XkS7r\nL6j9b/q3oQXjiMTVA2LyjmexLlfZ8aMbYPCXFQQyENA4krHC7S47Qam9gz08sBpW\nw68Pc02lcE5+BpaTwIdDUzK1B2j8/9uxwHQ91YQIh+kcd+N6AD8A1Exg6m2tIVYP\n9nqKC/qNAgMBAAECggEAIQFbY+q6ChsZnlG1yQ4L6wxq8c5y7CcdGHGaZaLU2ikZ\nSVRejrOu5iwxI/rkj9/W/VTQwEpqK0uH1g8dEcpRtUbppAAa1z2jCXMNqRaj5xgB\nvqrh3ct394AjARIh6UX8KzeGYdHFwG3DcF+oKNbTqoR/6ffEhwRZOtvJ6gni1Jrv\nY0o639I7u5dJ3/pVyNXxL1Fu1a9iKDXjST8XvS30Iyk9BRbXMZXtO1/vgMmbgtpW\niVBVsdgzg3b07bT6eiUlUDGbsfSYOWvGap5n7hRzwO/z8syWRAl5uO0sKl6fMJuq\nC4e9LrXcWbzNFP2gaieqln+Jn3bjeawWaYWwwXbJiQKBgQD4peIN2GL4RvdU+A2A\nR3mepA4u9oZJqMcdwaQf2K+rlIZRvIrqHVH4QXeZswzGkIZVRoAQaGnWu8AXOZhy\n2kAtpzqDieYgPj5rbQmj+odvIABdYAYGF+0oJBt3bZGykc86whmn9pgpTfUKLMCO\nZjILNf7W4lOo4SDcUb3cqj+ykwKBgQDI4ucv0CtqzILc+2mZNPvg1nfmVB/n7t59\n/h982iuoSx1JLoXcXs70tPENP6QftXKpVLJYQe4RQFCfBmpUumhigR4DV61R3+qN\nMHZTBphq+2YhXMzzrF8Vucaxc9MnW3ew+xBnuXnJBsBX8btdNfrkQk9WNFvcqUgD\nYapQ81gyXwKBgDBDerJ3SxaoJ8AdeflpZkkNzRb+kShhE21KNgJcfBTQ+pWJBGnX\nER8gDggOgi9UxE5vJdKJgenFfc7ERjwc8xIpgKkhrkKZ8mHFw3ZJ3doLGycEyYaL\nRGRjRdWTthE0TkY4gORicDysPiEgeKvrWFFmxLWPLu/76vX5A+J+RWjzAoGAbhjd\nd7jxcrDvL8mikFMXkJw6TkPg0K57xjWWcEjqEGxKeqdCugpEBp18VzR/8NjzJJbE\nv9YyccX76YwMjq8zMnltR4uv8+VymTrsPoio1ElAKhzAPvRDkJamrO671SAuMxMG\nZLKkamaG2Cwl8en5vUwSSz6a8g7c5u2J+J/FwKUCgYEAjAAQcXW8xpKL5yTzkco7\nJv+1WPTKT6nKt28ulUUdc+4TNTpHPoMWbByrE57HFnBJxSNgItgq9JxNzYEgjN9l\nQr+3OifJss7/LzOlNHGfbdp+8tDean8gqT7hkWJu1JOaP/Ji+E/wrAjB+Q2qK8cE\nMS9ngaWy9j+dWfuUXQwJEuQ=\n-----END PRIVATE KEY-----\n" | |
}, | |
databaseURL: "https://learn-anything-19d86.firebaseio.com" | |
}); | |
var db = firebase.database(); | |
var ref = db.ref("kajian/pendaftaran"); | |
var newReg = ref.push(json_data); | |
console.log('key = ' + newReg.key); | |
*/ | |
console.log('End Firebase'); | |
console.log('Begin simple routing for text_command using switch()'); | |
switch(text_command) { | |
case '/bantuan': | |
case '/start': | |
case '/mulai': | |
text_message = SELAMAT_DATANG; | |
break; | |
case '/end': | |
case '/finish': | |
case '/selesai': | |
text_message = TERIMA_KASIH; | |
break; | |
case '/daftar': | |
case '/data': | |
case '/data_diri': | |
if(text_content == 'undefined' || text_content.length == 0) { | |
text_message = PENDAFTARAN_PRIBADI; | |
} else { | |
text_message = TERIMA_KASIH; | |
} | |
break; | |
case '/data_usaha': | |
text_message = PENDAFTARAN_USAHA; | |
break; | |
case '/data_sosmed': | |
text_message = PENDAFTARAN_SOSMED; | |
break; | |
case '/update': | |
text_message = PENDAFTARAN_LENGKAP; | |
break; | |
default: | |
text_message = TERIMA_KASIH; | |
} | |
console.log('End simple routing for text_command using switch()'); | |
if(debug_flag) { | |
text_message = text_message + debug_message; | |
} | |
console.log('Begin sending request.post to Telegram API'); | |
request | |
.post('https://api.telegram.org/bot' + hook.env.abds_echo_bot_key + '/sendMessage') | |
.form({ | |
"chat_id": hook.params.message.chat.id, | |
"text": text_message | |
}); | |
console.log('End Bot Version ' + VERSION); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment