Skip to content

Instantly share code, notes, and snippets.

@abdshomad
Last active October 22, 2016 09:22
Show Gist options
  • Save abdshomad/7d33adc98b2867278d68a7094dee6f79 to your computer and use it in GitHub Desktop.
Save abdshomad/7d33adc98b2867278d68a7094dee6f79 to your computer and use it in GitHub Desktop.
module['exports'] = function arsipBot (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.95 oleh @abdshomad';
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\nBot Arsip ' + text_version + '\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 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.arsip_bot_key + '/sendMessage')
.form({
"chat_id": hook.params.message.chat.id,
"text": text_message
});
var user = hook.params.message.new_chat_participant || hook.params.message.new_chat_member;
if(user) {
request
.post('https://api.telegram.org/bot' + hook.env.arsip_bot_key + '/sendMessage')
.form({
"chat_id": hook.params.message.chat.id,
"text": "Assalaamu'alaikum, Ahlan " + user.first_name + ' ' + user.last_name + ', \n\nKlik /daftar untuk mengisi biodata Anda.'
});
}
console.log('End Bot Version ' + VERSION);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment