diff --git a/.gitignore b/.gitignore
index 269044e..f68ced6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,8 @@
.DS_Store
+#config files
+application/config/database.php
+
application/cache/*
!application/cache/index.html
diff --git a/application/config/config.php b/application/config/config.php
index ed5ab96..cecb79d 100644
--- a/application/config/config.php
+++ b/application/config/config.php
@@ -522,6 +522,6 @@
*/
$config['proxy_ips'] = '';
-define('BOT_TOKEN', 'xxxx');
+define('BOT_TOKEN', '621502946:AAEFxFJlgUW1TOxVB019VIE3levFYFnYWW4');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
define('WEBHOOK_URL', 'https://path.to.webhook');
diff --git a/application/config/database.php b/application/config/database.php
index 82a1ae9..d642b08 100644
--- a/application/config/database.php
+++ b/application/config/database.php
@@ -76,8 +76,8 @@
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
- 'username' => '',
- 'password' => '',
+ 'username' => 'root',
+ 'password' => 'root',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
diff --git a/application/controllers/Question.php b/application/controllers/Question.php
new file mode 100644
index 0000000..3537caf
--- /dev/null
+++ b/application/controllers/Question.php
@@ -0,0 +1,33 @@
+ array(array(
+ array('text' => 'OK', 'callback_data' => 'callback1'),
+ array('text' => 'Not OK', 'callback_data' => 'callback2')
+ )));
+ echo json_encode($arr);
+ }
+
+ public function insert(){
+ $this->load->model('QuestionModel','qs');
+ $jsonArray = json_decode(file_get_contents('php://input'),true);
+ $return = $this->qs->insert($jsonArray);
+ print_r($return);
+ // var_dump($return);
+ }
+
+ public function insertbulk(){
+ $this->load->model('QuestionModel','qs');
+ $jsonArray = json_decode(file_get_contents('php://input'),true);
+ // print_r($jsonArray);
+ $return = $this->qs->insertbulk($jsonArray);
+ print_r($return);
+ // var_dump($return);
+ }
+
+}
diff --git a/application/controllers/Response.php b/application/controllers/Response.php
new file mode 100644
index 0000000..295d52f
--- /dev/null
+++ b/application/controllers/Response.php
@@ -0,0 +1,54 @@
+ array(array(
+ array('text' => 'OK', 'callback_data' => 'callback1'),
+ array('text' => 'Not OK', 'callback_data' => 'callback2')
+ )));
+ echo json_encode($arr);
+ }
+
+ public function get($type="",$username=""){
+ $this->load->model('ResponseModel','rs');
+ $data = $this->rs->getResponse($type,$username);
+ echo "
+
+ | username | first_name | last_name | message | response |
+
+ ";
+ foreach ($data as $row){
+ echo "";
+ foreach ($row as $val){
+ echo "| $val | ";
+ }
+ echo "
";
+ }
+ echo "
";
+ }
+
+ public function save($type="",$username=""){
+ $this->load->model('ResponseModel','rs');
+ $data = $this->rs->getResponse($type,$username);
+
+ // filename for download
+ $filename = $type . ".xls";
+
+ header("Content-Disposition: attachment; filename=\"$filename\"");
+ header("Content-Type: application/vnd.ms-excel");
+
+ echo "username\tfirst_name\tlast_name\t>message\tresponse\r\n";
+ foreach ($data as $row){
+ foreach ($row as $val){
+ echo "$val\t";
+ }
+ echo "\r\n";
+ }
+ exit;
+ }
+
+}
diff --git a/application/controllers/Telegram.php b/application/controllers/Telegram.php
index f5f11d7..25f6f6c 100644
--- a/application/controllers/Telegram.php
+++ b/application/controllers/Telegram.php
@@ -5,6 +5,12 @@ class Telegram extends CI_Controller {
public function index()
{
+ $arr = array(
+ 'inline_keyboard' => array(array(
+ array('text' => 'OK', 'callback_data' => 'callback1'),
+ array('text' => 'Not OK', 'callback_data' => 'callback2')
+ )));
+ echo json_encode($arr);
echo "telegram bot";
}
@@ -33,6 +39,8 @@ public function webhook($reg=""){
if (isset($update["message"])) {
$this->tg->processMessage($update);
+ }elseif ($update['callback_query'] != null) {
+ $this->tg->processMessage($update);
}
}
diff --git a/application/models/QuestionModel.php b/application/models/QuestionModel.php
new file mode 100644
index 0000000..96a33f1
--- /dev/null
+++ b/application/models/QuestionModel.php
@@ -0,0 +1,157 @@
+db = $this->load->database('telegram', TRUE);
+ }
+
+ function insert($data){
+
+ $allowedFields = [
+ "message","answer","todo",
+ "trigger",
+ "prev",
+ "next"
+ ];
+
+ if (!isset($data['message'])){
+ return "No message inputted";
+ }elseif($data['message'] == ""){
+ return "No message inputted";
+ }
+
+ $next = "false";
+ if (isset($data['prev'])){
+ $query = "SELECT next FROM question WHERE id=?";
+ $exec = $this->db->query($query, [$data['prev']]);
+ if($exec->num_rows() == 0){
+ return "No prev found";
+ }else{
+ $next = array_filter(explode(",",$exec->row()->next));
+ }
+ }
+
+ $fields = [];
+ $binds = [];
+ $contents = [];
+ foreach ($data as $key=>$val){
+ if (!in_array($key, $allowedFields)) {
+ unset($data[$key]);
+ }else{
+ array_push($fields,"question.".$key);
+ array_push($binds,'?');
+ array_push($contents,$val);
+ }
+ }
+
+ $query = "INSERT INTO question (".implode(",",$fields).")
+ VALUES (".implode(",",$binds).")";
+ $this->db->query($query, $contents);
+ $lastId = $this->db->insert_id();
+ if($next != "false" && isset($data['prev'])){
+ array_push($next,$lastId);
+ $query = "UPDATE question
+ SET question.next = ?
+ WHERE question.id = ?;";
+
+ $this->db->query($query, [implode(",",$next),$data['prev']]);
+ }
+ return $next;
+ // return $next;
+
+ }
+
+ function insertbulk($databulk){
+
+ foreach ($databulk as $data) {
+ $allowedFields = [
+ "key","type",
+ "message","answer","todo",
+ "trigger",
+ "prev",
+ "next"
+ ];
+
+ if (!isset($data['message'])){
+ echo "No message inputted\n";
+ }elseif($data['message'] == ""){
+ echo "No message inputted\n";
+ }
+
+ $next = 'false';
+ $nextArr = [];
+ if (isset($data['prev']) && $data['prev'] != ''){
+ $query = "SELECT question.next,question.key FROM question WHERE question.key in ? and question.type = ?";
+ $prevArr = explode(',',$data['prev']);
+ $exec = $this->db->query($query, [$prevArr,$data['type']]);
+ if($exec->num_rows() == 0){
+ echo "No prev key found".$data['prev']."\n";
+ }else{
+ foreach ($exec->result() as $row)
+ {
+ $next = array_filter(explode(",",$row->next));
+ $nextArr[$row->key] = $next;
+ }
+ }
+ }
+
+ $fields = [];
+ $binds = [];
+ $contents = [];
+ foreach ($data as $key=>$val){
+ if (!in_array($key, $allowedFields)) {
+ unset($data[$key]);
+ }else{
+ if($val != null){
+ array_push($fields,"question.".$key);
+ array_push($binds,'?');
+ if($key == 'answer' && $val != ''){
+ if(is_object($val)){
+ $choices = [];
+ foreach($val as $key2 => $val2){
+ $choice = (object) array('text' => $val2,'callback_data' => $key2);
+ array_push($choices,$choice);
+ }
+ $keyboard = (object) array('inline_keyboard' => [$choices]);
+ $val = json_encode($keyboard);
+ }elseif(is_array($val)){
+ $choices = [];
+ foreach($val as $val2){
+ array_push($choices,$val2);
+ }
+ $keyboard = (object) array('keyboard' => [$choices],
+ 'one_time_keyboard' => true,
+ 'resize_keyboard' => true);
+ $val = json_encode($keyboard);
+ }
+ }
+ array_push($contents,$val);
+ }
+ }
+ }
+
+ $query = "INSERT INTO question (".implode(",",$fields).")
+ VALUES (".implode(",",$binds).")";
+ $this->db->query($query, $contents);
+ $lastId = $this->db->insert_id();
+ if($next != "false" && isset($data['prev'])){
+ foreach($nextArr as $key => $next){
+ array_push($next,$data['key']);
+ $query = "UPDATE question
+ SET question.next = ?
+ WHERE question.key in ? and question.type = ?;";
+ $prevArr = explode(',',$key);
+
+ $this->db->query($query, [implode(",",$next),$prevArr,$data['type']]);
+ }
+ }
+ }
+ return "ok";
+ // return $next;
+
+ }
+}
\ No newline at end of file
diff --git a/application/models/ResponseModel.php b/application/models/ResponseModel.php
new file mode 100644
index 0000000..8bfb09e
--- /dev/null
+++ b/application/models/ResponseModel.php
@@ -0,0 +1,39 @@
+db = $this->load->database('telegram', TRUE);
+ }
+
+ function getResponse($type = "",$username = ""){
+
+ $filter = "where 1 ";
+ if ($type != ""){
+ $filter .= " and response.questionType = '$type'";
+ }
+ if ($username != ""){
+ $filter .= " and users.username = '$username'";
+ }
+
+ $query = "SELECT users.username, users.first_name, users.last_name, question.message, response.response
+ FROM response
+ JOIN question ON question.key = response.questionKey AND question.type = response.questionType
+ LEFT JOIN users ON users.user_id = response.userId $filter ;";
+
+ $data = [];
+ $exec = $this->db->query($query);
+ if($exec->num_rows() == 0){
+ echo "No response found";
+ }else{
+ foreach ($exec->result() as $row)
+ {
+ array_push($data,$row);
+ }
+ }
+ return $data;
+ }
+}
\ No newline at end of file
diff --git a/application/models/TelegramModel.php b/application/models/TelegramModel.php
index f753b23..c350f33 100644
--- a/application/models/TelegramModel.php
+++ b/application/models/TelegramModel.php
@@ -120,11 +120,162 @@ function apiRequestJson($method, $parameters) {
return $this->exec_curl_request($handle);
}
+ function updateUserPrev($user_id,$state){
+ $this->db->query("UPDATE status SET prev='$state' WHERE user_id=$user_id");
+ }
+ function updateUserNext($user_id,$state){
+ $this->db->query("UPDATE status SET next='$state' WHERE user_id=$user_id");
+ }
+ function updateUserQuestionType($user_id,$state){
+ $this->db->query("UPDATE status SET questiontype='$state' WHERE user_id=$user_id");
+ }
+ function saveResponse($prev,$type,$user_id,$response){
+ $query = "REPLACE INTO response
+ (questionKey,questionType,userId,response)
+ VALUES
+ (?,?,?,?);";
+ $this->db->query($query,[$prev,$type,$user_id,$response]);
+ }
+
+ function getQuestion($trigger,$user_id){
+ $status = $this->db->query("SELECT `questiontype`,`prev`, `next` FROM `status` WHERE `user_id`='$user_id'")->row();
+
+ $filter = "";
+ if ($status->next != ''){
+ $filter .= " and `key` in ($status->next)";
+ }
+ if ($status->prev != ''){
+ $filter .= " and FIND_IN_SET('$status->prev',`prev`)";
+ }else{
+ $filter .= " and `prev` = ''";
+ }
+
+ if ($status->next != '' || $status->prev != ''){
+ $filter .= " and `type` = '$status->questiontype'";
+ }
+
+
+ $query = "SELECT question.id as id, question.key as qkey, question.type as type, question.prev as prev, question.next as next, question.message as message, question.answer as answer FROM question WHERE `trigger`='$trigger' $filter";
+ $return = $this->db->query($query)->row();
+ if ($return != null) {
+ return $return;
+ }
+
+ $query = "SELECT question.id as id, question.key as qkey, question.type as type, question.prev as prev, question.next as next, question.message as message, question.answer as answer FROM question WHERE `trigger`='' $filter";
+ $return = $this->db->query($query)->row();
+ if ($return != null) {
+ return $return;
+ }
+ }
+
function processMessage($update) {
$this->saveRawUpdate($update);
if($this->isUpdateExist($update)){
return;
}
+
+ if($update['callback_query'] != null) {
+ $message = $update['callback_query']["message"];
+ $message_id = $message['message_id'];
+ $chat_id = $message['chat']['id'];
+ $user_id = $message['chat']['id'];//pake yg chat
+ $text = $update['callback_query']['data'];
+ }else{
+ $message = $update["message"];
+ $message_id = $message['message_id'];
+ $chat_id = $message['chat']['id'];
+ $user_id = $message['from']['id'];
+ $text = $message['text'];
+ }
+
+ $status = $this->getUserStatus($user_id);
+ if($status != "registration_done"){
+ return $this->processMessageRegister($update);
+ }
+
+ if(isset($message['entities']) && isset($message['entities']['type'])){
+ //do nothing
+ }elseif (isset($message['text'])) {
+ // incoming text message
+ $this->saveMessage($message);
+
+
+ //dynamic question from db start here
+ $question = $this->getQuestion($text,$user_id);
+ if (strpos($text, "/reset") === 0) {
+ $this->updateUserPrev($user_id,'');//reset
+ $this->updateUserNext($user_id,'');//reset
+ $this->updateUserQuestionType($user_id,'');//reset
+ $sendMessage = array('chat_id' => $chat_id, "text" => "Reset success.");
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+ }elseif($question != null){
+ $sendText = $question->message;
+ $sendMessage = array('chat_id' => $chat_id, "text" =>$sendText);
+ if($question->answer != null && $question->answer != ""){
+ $sendMessage["reply_markup"] = json_decode($question->answer,true);
+ }
+ $this->apiRequestJson("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+
+ if($question->prev != ''){
+ $userStatus = $this->getUserStatusComplete($user_id);
+ $this->saveResponse($userStatus->prev,$userStatus->questiontype,$user_id,$text);//save answer if prev (id last question) exist;
+ }
+
+ $this->updateUserPrev($user_id,$question->qkey);// use id not prev, because it is the current state of user after finishing the question
+ $this->updateUserNext($user_id,$question->next);
+ $this->updateUserQuestionType($user_id,$question->type);
+
+ }else{
+ $userStatus = $this->getUserStatusComplete($user_id);
+ // $this->apiRequest("sendMessage", $userStatus);
+ // $this->saveBotMessage($message_id,$sendMessage);
+ if($userStatus->prev != '' && $userStatus->questiontype != ''){
+ $this->saveResponse($userStatus->prev,$userStatus->questiontype,$user_id,$text);//save answer if prev (id last question) exist;
+ $this->updateUserPrev($user_id,'');//reset
+ $this->updateUserNext($user_id,'');//reset
+ $this->updateUserQuestionType($user_id,'');//reset
+ $sendMessage = array('chat_id' => $chat_id, "text" => "Selamat anda telah selesai menjawab pertanyan ".$userStatus->questiontype);
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+ }else{
+ $sendMessage = array('chat_id' => $chat_id, "text" => "There is invalid action. Please continue with the proper answer or try to /reset");
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+ }
+ }
+
+ return;
+
+ //test purpose
+ $sendMessage = array('chat_id' => $chat_id, "text" => $message);
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+ $sendMessage = array('chat_id' => $chat_id, "text" => $question);
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+
+
+ return;
+
+ //debug purpose
+ $sendMessage = array('chat_id' => $chat_id, "text" => $message);
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+ $sendMessage = array('chat_id' => $chat_id, "text" => $question);
+ $this->apiRequest("sendMessage", $sendMessage);
+ $this->saveBotMessage($message_id,$sendMessage);
+
+ return;
+ }
+ }
+
+ function processMessageRegister($update) {
+ // $this->saveRawUpdate($update);
+ // if($this->isUpdateExist($update)){
+ // return;
+ // }
$message = $update["message"];
// process incoming message
$message_id = $message['message_id'];
@@ -197,7 +348,7 @@ function processMessage($update) {
$sendMessage = array('chat_id' => $chat_id, "text" => 'Terima kasih! Saya akan menghubungi Anda lagi besok.', 'reply_markup' => array('hide_keyboard' => true));
$this->apiRequest("sendMessage", $sendMessage);
$this->saveBotMessage($message_id,$sendMessage);
- $this->updateUserStatus($user_id,"registration_done","survey_one");
+ $this->updateUserStatus($user_id,"registration_done","");
} else if (strpos($text, "/stop") === 0) {
// stop now
$this->saveMessage($message);
@@ -213,7 +364,7 @@ function processMessage($update) {
$sendMessage = array('chat_id' => $chat_id, "text" => 'Terima kasih! Saya akan menghubungi Anda lagi besok.', 'reply_markup' => array('hide_keyboard' => true));
$this->apiRequest("sendMessage", $sendMessage);
$this->saveBotMessage($message_id,$sendMessage);
- $this->updateUserStatus($user_id,"registration_done","survey_one");
+ $this->updateUserStatus($user_id,"registration_done","");
} else {
$this->apiRequest("sendMessage", array('chat_id' => $chat_id, "text" => 'Jawaban yang anda kirimkan salah'));
}
@@ -268,10 +419,6 @@ function processMessage($update) {
$this->saveMessage($message);
}
}
-
-
-
-
}
function saveRawUpdate($update){
@@ -400,6 +547,12 @@ function getUserStatus($user_id){
}
+ function getUserStatusComplete($user_id){
+ $query = "SELECT prev,next,questiontype FROM status WHERE user_id=?";
+ $exec = $this->db->query($query, [$user_id]);
+ return $exec->row();
+ }
+
function updateUserStatus($user_id,$status,$next=""){
$this->db->query("UPDATE status SET status='$status', next='$next' WHERE user_id=$user_id");
}
diff --git a/database/database.sql b/database/database.sql
new file mode 100644
index 0000000..a871ba4
--- /dev/null
+++ b/database/database.sql
@@ -0,0 +1,261 @@
+-- MySQL dump 10.13 Distrib 5.7.17, for macos10.12 (x86_64)
+--
+-- Host: localhost Database: telegrambot
+-- ------------------------------------------------------
+-- Server version 5.5.5-10.0.23-MariaDB
+
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
+/*!40101 SET NAMES utf8 */;
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
+/*!40103 SET TIME_ZONE='+00:00' */;
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
+
+--
+-- Table structure for table `chats`
+--
+
+DROP TABLE IF EXISTS `chats`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `chats` (
+ `id` int(64) NOT NULL,
+ `type` varchar(256) NOT NULL,
+ `title` varchar(256) NOT NULL,
+ `username` varchar(256) NOT NULL,
+ `first_name` varchar(256) NOT NULL,
+ `last_name` varchar(256) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `chats`
+--
+
+LOCK TABLES `chats` WRITE;
+/*!40000 ALTER TABLE `chats` DISABLE KEYS */;
+INSERT INTO `chats` VALUES (510182980,'private','','mhseptiadi','Muhammad','Septiadi'),(677588186,'private','','mh_septiadi','Muhammad','Septiadi');
+
+/*!40000 ALTER TABLE `chats` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `messages`
+--
+
+DROP TABLE IF EXISTS `messages`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `messages` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `message_id` int(32) NOT NULL,
+ `from_user` int(32) NOT NULL,
+ `dates` datetime NOT NULL,
+ `chat_id` int(32) NOT NULL,
+ `texts` text NOT NULL,
+ `attachment` text NOT NULL,
+ `attachment_type` varchar(32) NOT NULL,
+ `connected_website` varchar(256) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `messages`
+--
+
+LOCK TABLES `messages` WRITE;
+/*!40000 ALTER TABLE `messages` DISABLE KEYS */;
+/*!40000 ALTER TABLE `messages` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `question`
+--
+
+DROP TABLE IF EXISTS `question`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `question` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `type` varchar(45) NOT NULL DEFAULT '',
+ `key` varchar(45) NOT NULL DEFAULT '',
+ `message` varchar(256) NOT NULL,
+ `answer` varchar(256) NOT NULL DEFAULT '',
+ `todo` varchar(45) NOT NULL DEFAULT '',
+ `trigger` varchar(256) NOT NULL DEFAULT '',
+ `prev` varchar(45) NOT NULL DEFAULT '',
+ `next` varchar(45) NOT NULL DEFAULT '',
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=215 DEFAULT CHARSET=utf8;
+
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `question`
+--
+
+LOCK TABLES `question` WRITE;
+/*!40000 ALTER TABLE `question` DISABLE KEYS */;
+INSERT INTO `question` VALUES (196,'biodata','1','siapakah nama anda?','','','/biodata','','2'),(197,'biodata','2','jenis kelamin anda?','{\"inline_keyboard\":[[{\"text\":\"pria\",\"callback_data\":\"male\"},{\"text\":\"wanita\",\"callback_data\":\"female\"}]]}','','','1','3,4,3,4'),(198,'biodata','3','apakah anda mengandung?','{\"inline_keyboard\":[[{\"text\":\"ya\",\"callback_data\":\"ya\"},{\"text\":\"tidak\",\"callback_data\":\"tidak\"}]]}','','female','2','4,4'),(199,'biodata','4','apakah anda sudah menikah?','{\"inline_keyboard\":[[{\"text\":\"ya\",\"callback_data\":\"ya\"},{\"text\":\"tidak\",\"callback_data\":\"tidak\"}]]}','','','2,3','5,5'),(200,'biodata','5','berapakah umur anda?','','','','4','6,6'),(201,'biodata','6','berapakah jumlah anggota keluarga anda?','','','','5',''),(214,'register','1','Selamat datang di SID-Indonesia!\n Saya akan bertanya kepada Anda secara berkala tentang keadaan dan kesejahteraan Anda sehari-hari.\n Sebelum melanjutkan, silahkan kunjungi lembar persetujuan di laman berikut.\n ','{\"keyboard\":[[\"Setuju\",\"Tidak Setuju\"]],\"one_time_keyboard\":true,\"resize_keyboard\":true}','','/register','','');
+
+/*!40000 ALTER TABLE `question` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `raw`
+--
+
+DROP TABLE IF EXISTS `raw`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `raw` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `update_id` int(32) NOT NULL,
+ `content` text NOT NULL,
+ `datetimes` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `raw`
+--
+
+LOCK TABLES `raw` WRITE;
+/*!40000 ALTER TABLE `raw` DISABLE KEYS */;
+/*!40000 ALTER TABLE `raw` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `response`
+--
+
+DROP TABLE IF EXISTS `response`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `response` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `questionKey` varchar(45) NOT NULL DEFAULT '',
+ `questionType` varchar(45) NOT NULL DEFAULT '',
+ `userId` int(32) NOT NULL DEFAULT '0',
+ `response` varchar(45) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `index2` (`userId`,`questionKey`)
+) ENGINE=InnoDB AUTO_INCREMENT=59 DEFAULT CHARSET=utf8;
+
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `response`
+--
+
+LOCK TABLES `response` WRITE;
+/*!40000 ALTER TABLE `response` DISABLE KEYS */;
+INSERT INTO `response` VALUES (48,'1','biodata',510182980,'wawan'),(49,'2','biodata',510182980,'female'),(50,'3','biodata',510182980,'tidak'),(51,'4','biodata',510182980,'ya'),(52,'5','biodata',510182980,'21'),(53,'6','biodata',510182980,'2'),(54,'1','biodata',677588186,'tia'),(55,'2','biodata',677588186,'male'),(56,'4','biodata',677588186,'ya'),(57,'5','biodata',677588186,'34'),(58,'6','biodata',677588186,'4');
+
+/*!40000 ALTER TABLE `response` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `status`
+--
+
+DROP TABLE IF EXISTS `status`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `status` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `user_id` int(11) NOT NULL,
+ `status` varchar(32) NOT NULL,
+ `prev` varchar(32) NOT NULL DEFAULT '',
+ `next` varchar(32) NOT NULL DEFAULT '',
+ `questiontype` varchar(45) NOT NULL DEFAULT '',
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM AUTO_INCREMENT=49 DEFAULT CHARSET=latin1;
+
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `status`
+--
+
+LOCK TABLES `status` WRITE;
+/*!40000 ALTER TABLE `status` DISABLE KEYS */;
+INSERT INTO `status` VALUES (46,510182980,'registration_done','','',''),(48,677588186,'registration_done','','','');
+
+/*!40000 ALTER TABLE `status` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `updates`
+--
+
+DROP TABLE IF EXISTS `updates`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `updates` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `update_id` int(32) NOT NULL,
+ `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `updates`
+--
+
+LOCK TABLES `updates` WRITE;
+/*!40000 ALTER TABLE `updates` DISABLE KEYS */;
+/*!40000 ALTER TABLE `updates` ENABLE KEYS */;
+UNLOCK TABLES;
+
+--
+-- Table structure for table `users`
+--
+
+DROP TABLE IF EXISTS `users`;
+/*!40101 SET @saved_cs_client = @@character_set_client */;
+/*!40101 SET character_set_client = utf8 */;
+CREATE TABLE `users` (
+ `id` int(32) NOT NULL AUTO_INCREMENT,
+ `user_id` int(32) NOT NULL,
+ `is_bot` tinyint(1) NOT NULL,
+ `first_name` varchar(256) NOT NULL,
+ `last_name` varchar(256) NOT NULL,
+ `username` varchar(256) NOT NULL,
+ `language_code` varchar(256) NOT NULL,
+ `contact` varchar(32) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=49 DEFAULT CHARSET=utf8;
+
+/*!40101 SET character_set_client = @saved_cs_client */;
+
+--
+-- Dumping data for table `users`
+--
+
+LOCK TABLES `users` WRITE;
+/*!40000 ALTER TABLE `users` DISABLE KEYS */;
+INSERT INTO `users` VALUES (46,510182980,0,'Muhammad','Septiadi','mhseptiadi','en-US','6287889217654'),(48,677588186,0,'Muhammad','Septiadi','mh_septiadi','en-ID','6281281138003');
+
+/*!40000 ALTER TABLE `users` ENABLE KEYS */;
+UNLOCK TABLES;
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
+
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
+
+-- Dump completed on 2018-08-29 8:02:38
+
diff --git a/database/question.post b/database/question.post
new file mode 100644
index 0000000..e91f625
--- /dev/null
+++ b/database/question.post
@@ -0,0 +1,76 @@
+[
+ {
+ "type":"biodata",
+ "key":"1",
+ "trigger":"/biodata",
+ "message":"siapakah nama anda?",
+ "answer":null,
+ "prev":""
+ },
+ {
+ "type":"biodata",
+ "key":"2",
+ "trigger":null,
+ "message":"jenis kelamin anda?",
+ "answer":{
+ "male":"pria",
+ "female":"wanita"
+ },
+ "prev":"1"
+ },
+ {
+ "type":"biodata",
+ "key":"3",
+ "trigger":"female",
+ "message":"apakah anda mengandung?",
+ "answer":{
+ "ya":"ya",
+ "tidak":"tidak"
+ },
+ "prev":"2"
+ },
+ {
+ "type":"biodata",
+ "key":"4",
+ "trigger":"",
+ "message":"apakah anda sudah menikah?",
+ "answer":{
+ "ya":"ya",
+ "tidak":"tidak"
+ },
+ "prev":"2,3"
+ },
+ {
+ "type":"biodata",
+ "key":"5",
+ "trigger":null,
+ "message":"berapakah umur anda?",
+ "answer":null,
+ "prev":"4"
+ },
+ {
+ "type":"biodata",
+ "key":"6",
+ "trigger":null,
+ "message":"berapakah jumlah anggota keluarga anda?",
+ "answer":null,
+ "prev":"5"
+ }
+]
+
+---------------------
+
+[
+ {
+ "type":"register",
+ "key":"1",
+ "trigger":"/register",
+ "message":"Selamat datang di SID-Indonesia!
+ Saya akan bertanya kepada Anda secara berkala tentang keadaan dan kesejahteraan Anda sehari-hari.
+ Sebelum melanjutkan, silahkan kunjungi lembar persetujuan di laman berikut.
+ https://www.facebook.com/notes/beats/lembar-persetujuan/2017675754940751",
+ "answer":["Setuju", "Tidak Setuju"],
+ "prev":""
+ }
+
+]
\ No newline at end of file
diff --git a/database/telegrambot.sql b/database/telegrambot.sql
index 171a575..31f27be 100644
--- a/database/telegrambot.sql
+++ b/database/telegrambot.sql
@@ -78,7 +78,8 @@ CREATE TABLE `status` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`status` varchar(32) NOT NULL,
- `next` varchar(32) NOT NULL
+ `next` varchar(32) NOT NULL,
+ `state` int(32) DEFAULT 0
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
@@ -182,3 +183,26 @@ ALTER TABLE `users`
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
+
+
+
+
+CREATE TABLE `question` (
+ `id` int(32) NOT NULL,
+ `message` varchar(256) NOT NULL,
+ `answer` varchar(256) DEFAULT '',
+ `trigger` varchar(256) DEFAULT '',
+ `prev` int(32) DEFAULT 0
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+ALTER TABLE `question`
+ ADD PRIMARY KEY (`id`);
+
+ALTER TABLE `question`
+ MODIFY `id` int(32) NOT NULL AUTO_INCREMENT;
+
+INSERT INTO `question` (`message`,`trigger`) VALUES ("this is question with no format answer",'/test1');
+INSERT INTO `question` (`message`,`prev`) VALUES ("this should be triggered by no format answer",1);
+INSERT INTO `question` (`message`,`answer`,`trigger`) VALUES ("this is question with format answer",'{"inline_keyboard":[[{"text":"OK","callback_data":"callback1"},{"text":"Not OK","callback_data":"callback2"}]]}','/test2');
+INSERT INTO `question` (`message`,`answer`,`trigger`,`prev`) VALUES ("OK received",'','callback1',3);
+INSERT INTO `question` (`message`,`answer`,`trigger`,`prev`) VALUES ("Not OK received",'','callback2',3);
\ No newline at end of file