<?php

class bot
{
    public $token;
    public $chatid;
    public $recmsg;
    public $message_id;
    public $username;
    public $firstname;
    public $lastname;
    public $datamytext;
    public $datachatid;
    public $phone;
    public $exRecmsg;
    public $exRecGlass;
    public $gpFromChatId;
    public $gpFromFirstname;
    public $gpFromLastname;
    public $gpFromUsername;
    public $gpNewMemberChatid;
    public $gpNewMemberFirstname;
    public $gpNewMemberLastname;
    public $gpNewMemberUsername;
    public $gpLeftMemberChatid;
    public $gpLeftMemberFirstname;
    public $gpLeftMemberLastname;
    public $gpLeftMemberUsername;
    public $gpChatTitle;
    public $gpActionType;
    public $gpSecondMemberChatid;
    public $gpSecondMemberFirstname;
    public $gpSecondMemberLastname;
    public $gpSecondMemberUsername;
    public $backKey;
    public $mainKey;

    function __construct($token, $content)
    {
        $this->token = $token;
        $this->backKey = json_encode(
            array(
                "keyboard" => array(
                    array("🔙 بازگشت")
                ),
                "resize_keyboard" => True
            )
        );
        $this->mainKey = json_encode(
            array(
                "keyboard" => array(
                    array(urlencode("جستجو")),
                ),
                "resize_keyboard" => True
            )
        );

        if ($content != '') {
            $update = json_decode($content, true);
            $message = $update["message"];
            $this->recmsg = $message['text'];
            $this->recmsg = str_replace('@teletools_member_counter_bot', '', $this->recmsg);
            $this->message_id = $message['message_id'];
            $this->chatid = $message['chat']['id'];
            $this->username = $message['from']['username'];
            $this->firstname = $message['from']['first_name'];
            $this->lastname = $message['from']['last_name'];
            $this->datamytext = $update["callback_query"]["data"];
            $this->datachatid = $update["callback_query"]["from"]["id"];
            if ($this->chatid == '') {
                $this->chatid = $this->datachatid;
                $this->recmsg = $this->datamytext;
            }
            $this->phone = $message["contact"]["phone_number"];
            $erecmsg = str_replace("/start", "", $this->recmsg);
            $erecmsg = str_replace(" ", "", $erecmsg);
            $exrec = explode("_", $erecmsg);
            $this->exRecmsg = $exrec;
            $this->exRecGlass = explode("a", $this->datamytext);
            ///Group information
            $this->gpFromChatId = $message['from']['id'];
            $this->gpFromFirstname = $message['from']['first_name'];
            $this->gpFromLastname = $message['from']['last_name'];
            $this->gpFromUsername = $message['from']['username'];

            $this->gpNewMemberChatid = $message['new_chat_participant']['id'];
            $this->gpNewMemberFirstname = $message['new_chat_participant']['first_name'];
            $this->gpNewMemberLastname = $message['new_chat_participant']['last_name'];
            $this->gpNewMemberUsername = $message['new_chat_participant']['username'];


            $this->gpLeftMemberChatid = $message['left_chat_participant']['id'];
            $this->gpLeftMemberFirstname = $message['left_chat_participant']['first_name'];
            $this->gpLeftMemberLastname = $message['left_chat_participant']['last_name'];
            $this->gpLeftMemberUsername = $message['left_chat_participant']['username'];
            $this->gpChatTitle = $message['chat']['title'];

            if (isset($message['new_chat_participant']['id']) && $message['new_chat_participant']['id'] == $this->gpFromChatId) {

                $this->gpActionType = 'joined';
                $this->gpSecondMemberChatid = $message['new_chat_participant']['id'];
                $this->gpSecondMemberFirstname = $message['new_chat_participant']['first_name'];
                $this->gpSecondMemberLastname = $message['new_chat_participant']['last_name'];
                $this->gpSecondMemberUsername = $message['new_chat_participant']['username'];
            } elseif (isset($message['new_chat_participant']['id'])) {
                $this->gpActionType = 'added';
                $this->gpSecondMemberChatid = $message['new_chat_participant']['id'];
                $this->gpSecondMemberFirstname = $message['new_chat_participant']['first_name'];
                $this->gpSecondMemberLastname = $message['new_chat_participant']['last_name'];
                $this->gpSecondMemberUsername = $message['new_chat_participant']['username'];
            } elseif (isset($message['left_chat_participant']['id']) && $message['left_chat_participant']['id'] == $this->gpFromChatId) {
                $this->gpActionType = 'left';
                $this->gpSecondMemberChatid = $message['left_chat_participant']['id'];
                $this->gpSecondMemberFirstname = $message['left_chat_participant']['first_name'];
                $this->gpSecondMemberLastname = $message['left_chat_participant']['last_name'];
                $this->gpSecondMemberUsername = $message['left_chat_participant']['username'];
            } elseif (isset($message['left_chat_participant']['id'])) {
                $this->gpActionType = 'removed';
                $this->gpSecondMemberChatid = $message['left_chat_participant']['id'];
                $this->gpSecondMemberFirstname = $message['left_chat_participant']['first_name'];
                $this->gpSecondMemberLastname = $message['left_chat_participant']['last_name'];
                $this->gpSecondMemberUsername = $message['left_chat_participant']['username'];
            }
        }
    }
  

