Last active
October 22, 2016 09:19
-
-
Save abdshomad/aab73aabe09546b1a99bc952792d669c 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 sedekahBot (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.2 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 Sedekah ' + text_version + '\n\nLengkapilah data berikut untuk mulai bersedekah: \n\n------- begin data ------- \n\n', | |
SELAMAT_DATANG = 'Bismillaah. \n\nSelamat datang di Bot Sedekah ' + text_version + '. \nGunakan/klik perintah-perintah di bawah ini untuk melanjutkan proses pendaftaran Anda: \n\n/daftar : Memulai proses pendaftaran. \n\n/sedekah : Mulai bersedekah. \n\n/bantuan : Menampilkan teks bantuan ini.\n\nAlhamdulillaah.', | |
PENDAFTARAN_END = '\n------- end data -------\n\nAlhamdulillaah.', | |
FORM_SEDEKAH = '#sedekah \n\nNama Lengkap : \nNomor HP : \nE-mail : \nAlamat Pengambilan Barang : \n', | |
PENDAFTARAN_SEDEKAH = PENDAFTARAN_BEGIN + FORM_SEDEKAH + PENDAFTARAN_END, | |
PENDAFTARAN_LENGKAP = PENDAFTARAN_BEGIN + PENDAFTARAN_END, | |
TERIMA_KASIH = 'Alhamdulillaah. Terima kasih. Informasi Anda telah kami simpan. \nTerima kasih telah menggunakan layanan Bot Sedekah ' + 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_SEDEKAH; | |
} else { | |
text_message = TERIMA_KASIH; | |
} | |
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.sedekah_bot_key + '/sendMessage') | |
.form({ | |
"chat_id": hook.params.message.chat.id, | |
"text": text_message | |
}); | |
// arsip | |
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.sedekah_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