Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
.DS_Store

#config files
application/config/database.php

application/cache/*
!application/cache/index.html

Expand Down
2 changes: 1 addition & 1 deletion application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
4 changes: 2 additions & 2 deletions application/config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'username' => 'root',
'password' => 'root',
'database' => '',
'dbdriver' => 'mysqli',
'dbprefix' => '',
Expand Down
33 changes: 33 additions & 0 deletions application/controllers/Question.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Question 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);
}

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);
}

}
54 changes: 54 additions & 0 deletions application/controllers/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Response 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);
}

public function get($type="",$username=""){
$this->load->model('ResponseModel','rs');
$data = $this->rs->getResponse($type,$username);
echo "<table>
<tr>
<td>username</td><td>first_name</td><td>last_name</td><td>message</td><td>response</td>
</tr>
";
foreach ($data as $row){
echo "<tr>";
foreach ($row as $val){
echo "<td>$val</td>";
}
echo "</tr>";
}
echo "</table>";
}

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;
}

}
8 changes: 8 additions & 0 deletions application/controllers/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
157 changes: 157 additions & 0 deletions application/models/QuestionModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class QuestionModel extends CI_Model {

function __construct() {
parent::__construct();
$this->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;

}
}
39 changes: 39 additions & 0 deletions application/models/ResponseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class ResponseModel extends CI_Model {

function __construct() {
parent::__construct();
$this->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;
}
}
Loading