<?php
class telegram_model extends CI_Model {
    public function send_message($tokenkey,$id,$text,$keyboard){
$params=[
    'chat_id'=>$id,
    'text'=>$text,
	'reply_markup'=>json_encode($keyboard),
];
$ch =curl_init("https://api.telegram.org/bot".$tokenkey."/SendMessage");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
    public function send_message_inline($tokenkey,$id,$keyboard){
$params=[
    'chat_id'=>$id,
	'reply_markup'=>json_encode($keyboard),
];
$ch =curl_init("https://api.telegram.org/bot".$tokenkey."/SendMessage");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
public function send_message_only($tokenkey,$id,$text){
$params=[
    'chat_id'=>$id,
    'text'=>$text,
];
$ch =curl_init("https://api.telegram.org/bot".$tokenkey."/SendMessage");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
public function send_photo($tokenkey,$id,$url,$caption = "null"){
if($caption != "null"){
$params=[
    'chat_id'=>$id,
    'photo'=> new CURLFile($url),
	"caption"=>$caption
];
}else{
$params=[
    'chat_id'=>$id,
    'photo'=> new CURLFile($url),
];	
}
$ch =curl_init("https://api.telegram.org/bot".$tokenkey."/sendPhoto");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
public function set_telinfo($data){
$this->db->insert('telegram_information', $data);
}

public function update_token($uid,$data){
	$this->db->where('uid',$uid);
    $this->db->update('telegram_information', $data);
}
public function get_token($url){
	$this->db->where("boturl",$url);
	$result = $this->db->get('telegram_information');
	 return $result->row(0)->tokenkey;
}
public function get_bot_url($uid){
	$this->db->where('uid',$uid);
	$result = $this->db->get('telegram_information');
	 return $result->result();
}
public function get_token_buid($uid){
	$this->db->where("uid",$uid);
	$result = $this->db->get('telegram_information');
	 return $result->row(0)->tokenkey;
}
public function get_group($url){
	$this->db->where("boturl",$url);
	$result = $this->db->get('telegram_information');
	 return $result->row(0)->botgroup;
}
public function get_users_group($groupname){
	$this->db->where("botgroup",$groupname);
	$result = $this->db->get('telegram_information');
	 return $result->result();
}
public function get_group_byuid($uid){
	$this->db->where("uid",$uid);
	$result = $this->db->get('telegram_information');
	 return $result->row(0)->botgroup;
}
public function get_groupuid_byurl($boturl){
	$this->db->where("boturl",$boturl);
	$result = $this->db->get('telegram_information');
	 return $result->row(0)->uid;
}
public function delete_telegram_token($uid){
	$this->db->where("uid",$uid);
	$this->db->delete('telegram_information');
}
public function insert_telegram_table($name){
	$this->load->dbforge();
	$fields = array(
        'name' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
        ),
        'level' => array(
                'type' =>'INT',
                'constraint' => '3',
        ),
        'command' => array(
                'type' => 'VARCHAR',
                'constraint' => '100',
        ),
		'func' => array(
			'type' => 'VARCHAR',
                'constraint' => '100',
		),
		'locate' => array(
			'type' => 'VARCHAR',
            'constraint' => '100',			 
		),
		'text' => array(
			'type' => 'VARCHAR',
                'constraint' => '100',
		),
		'isdiynamic'=>array(
			'type' => 'INT',
                'constraint' => '1',
		),
		'material'=>array(
			'type' => 'INT',
                'constraint' => '1',
		)
);
	$this->dbforge->add_field('id');
	$this->dbforge->add_field($fields);
	$this->dbforge->create_table('telebot_'.$name, TRUE);
}
public function drop_telegram_table($name){
	$this->load->dbforge();
	$this->dbforge->drop_table('telebot_'.$name);
}
public function set_telegram_db($tablename,$data){
	$this->db->insert($tablename, $data);
}
public function delete_telegram_db($tablename){
	$this->db->empty_table($tablename);
}
public function get_telegram_bd_locate($tablename,$locatename){
	$this->db->where('locate',$locatename);
		 $result = $this->db->get($tablename);
        return $result->result();
}
public function get_telegram_bd_dymanic($tablename){
	$this->db->where('isdiynamic',1);
 $result = $this->db->get($tablename);
        return $result->result();
}
public function set_user_info($data){
	$this->db->insert("telegram_users_info", $data);
}
public function get_telegram_bd_name($tablename,$name){
	$this->db->where('name',$name);
		 $result = $this->db->get($tablename);
        return $result->result();
}
public function get_telegram_post($uid,$postid){
	$this->db->where('uid',$uid);
	$this->db->where('postid',$postid);
	 $result = $this->db->get("telegram_post_info");
        return $result->result();
}
public function update_telegram_post($uid,$postid,$data){
    $this->db->where('uid',$uid);
	$this->db->where('postid',$postid);
    $this->db->update('telegram_post_info', $data);
}
public function set_telegram_post($data){
	$this->db->insert("telegram_post_info", $data);
	
}
public function set_telegram_data($data){
	$this->db->insert("telegram_data", $data);
	
}

public function update_telegram_data($uid,$telegramid,$type,$data){
	 $this->db->where('uid',$uid);
	$this->db->where('telegramid',$telegramid);
	$this->db->where('type',$type);
    $this->db->update('telegram_data', $data);
}
public function get_telegram_data($uid,$telegramid,$type){
	$this->db->where('uid',$uid);
	$this->db->where('telegramid',$telegramid);
	$this->db->where('type',$type);
	 $result = $this->db->get("telegram_data");
        return $result->result();
}
public function clear_telegram_data($uid,$telegramid){
	$this->db->where('uid',$uid);
   $this->db->where('telegramid',$telegramid);
	$this->db->delete('telegram_data');
}
public function set_telegram_message($data){
	$this->db->insert("telegram_message", $data);
}
public function get_telegram_message($uid){
	$this->db->where('uid',$uid);
	 $result = $this->db->get("telegram_message");
    return $result->result();
}
public function delete_telegram_message($id){
	$this->db->where('id',$id);
	$this->db->delete('telegram_message');
}
public function set_caption_image($data){
	$this->db->insert("caption_images", $data);
}
public function delete_caption_image($id,$uid){
	$this->db->where('uid',$uid);
	$this->db->where('id',$id);
	$this->db->delete('caption_images'); 
}
public function get_caption_text($uid,$group_name){
	$this->db->where('uid',$uid);
	$this->db->where('group_name',$group_name);
	$result = $this->db->get("caption_images");
	return $result->result();
}
public function get_caption_by_id($uid,$id){
	$this->db->where('uid',$uid);
	$this->db->where('id',$id);
	$result = $this->db->get("caption_images");
	return $result->result();
}
public function update_caption_by_id($uid,$id,$data){
	$this->db->where('uid',$uid);
	$this->db->where('id',$id);
	$this->db->update('caption_images', $data); 
}
public function get_caption_image($uid,$groupname,$imagename){
	$this->db->where('uid',$uid);
	$this->db->where('group_name',$groupname);
	$this->db->where('imagename',$imagename);
	$result = $this->db->get("caption_images");
	$result = $result->result();
    return $result[0]->text;
	
}
public function get_visited_users($uid,$mon){
	$this->db->where('uid',$uid);
	$this->db->where('mon',$mon);
	$result = $this->db->get("visited_bot");
	$result = $result->result();
    return @$result[0]->number;
}
public function set_visited_users($data){
	$this->db->insert("visited_bot", $data);
}
public function update_visited_users($uid,$mon,$data){
	$this->db->where('uid',$uid);
	$this->db->where('mon',$mon);
	$this->db->update('visited_bot', $data);
}
public function is_visited_users_exist($uid,$mon){
$this->db->where("uid",$uid);
$this->db->where("mon",$mon);
	  $result = $this->db->get('visited_bot');
	  if(count($result->result()) == 0){
		  return false;
	  }else{
		  return true;
	  }
}
}
?>