    function sendphoto($address, $chatid, $key, $caption)
    {

        $bot_url = "https://api.telegram.org/bot" . $this->token . "/";
        $url = $bot_url . "sendPhoto?caption=$caption&reply_markup=$key&parse_mode=Html&chat_id=" . $chatid;
        $file = '' . 'upload' . '/' . $address;
        $mime = mime_content_type($file);
        $info = pathinfo($file);
        $name = $info['basename'];
        $output = new CURLFile($file, $mime, $name);

        $data = array(
            "photo" => $output,
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            $error = curl_error($ch);
        }
        curl_close($ch);

        return $result . $error;
    }

    function sendmessage($text, $chatid, $keys)
    {
        $ch = curl_init("https://api.telegram.org/bot" . $this->token . "/sendmessage?chat_id=$chatid&text=$text&parse_mode=Html&reply_markup=$keys");
        curl_exec($ch);
    }
    function sendFile($address, $text, $chatid, $keys)
    {
        $ch = curl_init("https://api.telegram.org/bot5620820030:AAEfUZgYAOgCTOyTlM8ul8O3CoU1Jrxv3ms/sendDocument?document=https://teleaccmanager.telegramtools.io/dev/telegram_member_counter/mamad.xlsx&chat_id=$chatid");
        curl_exec($ch);


        $result2 = file_get_contents("https://api.telegram.org/bot5620820030:AAEfUZgYAOgCTOyTlM8ul8O3CoU1Jrxv3ms/sendDocument?document=https://teleaccmanager.telegramtools.io/dev/telegram_member_counter/mamad.xlsx&chat_id=$chatid");
        print("RESULT 2 : $result2");
    }
    function channel_member_status($chatid, $group_id)
    {
        $c = curl_init("https://api.telegram.org/bot" . $this->token . "/getChatMember?chat_id=$group_id&user_id=$chatid");
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
        $sended = curl_exec($c);
        if (curl_error($c)) {
            die(curl_error($c));
        }
        $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
        curl_close($c);
        $update2 = json_decode($sended, true);
        $getinfo = $update2["result"]["status"];
        return $getinfo;
    }
    function isAdmin($message_from_id, $chatid)
    {
        #checker
        $channel_member_status = $this->channel_member_status($message_from_id, $chatid);
        if (!($channel_member_status == 'creator' || $channel_member_status == 'administrator')) {
            return True;
        }
        #checker

        return False;
    }

    function inchannel($chatid, $channelChatid)
    {
        $c = curl_init("https://api.telegram.org/bot" . $this->token . "/getChatMember?chat_id=$channelChatid&user_id=$chatid");
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        $sended = curl_exec($c);
        if (curl_error($c)) {
            die(curl_error($c));
        }
        $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
        curl_close($c);
        print($sended);
        $update2 = json_decode($sended, true);
        $getinfo = $update2["result"]["status"];

        if ($getinfo == "creator" || $getinfo == "member" || $getinfo == "administrator") {
            return "ok";
        } else {
            return "no";
        }
    }



    function copyMessage($chatid, $from_chatid, $message_id)
    {
        $send = "https://api.telegram.org/bot" . $this->token . "/copyMessage?chat_id=$chatid&from_chat_id=$from_chatid&message_id=$message_id=";
        $c = curl_init($send);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt(... other options you want...)
        curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
        //curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
        $sended = curl_exec($c);
        if (curl_error($c)) {
            die(curl_error($c));
        }
        $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
        curl_close($c);
        $decodeData = json_decode($sended, true);
        $id_message = $decodeData['result']['message_id'];
        return $id_message;
    }

    function unBanMember($chatid, $channelId)
    {
        $send = "https://api.telegram.org/bot" . $this->token . "/unbanChatMember?chat_id=$channelId&user_id=$chatid&only_if_banned=True";
        $sended = file_get_contents($send);
        print $sended;
    }

    function getChannelLink($channelId)
    {
        $ch = curl_init("https://api.telegram.org/bot" . $this->token . "/createChatInviteLink?chat_id=$channelId&member_limit=1");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        $result = curl_exec($ch);
        print('result ' . $result);
        $data = json_decode($result, true);
        print("THIS DATA  : " . $data['result']['invite_link']);
        $vipChannel = $data['result']['invite_link'];
        if ($vipChannel == "" || $vipChannel == " ") {
            return False;
        } else {
            return $vipChannel;
        }

    }
}


?>