<?php
// 主服务器 推送测试
namespace app\service\plugins;

use think\facade\Db;
use think\facade\Cache;
use think\facade\Log;
use app\service\Telegram;
use app\model\Member;
use app\service\Customer;


class Premium
{
    private $data = array();
    private $bots;
    private $telegram;
    private $times;
    private $token;
    private $Member;
    private $userinfo;
    private $Customer;
    


    public function __construct($data, $botid)
    {
        $botinfo = Db::name("admin_bot")->where("botid", $botid)->find();
        $this->data = $data;
        $this->bots = $botinfo;
        $this->Customer=new Customer();
        $this->times = date('Y-m-d H:i:s');
        $this->token = base64_encode($botinfo['botkey']);
        $this->telegram = new Telegram($botinfo['botkey']);
        $this->Member = new Member();
    }
    public function handlename($from)
    {
        $tgid = intval($from["id"]);
        if (isset($from['last_name'])) {
            $last_name = $from['first_name'] . $from['last_name'];
        } else {
            $last_name = isset($from['first_name']) ? $from['first_name'] : "";
        }
        $userfornameq = $last_name;
        if (mb_strlen($last_name, 'utf-8') > 6) {
            $last_name = mb_substr($last_name, 0, 6) . "...";
        } else {
            $last_name = $last_name;
        }


        $tghao = isset($from['username']) ? $from['username'] : "";
        $Markdown = '[' . $last_name . '](tg://user?id=' . $tgid . ')';
        $HTML = '<a href="' . 'tg://user?id=' . $tgid . '">' . $last_name . '</a>';
        $is_bot = isset($from['is_bot']) ? $from['is_bot'] : false;
        $Rename = md5($tghao . $userfornameq . $last_name);
        $hui = array("tghao" => $tghao, 'tgname' => htmlspecialchars($userfornameq), 'tgnames' => htmlspecialchars($last_name), 'Markdown' => $Markdown, 'HTML' => $HTML, "rename" => $Rename, "id" => $tgid, 'is_bot' => $is_bot);

        return $hui;
    }
    public function init($func, $from, $chat, $date)
    {
        //var_dump($func, $from, $chat, $date);
        if (isset($chat['type'])) {
            if ($chat['type'] == "private") {


                $this->userinfo =  $this->Member->where('userid', '=', $from['id'])->where('botid', $this->bots['botid'])->find();

                $handlename = $this->handlename($from);

                if (empty($this->userinfo)) {
                    // var_dump($this->times);
                    $res = $this->Member->save(['email' => "Telegram" . $from['id'] . "@kuiba.con", 'password' => md5($this->times), 'nickname' => $handlename['tgname'], 'sex' => '2', 'vip_time' => 0, 'login_time' => $this->times, "login_ip" => "127.0.0.1", 'status' => '1', 'create_time' => $this->times, 'update_time' => $this->times, 'botid' => $this->bots['botid'], 'userid' => $from['id'], 'botnickname' => $this->bots['name']]);
                    if ($res) {
                        //return $this->success('操作成功');
                    } else {
                        return $this->error('操作失败');
                    }
                } else {
                    $this->Member->where('userid', '=', $from['id'])->where('botid', $this->bots['botid'])->update(['nickname' => $handlename['tgname'], 'login_time' => $this->times, 'update_time' => $this->times]);
                }
            }
        }
    }

    public function command($command, $param, $message_id, $from, $chat, $date, $is_topic_message = null)
    {
        if ($command == "/start") {
            if ($chat['type'] == "private") {

                $startbanner = isset($this->bots['startbanner']) ? $this->bots['startbanner'] : "https://img1.imgtp.com/2023/04/25/KvnPEf7O.jpg";
                //在这里更新一个按钮
                //根据开启功能引导
                $inline_keyboard = array(
                    array(
                        array(
                            'text' => '🛒购买telegram 会员',
                            'callback_data' => 'yindan huiyuan'
                        )

                    )
                );




                $inline_keyboard[] = array(
                    array(
                        'text' => '客服中心',
                        'callback_data' => 'CUSTOMER_SERVICE'
                    ),
                    array(
                        'text' => '❎关闭',
                        'callback_data' => 'close'
                    )
                );


                $button = json_encode(
                    array(
                        'inline_keyboard' => $inline_keyboard
                    )
                );

                //, $msgg, null, $button);

                $this->telegram->sendPhoto($chat['id'], $startbanner, "欢迎光临", null, $button); // $this->telegram->sendMessage 
                $button = json_encode(
                    array(
                        'keyboard' => array(
                            array(
                                array(
                                    'text' => '购买会员',
                                ),
                                array(
                                    'text' => '价格',
                                ),
                                array(
                                    'text' => '赠送朋友会员',
                                )
                            ),
                            array(

                                array(
                                    'text' => '订单管理',
                                ),
                                array(
                                    'text' => '个人中心',
                                ),
                                array(
                                    'text' => '联系客服',
                                )
                            ),

                        ),
                        'one_time_keyboard' => false,
                        'resize_keyboard' => true,
                        'input_field_placeholder' => "请在下发点击您需要的按钮",
                        'selective' => false,
                    )
                );


                $startnumberone = "24小时 会员代开";
                var_dump($startnumberone);
                $this->telegram->KeyboardButton($chat['id'], $startnumberone, $button);
            }
        }
        if ($command == "/mp") {
            if ($this->bots['botad'] == $from['id']) {

                $zongDingDan = Db::name("order")->where(array("botid" => $this->bots['botid']))->count();
                $successDan = Db::name("order")->where(array("botid" => $this->bots['botid'], "start" => 1))->count();
                $successmoney = Db::name("order")->where(array("botid" => $this->bots['botid'], "start" => 1))->sum("money");
                $premium = Db::name("order")->where(array("botid" => $this->bots['botid'], "moth" => 'huiyuangoumaai', "start" => 1))->sum("money");
                $paymoney = Db::name("order")->where(array("botid" => $this->bots['botid'], "moth" => 'yuepay', "start" => 1))->sum("money");
                $lirunmoney = Db::name("order")->where(array("botid" => $this->bots['botid'], "moth" => 'huiyuangoumaai', "start" => 1))->sum("lirun");

                $usermoneycount = $this->Member->where(array("botid" => $this->bots['botid']))->sum("money");
                $msg = "销售面板" . "\r\n" . "代充余额:<strong>" . $this->bots['money'] . "</strong>" . "\r\n" . "总订单:<strong>" . $zongDingDan . "单</strong>" . "\r\n" . "成功订单:<strong>" . $successDan . "单</strong>" . "\r\n" . "成功金额:<strong>" . $successmoney . "U 会员(" . $premium . ")+充值(" . $paymoney . ")</strong>" . "\r\n" . "利润金额:<strong>" . $lirunmoney . "U</strong>" . "\r\n" . "会员余额:<strong>" . $usermoneycount . "U</strong>";
                $button = json_encode(
                    array(
                        'inline_keyboard' => array(
                            array(
                                array(
                                    'text' => '设置价格',
                                    'callback_data' => 'adminset price'
                                ),
                                array(
                                    'text' => '代充绑定',
                                    'callback_data' => 'adminset trxadd'
                                ),
                                array(
                                    'text' => 'api信息',
                                    'callback_data' => 'adminset api'
                                )

                            ),
                            array(
                                array(
                                    'text' => '推送下级消息',
                                    'callback_data' => 'adminset push'
                                ),
                                array(
                                    'text' => '充值余额',
                                    'callback_data' => 'adminset paymoney'
                                ),
                                array(
                                    'text' => '下级余额',
                                    'callback_data' => 'adminset member 1'
                                )

                            ),
                            array(
                                array(
                                    'text' => '订单管理',
                                    'callback_data' => 'adminset orderlist 1'
                                )


                            ),
                            array(
                                array(
                                    'text' => '关闭',
                                    'callback_data' => 'close'
                                ),
                                array(
                                    'text' => '联系客服',
                                    'callback_data' => 'CUSTOMER_SERVICE2'
                                )

                            )


                        )
                    )
                );

                $this->telegram->sendMessage($chat['id'], $msg, $message_id, $button);
            } else {
                $this->telegram->sendMessage($chat['id'], "暂无这个指令,或者您无权使用");
            }
        }
    }
    public function message($message, $message_id, $from, $chat, $date, $entities = null, $is_topic_message = null)
    {
        if ($chat['type'] == "private") {
            $userinfo = $this->handlename($from);

            if ($message == "个人中心") {
                $user = $this->Member->where(array("userid" => $from['id'], "botid" => $this->bots['botid']))->find();

                $huiyuanodderok = Db::name("order")->where('userid', $from['id'])->where('start', 1)->count();
                
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => 'api开发信息',
                                        'callback_data' => 'Member api'
                                    ),
                                    

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );
                

                //	$this->telegram->sendMessage($chat['id'], $message, $message_id, $buttonr, 'Markdown');

                $ban = isset($user['trxadd']) ? $user['trxadd'] : "未绑定";

                $this->telegram->sendMessage($chat['id'], "个人信息

TG昵称： <strong>{$userinfo['tgnames']}</strong>
TG ID： <code>{$from['id']}</code>

可用余额：<strong>{$user['money']} USDT</strong>

累计下单：<strong>{$huiyuanodderok}</strong> 单
钱包地址：<code>{$ban}</code>", "", $button);
                return true;
            }



            if ($message == "订单管理") {

                $buttonr = json_encode(
                    array(
                        'inline_keyboard' => array(
                            array(
                                array(
                                    'text' => '充值订单',
                                    'callback_data' => 'oererxkkaihesoftk 1 1'
                                ),
                                array(
                                    'text' => '会员订单',
                                    'callback_data' => 'oererxkkaihesoftk 2 1'
                                )
                            )


                        )
                    )
                );
                $this->telegram->sendMessage($chat['id'], "订单管理" . "\r\n" . "点击下方按钮 查询您查询种类", $message_id, $buttonr, 'Markdown');
                return true;
            }

            if ($message == "推送消息") {
                if ($this->bots['botad'] == $from['id']) {
                    $this->telegram->sendMessage($from['id'], "ts+您需要推送的内容！消息 到达机器人所有用户");
                    return true;
                }
            }

            
            if (substr($message, 0, 2) == "ts") {
                if ($this->bots['botad'] == $from['id']) {

                    $pos = strpos($message, 'ts');
                    if ($pos !== false) {
                        $result = substr($message, $pos + 2);
                        $order = uniqid();
                        //查询所有用户信息 给reds
                        $userlist = Db::name('member')->where('botid', $this->bots['botid'])->select();

                        $jobData = array("botkey" => $this->bots["botkey"], "bid" => $this->bots['botid'], "order" => $order, "msg" => $result, 'userlist' => $userlist);

                        $usercount = Db::name('member')->where('botid', $this->bots['botid'])->count();

                        if (empty(Cache::get("tuisinxiaoxic" . $this->bots['botid']))) {

                            Cache::set("tuisinxiaoxi" . $this->bots['botid'], $jobData, 600);
                            //记录 
                            foreach ($userlist as $key => $value) {
                                // code...
                                Db::name('push')->insert(['userid' => $value['userid'], 'botid' => $this->bots['botid'], 'text' => $result, 'botkey' => $this->bots["botkey"]]);
                            }

                            $this->telegram->sendMessage($from['id'], "推送成功" . "本次推送人数:" . $usercount . "请等待任务执行结束！");
                        } else {
                            $this->telegram->sendMessage($from['id'], "距离上次 推送 需要超过半小时");
                        }
                    }
                    return true;
                }
            }
            if ($message == "联系客服" || $message == "客服") {

                $this->telegram->sendMessage($from['id'], $this->bots['kefutelegram']);
                return true;
            }
            if ($message == "余额充值" || $message == "充值") {
                $MemberRecharge = isset($this->bots['MemberRecharge']) ? $this->bots['MemberRecharge'] : "1";
                if ($MemberRecharge == "1") {
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '6u',
                                        'callback_data' => 'paychange 6'
                                    ),
                                    array(
                                        'text' => '10u',
                                        'callback_data' => 'paychange 10'
                                    ),
                                    array(
                                        'text' => '30u',
                                        'callback_data' => 'paychange 30'
                                    ),
                                    array(
                                        'text' => '50u',
                                        'callback_data' => 'paychange 50'
                                    ),
                                    array(
                                        'text' => '100u',
                                        'callback_data' => 'paychange 100'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '200u',
                                        'callback_data' => 'paychange 200'
                                    ),
                                    array(
                                        'text' => '400u',
                                        'callback_data' => 'paychange 400'
                                    ),
                                    array(
                                        'text' => '500u',
                                        'callback_data' => 'paychange 500'
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '《其他金额》',
                                        'callback_data' => 'payzhitypku other'
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->sendMessage($from['id'], "请选择充值金额", $message_id, $button);
                    return true;
                } else {
                    $this->telegram->sendMessage($from['id'], "余额充值 已关闭", $message_id);
                    return true;
                }
            }

            if ($message == "余额") {
                $user = $this->Member->where(array("userid" => $from['id'], "botid" => $this->bots['botid']))->find();
                //$this->db->select ('daikaihuiyuan_user_money',"*",['userid' => $from['id']]);

                // $this->Member->where( array( "userid"=>$from['id'],"botid"=>$this->bots['botid']))->update(['bishu'=>10]);


                $smg = "【userid】" . $from['id'] . "
【账户余额】{$user['money']}U";
                $this->telegram->sendMessage($from['id'], $smg);
                return true;
            }
            if ($message == "赠送朋友会员") {
                $ruhao = isset($userinfo['tghao']) ? $userinfo['tghao'] : $this->bots['kefutelegram'];
                $ruhao = str_replace('_', '\_', $ruhao);
                $msg = "请发送你需要开通/续费会员的Telegram用户名：
1️⃣@开头，如 @{$ruhao}
2️⃣http开头，如 https://t.me/{$ruhao}

批量下单，可一次发送多个用户名，每行一个，或者 以 空格 逗号 间隔：
🔔例如：
@用户名1
@用户名2

🔔或
@用户名1 @用户名2";

                $this->telegram->sendMessage($chat['id'], $msg, "", null, 'Markdown', true);
            }

            





            //价格
            if ($message == "价格") {

                $array = explode(",", $this->bots['pricexiao']);
                if (count($array) != 3) {
                    $this->telegram->sendMessage($from['id'], "管理员价格配置错误了");
                } else {
                    
                    
                    
                    $t1 = $array[0];
                    $t2 = $array[1];
                    $t3 = $array[2];


                    $msg = "会员价格
🔥3个月 / {$t1} U
🔥6个月 / {$t2} U
🔥12个月 / {$t3} U";



                    $this->telegram->sendMessage($from['id'], $msg);
                }
            }


            //联系客服
            if ($message == "购买会员") {

                $ruhao = isset($userinfo['tghao']) ? $userinfo['tghao'] : "@zf789";
                $ruhao = str_replace('_', '\_', $ruhao);


                $this->telegram->sendMessage($chat['id'], "请发送你需要开通/续费会员的Telegram用户名：
1️⃣@开头，如 @{$ruhao}
2️⃣http开头，如 https://t.me/{$ruhao}

批量下单，可一次发送多个用户名，每行一个，或者 以 空格 逗号 间隔：
🔔例如：
@用户名1
@用户名2

🔔或
@用户名1 @用户名2", "", null, 'Markdown', true);

                //$this->telegram->sendMessage ($from['id'],$smg);
                return true;
            }
            // JoinProxy
            //创建3*10*客户1
            

            $message = str_replace("https://t.me/", "@", $message);

            $atCount = substr_count($message, '@');
            if ($atCount == 1) {
                if (preg_match('/^@[a-zA-Z0-9_]+$/', $message)) {

                    //取消号码验证
                    $ym = $this->checktelegramuser(str_replace("@", "", $message));
                    if (!empty($ym[0])) {

                        if ($ym[0] != "") {
                            $txfile = $ym[1];
                            $nicheng = $ym[0];
                            $max_length = 5; // 最大长度
                            if (mb_strlen($nicheng) > $max_length) {
                                $nicheng = mb_substr($nicheng, 0, $max_length) . (mb_strlen($nicheng) > $max_length ? '...' : '');
                            } else {
                                $nicheng = $nicheng;
                            }
                            $usernamr = $message;
                            //正常购买
                            //如果有设定价格 则 使用设定价格

                            $array = explode(",", $this->bots['pricexiao']);
                            if (count($array) != 3) {
                                $this->telegram->sendMessage($from['id'], "管理员价格配置错误了");
                            } else {
                                $t1 = $array[0];
                                $t2 = $array[1];
                                $t3 = $array[2];


                                $button = json_encode(
                                    array(
                                        'inline_keyboard' => array(
                                            array(
                                                array(
                                                    'text' => '3 个月 售价 ' . $t1 . 'U',
                                                    'callback_data' => 'kguehunyu 3 ' . $t1 . ' 0 ' . $usernamr . " " . $nicheng
                                                ),
                                                array(
                                                    'text' => '6 个月 售价 ' . $t2 . 'U',
                                                    'callback_data' => 'kguehunyu 6 ' . $t2 . ' 1 ' . $usernamr . " " . $nicheng
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '12 个月 售价 ' . $t3 . 'U',
                                                    'callback_data' => 'kguehunyu 12 ' . $t3 . ' 2 ' . $usernamr . " " . $nicheng
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '关闭',
                                                    'callback_data' => 'close'
                                                ),
                                                array(
                                                    'text' => '联系客服',
                                                    'callback_data' => 'CUSTOMER_SERVICE'
                                                )

                                            )


                                        )
                                    )
                                );





                                $msg = "开通用户：{$usernamr}
用户昵称：{$nicheng}

<strong>确定为此用户 开通/续费 Telegram Premium会员吗？</strong>";

                                $hui = $this->telegram->sendPhoto($chat['id'], $txfile, $msg, null, $button);
                                if (empty($hui)) {
                                    $this->telegram->sendPhoto($chat['id'], "https://img1.imgtp.com/2023/09/16/7HMoTjjp.png", $msg, null, $button);
                                }
                            }
                        } else {
                            $this->telegram->sendMessage($from['id'], "该账号已订阅 会员！请等待 到期后在开通！请联系客服");
                            return true;
                        }
                    } else {
                        $this->telegram->sendMessage($from['id'], "信息获取失败！请【重新发送】您的与用户名！");
                        return true;
                    }
                }
            }
            if ($atCount >= 2) {
                $msg = "欢迎使用批量开通！已提取开通用户名 请注意核对哟！" . "\r\n" . "<strong>由于开通用户名较多！</strong>" . "\r\n" . "<strong>请注意核对哟！</strong>" . "\r\n" . "提交有效用户名如下" . "\r\n";
                $pattern = '/@(\w+)/';
                preg_match_all($pattern, $message, $matches);

                if (empty($matches[1])) {
                } else {
                    $plorder = "tmtpl" . uniqid();
                    $usernames = $matches[1];
                    // 使用 array_unique() 去重
                    $usernames = array_unique($usernames);
                    foreach ($usernames as $key => $value) {
                        // code...
                        $msg .= "<strong>@" . $value . "</strong>" . "\r\n";
                    }
                    cache::set($this->bots['botid'] . $plorder, $usernames, 1200);
                    $msg .= "一共<strong>" . count($usernames) . "</strong>个";
                }

                $url = "https://i.ibb.co/cvS1GDq/image.png";
                //如果有设定价格 则 使用设定价格


                $array = explode(",", $this->bots['pricexiao']);
                $t1 = $array[0];
                $t2 = $array[1];
                $t3 = $array[2];


                $button = json_encode(
                    array(
                        'inline_keyboard' => array(
                            array(
                                array(
                                    'text' => '3 个月 售价 ' . $t1 . 'U',
                                    'callback_data' => 'pkguehunyu 3 ' . $t1 . ' 0 ' . $plorder
                                ),
                                array(
                                    'text' => '6 个月 售价 ' . $t2 . 'U',
                                    'callback_data' => 'pkguehunyu 6 ' . $t2 . ' 1 ' . $plorder
                                )

                            ),
                            array(
                                array(
                                    'text' => '12 个月 售价 ' . $t3 . 'U',
                                    'callback_data' => 'pkguehunyu 12 ' . $t3 . ' 2 ' . $plorder
                                )

                            ),
                            array(
                                array(
                                    'text' => '关闭',
                                    'callback_data' => 'close'
                                ),
                                array(
                                    'text' => '联系客服',
                                    'callback_data' => 'CUSTOMER_SERVICE'
                                )

                            )


                        )
                    )
                );



                $this->telegram->sendPhoto($chat['id'], $url, $msg, null, $button);
            }
        }
        if ($chat['type'] == "supergroup" || $chat['type'] == "group") {
            preg_match_all('/[\x{4e00}-\x{9fff}-A-Za-z0-9@]+/u', $message, $matches);
            $message2 = strtolower(join('', $matches[0])); //过滤表情包和符号
            if ($message == "id") {
                $this->telegram->sendMessage($chat['id'], 'id:<code>' . $chat['id'] . '</code>');
                return true;
            }
            
            //KeyboardButton

        }
    }

    public function reply_message($text, $message_id, $reply_to_text, $reply_to_message_id, $reply_to_date, $from, $chat, $reply_to_is_bot)
    {
        //var_dump($text,$message_id,$reply_to_text,$reply_to_message_id,$reply_to_date,$from,$chat,$reply_to_is_bot); 
        if ($chat['type'] == "private") {
            if ($reply_to_text == "请回复本条并输入您新的地址！") {
                //判断地址是否合法
                if (preg_match('/^T(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$/', $text)) {

                    $systemadd = Db::name("conf")->where("key", 'system')->value('value');

                    Db::name("admin_bot")->where("botid", $this->bots['botid'])->update(['trxadd' => $text]);
                    $this->telegram->deleteMessage($chat['id'], $message_id);
                    $this->telegram->deleteMessage($chat['id'], $reply_to_message_id);
                    $this->telegram->sendMessage($chat['id'], "您的充值地址绑定成功了！您的地址【<code>" . $text . "</code>】" . "\r\n" . "系统充值地址为[<code>" . $systemadd . "</code>]");
                } else {
                    $this->telegram->sendMessage($chat['id'], "您的地址不合法 请检查地址是否正确", $message_id);
                }
            }
        }
    }

    


    function curl_get($url, $data = [])
    {
        // 处理get数据
        if (!empty($data)) {
            $url = $url . '?' . http_build_query($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //这个是重点。
        // 设置 HTTP 代理
        //curl_setopt($curl, CURLOPT_PROXY, '127.0.0.1:10809');
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    function checktelegramuser($username = "YT9556")
    {
        $url = "https://t.me/" . str_replace('@', '', $username);
        // var_dump($url);
        $teq = $this->curl_get($url);
        //var_dump($teq);

        //preg_match_all ('/<meta property="og:title" content="(.*?)">/', $teq, $pat_array);
        preg_match_all('/<meta property="twitter:(title|image)" content="(.*?)">/', $teq,  $pat_array);

        if (!empty($pat_array[2])) {


            return $pat_array[2];
        } else {
            return [0 => '', 1 => ''];
        }
        return [0 => '', 1 => ''];
    }

    public function edited_message($message, $message_id, $from, $chat, $date, $entities = null) {} //编辑消息
    public function forward_from($message, $message_id, $from, $chat, $forward_from, $date) {}
    public function forward_from_chat($message, $message_id, $from, $chat, $forward_from) {}
    public function forward_fromphoto($message, $message_id, $from, $chat, $date) {}
    public function contact($contact, $message_id, $from, $chat, $date) //联系人
    {}
    public function sticker($sticker, $message_id, $from, $chat, $date) {}
    public function sticker_reply_to_message($message, $message_id, $from, $chat, $date, $sticker) //tiezhi 被回复
    {}

    public function reply_to_message($message, $message_id, $date, $textmessage, $reply_to_message, $xdate, $from, $chat, $is_bot, $is_topic_message = null)
    {
        if ($chat['type'] == "private") {
            $userinfo = $this->Custom->handlename($from);

            if ($textmessage == "请回复本条并输入您要充值的金额、例如：100 （输入纯数字即可、单位默认USDT）") {
                //建立充值
                if (is_numeric($message)) {
                    $paymoney = $this->paymoney($message);
                    $order_no = uniqid();
                    $jzsh = time() + 60 * 15;

                    $lstime = time() - 600;
                    $lsdingdan = Db::name('order')->where(['userid' => $from['id'], "botid" => $this->bots['botid'], 'start' => 0])->where('create_time', '>=', $lstime)->count();

                    if ($lsdingdan >= 5) {
                        //不在支持新订单创建
                        //  $this->telegram->answerCallback($callback_id, "如果您无法支付！请联系客服处理订单哟！", true);
                        $url = "https://img.picui.cn/free/2024/07/10/668e1ed44a8cd.jpg";
                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => '关闭',
                                            'callback_data' => 'close'
                                        ),
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );

                        $msgg = "订单建立失败了！" . "\r\n" . "如果您无法支付！请联系客服哟！" . "\r\n" . "感谢您的支持";
                        $media = array(
                            'type' => 'photo',
                            'media' => $url,
                            'caption' => $msgg,
                            "parse_mode" => 'HTML',
                        );

                        $this->telegram->editMessageMedia($chat['id'], $message_id, NULL, json_encode($media), $button);

                        //  $this->telegram->editMessageCaption($chat['id'], $message_id, null, "订单建立失败了！"//."\r\n"."如果您无法支付！请联系客服哟！"."\r\n"."感谢您的支持");
                        // $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);

                        //  $this->telegram->editMessage($chat['id'], $message_id,  "订单建立失败了！"."\r\n"."如果您无法支付！请联系客服哟！"."\r\n"."感谢您的支持", $button, 'HTML', true);


                    } else {

                        Db::name('order')->insert(['userid' => $from['id'], 'money' => $message, 'paymoney' => $paymoney, 'create_time' => time(), "trxadd" => $this->bots['trxadd'], "botid" => $this->bots['botid'], "nichen" => $userinfo['tgnames'], "name" => "余额充值" . $userinfo['tgnames'], 'start' => 0, 'order_no' => $order_no, 'end_time' => $jzsh, "tu" => 0, 'smessage_id' => $message_id, "mode" => "Daikaihuiyuan", "moth" => "yuepay", 'lirun' => 0]);

                        if ($lsdingdan > 1) {
                            //不在监听 减少资源浪费
                        } else {
                            $job = 'app\bapi\syservice\Getzhandam';
                            $queueName = 'zhandam';
                            $data = array("end_time" => time() + 60 * 20, "trxadd" => $this->bots['trxadd'], 'order_no' => $order_no);
                            // Queue::push($job, $data, $queueName);
                        }
                        $odder = Db::name('order')->where("order_no", $order_no)->find();

                        


                        $xzssss = date("m-d H:i:s");
                        $msg = "订单号：{$odder['order_no']}
商品名称：<strong>{$odder['name']}</strong>
订单金额：<strong>{$odder['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";

                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => 'USDT支付',
                                            'callback_data' => 'payodderkkkssskkk payue ' . $order_no
                                        )
                                    ),
                                    array(
                                        array(
                                            'text' => '刷新',
                                            'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                        ),
                                        array(
                                            'text' => '取消订单',
                                            'callback_data' => 'checkodder ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );
                        $this->telegram->sendMessage($chat['id'], $msg, "", $button, 'HTML', true);
                    }
                } else {

                    $this->telegram->sendMessage($chat['id'], "机器人不能理解这个金额！ 请输入数字哟！ 祝你每天开心");
                }
            }
            //  var_dump($message,$reply_to_message,$textmessage);



        }
    }
    public function photo($photo, $caption, $message_id, $from, $chat, $date, $entities = null) {}

    public function video($video, $caption, $message_id, $from, $chat, $date, $entities = null) {}
    public function document($photo, $caption, $message_id, $from, $chat, $date) {}
    //callback_inline
    public function callback_inline($callback_data, $callback_id, $callback_from) {}


    public function callback_query($callback_data, $callback_id, $callback_from, $message_id, $from, $chat, $date)
    {
        if ($chat['type'] == "private") {
            $callbackExplode = explode(' ', $callback_data);

            $tou = $callbackExplode['0'];

            $userinfo = $this->handlename($callback_from);
            //adminset
            if ($tou == "adminset") {
                $type = $callbackExplode['1'];
                //  $this->telegram->answerCallback($callback_id, $type, true);
                if ($type == "price") {

                    $array = explode(",", $this->bots['pricexiao']);
                    $t1 = isset($array[0]) ? $array[0] : "15";
                    $t2 = isset($array[1]) ? $array[1] : "20";
                    $t3 = isset($array[2]) ? $array[2] : "35";

                    $msg = "目前销售价格" . "\r\n" . "三个月会员: <strong>" . $t1 . "</strong>u" . "\r\n" . "六个月会员: <strong>" . $t2 . "</strong>u" . "\r\n" . "一  年  会员: <strong>" . $t3 . "</strong>u" . "\r\n" . "您的销售价格 必须高于成本价格 " . "\r\n" . "关于成本价格 请咨询客服";
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '设置3个月价格',
                                        'callback_data' => 'adminprice 3'
                                    ),
                                    array(
                                        'text' => '设置6个月价格',
                                        'callback_data' => 'adminprice 6'
                                    ),
                                    array(
                                        'text' => '设置12个月价格',
                                        'callback_data' => 'adminprice 12'
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '刷新配置',
                                        'callback_data' => 'adminset price'
                                    ),
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
                if ($type == "api") {
                    //user_token
                    $Member  = $this->Member->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->find();
                    if (empty($Member['token'])) {
                        $apitoken = md5(time());
                        //更新到配置
                        $this->Member->where("botid", $this->bots['botid'])->where('userid', $callback_from['id'])->update(['token' => $apitoken]);
                    } else {
                        $apitoken = $Member['token'];
                    }
                    // $user = $this->Member->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->find();

                    $msg = "您的api信息" . "\r\n" . "uid:<code>" . $Member['id'] . "</code>" . "\r\n" . "apikey:<code>" . $apitoken . "</code>" . "\r\n" . "余额:<strong>" . $Member['money'] . "</strong>";

                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '对接文档',
                                        'url' => $this->bots['apiurl'].'/api.html'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );

                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
                if ($type == "panel") {
                    $zongDingDan = Db::name("order")->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->count();
                    $successDan = Db::name("order")->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid'], "start" => 1))->count();
                    $successmoney = Db::name("order")->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid'], "start" => 1))->sum("money");
                    $premium = Db::name("order")->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid'], "moth" => 'huiyuangoumaai', "start" => 1))->sum("money");
                    $paymoney = Db::name("order")->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid'], "moth" => 'yuepay', "start" => 1))->sum("money");

                    $usermoneycount = $this->Member->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->sum("money");
                    $msg = "销售面板" . "\r\n" . "代充余额:<strong>" . $this->bots['money'] . "</strong>" . "\r\n" . "总订单:<strong>" . $zongDingDan . "单</strong>" . "\r\n" . "成功订单:<strong>" . $successDan . "单</strong>" . "\r\n" . "成功金额:<strong>" . $successmoney . "U 会员(" . $premium . ")+充值(" . $paymoney . ")</strong>" . "\r\n" . "利润金额:<strong>50单</strong>" . "\r\n" . "会员余额:<strong>" . $usermoneycount . "U</strong>";
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '设置价格',
                                        'callback_data' => 'adminset price'
                                    ),
                                    array(
                                        'text' => '代充绑定',
                                        'callback_data' => 'adminset trxadd'
                                    ),
                                    array(
                                        'text' => 'api信息',
                                        'callback_data' => 'adminset api'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '推送下级消息',
                                        'callback_data' => 'adminset push'
                                    ),
                                    array(
                                        'text' => '下级余额',
                                        'callback_data' => 'adminset member 1'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '订单管理',
                                        'callback_data' => 'adminset orderlist 1'
                                    )


                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE2'
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
                if ($type == "member") {
                    $page = isset($callbackExplode[2]) ? $callbackExplode[2] : 1;
                    if ($page < 1) {
                        $this->telegram->answerCallback($callback_id, "已经是第一页了", true);
                        return;
                    }
                    $member = Db::name("member")->where('botid', $this->bots['botid'])->page($page, 10)->select();
                    $membertxt = "昵称|余额" . "\r\n";

                    foreach ($member as $key => $value) {
                        // $tim=date('m-d H-i-s',$value['time']);
                        $membertxt .= '🎭' . $value['nickname'] . " 💡" . $value['money'] . "\r\n"; // code...
                    }
                    // $this->telegram->sendMessage($chat['id'], $ordertxt);
                    $xpage = $page + 1;
                    $spage = $page - 1;
                    $buttonr = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '上一页',
                                        'callback_data' => 'adminset member ' . $spage
                                    ),
                                    array(
                                        'text' => '下一页',
                                        'callback_data' => 'adminset member ' . $xpage
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->editMessage($chat['id'], $message_id, $membertxt, $buttonr);
                }
                if ($type == "trxadd") {
                    $msg = "绑定您的 充值地址！" . "\r\n" . "绑定后你的转账 会自动充值到您的余额上！" . "\r\n" . "你的地址为:【<code>" . $this->bots['trxadd'] . "</code>】";

                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '修改新地址',
                                        'callback_data' => 'adminset xiugaitrxadd'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );



                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                    // $this->telegram->forcereply($chat['id'], "请回复本条并输入您要充值的金额、例如：100 （输入纯数字即可、单位默认USDT）", $message_id);
                }
                if ($type == "xiugaitrxadd") {
                    $this->telegram->forcereply($chat['id'], "请回复本条并输入您新的地址！", $message_id);
                }
                if ($type == "paymoney") {
                    $systemadd = Db::name("conf")->where("key", 'system')->value('value');
                    $res = $this->Customer->CreateQRcode($systemadd, "大于5u"); //$trxadd,$money
                    //var_dump($res);
                    // $this->telegram->sendMessage($chat['id'], json_encode($res));
                    $isid = $this->telegram->sendDocumentphoto($chat['id'], $res, "地址：<code>{$systemadd}</code>" . "\r\n" . "系统地址大于5u 转账多少 上分多少");
                }
                if ($type == "push") {
                    //写个自动推送消息
                    $this->telegram->sendMessage($chat['id'], "给您的所有下级推送消息！提供 ts+您需要推送的内容" . "\r\n" . "命令示例:ts宝宝们！我们开始营业了哟！" . "\r\n" . "用户端收到消息:宝宝们！我们开始营业了哟！");
                }
                /*订单列表*/

                if ($type == "orderlist") {
                    $page = isset($callbackExplode[2]) ? $callbackExplode[2] : 1;
                    if ($page < 1) {
                        $this->telegram->answerCallback($callback_id, "已经是第一页了", true);
                        return;
                    }
                    $member = Db::name("order")->where('botid', $this->bots['botid'])->order('id', 'desc')->page($page, 10)->select();
                    $membertxt = "昵称|余额" . "\r\n";
                    $fujiabotton = array();
                    foreach ($member as $key => $value) {
                        $tim = date('m-d H-i-s');
                        $start = $value['start'] == 1 ? '✅' : "❌";
                        if ($value['start'] != 1) {
                            $fujiabotton[] = array(
                                'text' => $value['id'],
                                'callback_data' => 'adminset orderinfo ' . $value['id']
                            );
                        }
                        $membertxt .= "[" . $value['id'] . "] " . " " . $start . '🎭' . $value['nichen'] . " 💡" . $value['paymoney'] . " ⏱️" . $value['tancan'] . "\r\n"; // code...
                    }

                    // $this->telegram->sendMessage($chat['id'], $ordertxt);
                    $xpage = $page + 1;
                    $spage = $page - 1;
                    $inline_keyboard = array(
                        array(
                            array(
                                'text' => '上一页',
                                'callback_data' => 'adminset orderlist ' . $spage
                            ),
                            array(
                                'text' => '下一页',
                                'callback_data' => 'adminset orderlist ' . $xpage
                            )

                        )
                    );

                    $inline_keyboard[] = $fujiabotton;

                    $inline_keyboard[] = array(
                        array(
                            'text' => '返回主面板',
                            'callback_data' => 'adminset panel'
                        ),
                        array(
                            'text' => '❎关闭',
                            'callback_data' => 'close'
                        )
                    );


                    $button = json_encode(
                        array(
                            'inline_keyboard' => $inline_keyboard
                        )
                    );

                    $this->telegram->editMessage($chat['id'], $message_id, $membertxt, $button);
                }
                /*orderinfo 详细信息*/
                if ($type == "orderinfo") {
                    $orderid = isset($callbackExplode[2]) ? $callbackExplode[2] : 1;
                    $orderinfo = Db::name("order")->where(array("id" => $orderid))->find();
                    $start = $orderinfo['start'] == 1 ? '✅' : "❌";
                    $create_time = date('m-d H:i:s', $orderinfo['create_time']);
                    $end_time = date('m-d H:i:s', $orderinfo['end_time']);

                    $msg = " 订单详情 " . "\r\n" . "ID:<code>" . $orderinfo['id'] . "</code>" . "\r\n" . "单号:<code>" . $orderinfo['order_no'] . "</code>" . "\r\n" . "状态:" . $start . "\r\n" . "标题:<strong>" . $orderinfo['name'] . "</strong>" . "\r\n" . "收款地址:<code>" . $orderinfo['trxadd'] . "</code>" . "\r\n" . "实付金额:<code>" . $orderinfo['paymoney'] . "</code>" . "\r\n" . "开始时间:<code>" . $create_time . "</code>" . "\r\n" . "结束时间:<code>" . $end_time . "</code>";
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '✅确认支付订单',
                                        'callback_data' => 'adminset orderok ' . $orderid
                                    ),

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                )


                            )
                        )
                    );


                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
                if ($type == "orderok") {
                    $orderid = isset($callbackExplode[2]) ? $callbackExplode[2] : 1;
                    $orderinfo = Db::name("order")->where(array("id" => $orderid))->find();
                    if ($orderinfo['start'] == 1) {
                        $this->telegram->answerCallback($callback_id, "订单状态是成功的哟", true);
                        return;
                    }
                    //确认成功订单
                    Db::name("order")->where(array("id" => $orderid))->update(['start' => 1]);
                    //订单成功处理
                    if ($orderinfo['moth'] == 'yuepay') {
                        //余额充值 调用增加余额
                        $req = $this->Customer->ChangeMoney($orderinfo['userid'], 'member', "+", $orderinfo['money'], $orderinfo['botid'], "充值");
                    }


                    $orderinfo = Db::name("order")->where(array("id" => $orderid))->find();
                    $start = $orderinfo['start'] == 1 ? '✅' : "❌";
                    $create_time = date('m-d H:i:s', $orderinfo['create_time']);
                    $end_time = date('m-d H:i:s', $orderinfo['end_time']);

                    $msg = " 订单详情 " . "\r\n" . "ID:<code>" . $orderinfo['id'] . "</code>" . "\r\n" . "单号:<code>" . $orderinfo['order_no'] . "</code>" . "\r\n" . "状态:" . $start . "\r\n" . "标题:<strong>" . $orderinfo['name'] . "</strong>" . "\r\n" . "收款地址:<code>" . $orderinfo['trxadd'] . "</code>" . "\r\n" . "实付金额:<code>" . $orderinfo['paymoney'] . "</code>" . "\r\n" . "开始时间:<code>" . $create_time . "</code>" . "\r\n" . "结束时间:<code>" . $end_time . "</code>";
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '✅确认支付订单',
                                        'callback_data' => 'adminset orderok ' . $orderid
                                    ),

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '返回主面板',
                                        'callback_data' => 'adminset panel'
                                    )

                                )


                            )
                        )
                    );

                    $this->telegram->answerCallback($callback_id, "修改成功", true);
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
            }


            if ($tou == "adminprice") {
                $type = $callbackExplode['1'];
                $money = isset($callbackExplode['2']) ? (float)$callbackExplode['2'] : 0;
                $array = explode(",", $this->bots['pricexiao']);
                if ($type == 3) {
                    $n = 0;
                }
                if ($type == 6) {
                    $n = 1;
                }
                if ($type == 13) {
                    $n = 2;
                }
                $price = $array[$n];

                // var_dump($money,$type);

                if ($money == 0) {
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '加 1 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "+1"
                                    ),
                                    array(
                                        'text' => '减 1 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "-1"
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '加 0.5 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "+0.5"
                                    ),
                                    array(
                                        'text' => '减 0.5 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "-0.5"
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '返回价格设置',
                                        'callback_data' => 'adminset price'
                                    )

                                )


                            )
                        )
                    );
                    $msg = "开设设置会员销售价格" . "\r\n" . "设置【<strong>" . $type . "</strong>】个月价格【<strong>" . $price . "</strong>】";
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                } else {
                    $newprice = $price + $money;
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '加 1 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "+1"
                                    ),
                                    array(
                                        'text' => '减 1 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "-1"
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '加 0.5 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "+0.5"
                                    ),
                                    array(
                                        'text' => '减 0.5 U',
                                        'callback_data' => 'adminprice ' . $type . ' ' . "-0.5"
                                    )
                                ),
                                array(
                                    array(
                                        'text' => '返回价格设置',
                                        'callback_data' => 'adminset price'
                                    )

                                )


                            )
                        )
                    );
                    $msg = "开设设置会员销售价格" . "\r\n" . "设置【<strong>" . $type . "</strong>】个月价格【<strong>" . $newprice . "</strong>】";
                    //更新到配置 更具n 生成数据
                    $array[$n] = $newprice;
                    $str = implode(',', $array);
                    Db::name("admin_bot")->where("botid", $this->bots['botid'])->update(['pricexiao' => $str]);
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
            }
            if($tou =="Member"){
                $type = $callbackExplode['1'];
                if ($type == "api") {
                    //user_token
                    $Member  = $this->Member->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->find();
                    if (empty($Member['token'])) {
                        $apitoken = md5(time());
                        //更新到配置
                        $this->Member->where("botid", $this->bots['botid'])->where('userid', $callback_from['id'])->update(['token' => $apitoken]);
                    } else {
                        $apitoken = $Member['token'];
                    }
                    // $user = $this->Member->where(array("userid" => $callback_from['id'], "botid" => $this->bots['botid']))->find();

                    $msg = "您的api信息" . "\r\n" . "uid:<code>" . $Member['id'] . "</code>" . "\r\n" . "apikey:<code>" . $apitoken . "</code>" . "\r\n" . "余额:<strong>" . $Member['money'] . "</strong>";

                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '对接文档',
                                        'url' => $this->bots['apiurl'].'/api.html'
                                    )
                                    

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    ),
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );

                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button);
                }
            }

            if ($tou == "goumaisangeyuehunyu") {
                $yue = $callbackExplode['1'];
                $money = $callbackExplode['2'];
                $tcid = $callbackExplode['3'];
                $msg = "注意：账号一定要保证正确！不要输入手机号以及用户名，复制id【带@的那个】发送即可，会员到账之前不要更改id，否则损失自行承担
请输入要充值的账号,多个账号之间用空格隔开，比如
 xxx xxx";
                Cache::set("goumaitaocan" . $callback_from['id'], $yue . "_" . $money . "_" . $tcid, 600);
                $this->telegram->sendMessage($chat['id'], $msg);
            }





            if ($tou == "oererxkkaihesoftk") {
                $start = $callbackExplode['1'];
                $page = isset($callbackExplode['2']) ? $callbackExplode['2'] : 1;
                if ($page < 1) {
                    $this->telegram->answerCallback($callback_id, "已经是第一页了", true);
                    return;
                }

                $moth = $start == '1' ? "yuepay" : "huiyuangoumaai";

                $odder = Db::name("order")->where('userid', $callback_from['id'])->where('moth', $moth)->page($page, 10)->select();
                $ordertxt = "金额|项目|状态|时间" . "\r\n";
                foreach ($odder as $key => $value) {
                    $tim = date('m-d H-i-s');
                    $start = $value['start'] == 0 ? "未支付" : "ok";
                    $ordertxt .= '💰' . $value['paymoney'] . " 💡" . $value['name'] . " ⏳" . $start . " ⏱️" . $tim . "\r\n"; // code...
                }
                // $this->telegram->sendMessage($chat['id'], $ordertxt);
                $xpage = $page + 1;
                $spage = $page - 1;
                $buttonr = json_encode(
                    array(
                        'inline_keyboard' => array(
                            array(
                                array(
                                    'text' => '上一页',
                                    'callback_data' => 'oererxkkaihesoftk ' . $start . ' ' . $spage
                                ),
                                array(
                                    'text' => '下一页',
                                    'callback_data' => 'oererxkkaihesoftk ' . $start . ' ' . $xpage
                                )
                            ),array(
                                array(
                                    'text' => '关闭',
                                    'callback_data' => 'close' 
                                ),
                                array(
                                    'text' => '联系客服',
                                    'callback_data' => 'CUSTOMER_SERVICE'
                                )
                            )


                        )
                    )
                );
                $this->telegram->editMessage($chat['id'], $message_id, $ordertxt, $buttonr);
            }





            



           




            if ($tou == "payzhitypku") {
                $id = $callbackExplode[1];

                if ($id == "other") {
                    //  $this->MemcacheModel->set("goumaitaocankkkk".$callback_from['id'],"ok",600);

                    // $smg="请输入数字，充值必须大于1U";
                    $this->telegram->forcereply($chat['id'], "请回复本条并输入您要充值的金额、例如：100 （输入纯数字即可、单位默认USDT）", $message_id);
                }
            }
            if ($tou == "paychange") {
                $money = $callbackExplode[1];

                if (is_numeric($money)) {


                    $lstime = time() - 600;
                    $lsdingdan = Db::name('order')->where(['userid' => $callback_from['id'], "botid" => $this->bots['botid'], 'start' => 0])->where('create_time', '>=', $lstime)->count();

                    if ($lsdingdan >= 5) {
                        //不在支持新订单创建
                        //  $this->telegram->answerCallback($callback_id, "如果您无法支付！请联系客服处理订单哟！", true);
                        $url = "https://img.picui.cn/free/2024/07/10/668e1ed44a8cd.jpg";
                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );

                        $msgg = "订单建立失败了！" . "\r\n" . "如果您无法支付！请联系客服哟！" . "\r\n" . "感谢您的支持";
                        $media = array(
                            'type' => 'photo',
                            'media' => $url,
                            'caption' => $msgg,
                            "parse_mode" => 'HTML',
                        );

                        $this->telegram->editMessageMedia($chat['id'], $message_id, NULL, json_encode($media), $button);
                    } else {

                        $paymoney = $this->paymoney($money);
                        $order_no = uniqid();
                        $jzsh = time() + 60 * 15;

                        Db::name('order')->insert(['userid' => $callback_from['id'], 'money' => $money, 'paymoney' => $paymoney, 'create_time' => time(), "trxadd" => $this->bots['trxadd'], "botid" => $this->bots['botid'], "nichen" => $userinfo['tgnames'], "name" => "余额充值" . $userinfo['tgnames'], 'start' => 0, 'order_no' => $order_no, 'end_time' => $jzsh, 'smessage_id' => $message_id, "mode" => "Daikaihuiyuan", "moth" => "yuepay", 'lirun' => 0]);

                        if ($lsdingdan > 1) {
                            //不在监听 减少资源浪费
                        } else {
                            $job = 'app\bapi\syservice\Getzhandam';
                            $queueName = 'zhandam';
                            $data = array("end_time" => time() + 60 * 20, "trxadd" => $this->bots['trxadd'], 'order_no' => $order_no);
                            // Queue::push($job, $data, $queueName);
                        }
                        $odder = Db::name('order')->where("order_no", $order_no)->find();



                        $xzssss = date("m-d H:i:s");
                        $msg = "订单号：{$odder['order_no']}
商品名称：<strong>{$odder['name']}</strong>
订单金额：<strong>{$odder['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";

                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => 'USDT支付',
                                            'callback_data' => 'payodderkkkssskkk payue ' . $order_no
                                        )
                                    ),
                                    array(
                                        array(
                                            'text' => '刷新',
                                            'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                        ),
                                        array(
                                            'text' => '取消订单',
                                            'callback_data' => 'checkodder ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );
                        $this->telegram->sendMessage($chat['id'], $msg, "", $button, 'HTML', true);
                    }
                }

                // $this->telegram->deleteMessage($chat['id'], $message_id); 
                return true;
            }


           


           
            if ($tou == "kguehunyu") {
                //'kgoumaisangeyuehunyu 3 '.$t1.' 0 ' .$usernamr
                $money = $callbackExplode[2];
                $usernamr = $callbackExplode[4];
                $yue = $callbackExplode[1];
                $nichengyi = $callbackExplode[5];
                $paymoney = $this->paymoney($money);
                $jzsh = time() + 60 * 15;
                $order_no = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
                $lstime = time() - 600;
                $lsdingdan = Db::name('order')->where(['userid' => $callback_from['id'], "botid" => $this->bots['botid'], 'start' => 0])->where('create_time', '>=', $lstime)->count();

                //var_dump(",,,,,,",$lsdingdan);exit();

                if ($lsdingdan >= 5) {
                    //不在支持新订单创建
                    //  $this->telegram->answerCallback($callback_id, "如果您无法支付！请联系客服处理订单哟！", true);
                    $url = "https://img.picui.cn/free/2024/07/10/668e1ed44a8cd.jpg";
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );

                    $msgg = "订单建立失败了！" . "\r\n" . "如果您无法支付！请联系客服哟！" . "\r\n" . "感谢您的支持";
                    $media = array(
                        'type' => 'photo',
                        'media' => $url,
                        'caption' => $msgg,
                        "parse_mode" => 'HTML',
                    );

                    $this->telegram->editMessageMedia($chat['id'], $message_id, NULL, json_encode($media), $button);

                    //  $this->telegram->editMessageCaption($chat['id'], $message_id, null, "订单建立失败了！"//."\r\n"."如果您无法支付！请联系客服哟！"."\r\n"."感谢您的支持");
                    // $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);

                    //  $this->telegram->editMessage($chat['id'], $message_id,  "订单建立失败了！"."\r\n"."如果您无法支付！请联系客服哟！"."\r\n"."感谢您的支持", $button, 'HTML', true);


                } else {
                    //查询订单 并要支付 更新统一的支付方式
                    $pricelist= $this->Customer->Get_shoujia($this->bots,$yue);
                    $lirun = $pricelist['botlirun'];
                    
                    Db::name('order')->insert(['userid' => $callback_from['id'], 'money' => $money, 'paymoney' => $paymoney, 'username' => $usernamr, 'create_time' => time(), "trxadd" => $this->bots['trxadd'], "botid" => $this->bots['botid'], "nichen" => $nichengyi, "name" => "会员购买" . $usernamr, 'tancan' => $yue, 'start' => 0, 'order_no' => $order_no, 'end_time' => $jzsh, 'smessage_id' => $message_id, "mode" => "Daikaihuiyuan", "moth" => "huiyuangoumaai", 'lirun' => $lirun, 'slirun' =>  $pricelist['ptlirun']]);
                    $user = Db::name('order')->where("order_no", $order_no)->find();

                    $xzssss = date("m-d H:i:s");
                    if ($user) {
                        $msg = "订单号：{$user['order_no']}
商品名称：<strong>{$user['tancan']}个月Telegram Premium会员</strong>
开通用户：{$user['username']}
用户昵称：<strong>{$user['nichen']}</strong>

订单金额：<strong>{$user['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";
                    }

                    if ($this->bots['botad'] == $callback_from['id']) {

                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => '👤管理员直接开通',
                                            'callback_data' => 'payodderkkkssskkk admin ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '刷新',
                                            'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                        ),
                                        array(
                                            'text' => '取消订单',
                                            'callback_data' => 'checkodder ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );
                    } else {
                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => 'USDT支付',
                                            'callback_data' => 'payodderkkkssskkk usdt ' . $order_no
                                        ),
                                        array(
                                            'text' => '余额支付',
                                            'callback_data' => 'payodderkkkssskkk yue ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '刷新',
                                            'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                        ),
                                        array(
                                            'text' => '取消订单',
                                            'callback_data' => 'checkodder ' . $order_no
                                        )

                                    ),
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );
                    }





                    $this->telegram->editMessageCaption($chat['id'], $message_id, null, $msg);
                    $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);
                }
            }

            if ($tou == "close") {

                $this->telegram->deleteMessage($chat['id'], $message_id);
                return true;
            }

            if ($tou == "CUSTOMER_SERVICE") {

                $this->telegram->sendMessage($chat['id'], $this->bots['kefutelegram']);
                return true;
            }
            if ($tou == "CUSTOMER_SERVICE2") {
                $SERVICE2 = Db::name("conf")->where("key", 'syskefu')->value('value');
                $this->telegram->sendMessage($chat['id'], $SERVICE2);
                return true;
            }

            if ($tou == "payodderkkkssskkk") {
                //'kgoumaisangeyuehunyu 3 '.$t1.' 0 ' .$usernamr
                $moth = $callbackExplode[1];
                $order_no = $callbackExplode[2];

                if ($moth == "yue") {
                    $odder = Db::name('order')->where("order_no", $order_no)->find();
                    $user = $this->Member->where(["userid" => (int) $odder['userid'], "botid" => $this->bots['botid']])->find();
                    $xinmoney = $user['money'] - $odder['money'];
                    if ($xinmoney >= 0) {
                        if ($odder['moth'] == "huiyuangoumaai") { //会员手动确认 提交

                            $tmuid = Db::name("conf")->where("key", 'tmuid')->value('value');
                            $tmkey = Db::name("conf")->where("key", 'tmkey')->value('value');
                            $cycleMap = ['3' => '0', '6' => '1', '12' => '2'];
                            $mappedValue = $cycleMap[$odder['tancan']] ?? '3';

                            $req = $this->Customer->ChangeMoney($odder['userid'], 'member', "-", $odder['money'], $odder['botid'], "消费" . $odder['username']);
                            
                            
                            
                            
                            if ($req['code'] == true) {
                                $reqx = $this->Customer->submitkt($tmuid, $tmkey,$this->bots['botid'],$odder['money'], $odder['username'], $mappedValue, $this->bots['apiurl']);
                                //var_dump($reqx);
                                if ($reqx['code'] == 0) {
                                    Db::name('order')->where("order_no", $order_no)->update(['start' => 1]);
                                    
                                    $this->telegram->sendMessage($this->bots['botad'], $reqx['msg'] . "\r\n" . "单号:<code>" . $order_no . "</code>" . "\r\n" . "用户名:<code>" . $odder['username'] . "</code>" . "\r\n" . "开通时间:<code>" . $odder['tancan'] . "</code>" . "\r\n" . "消费金额:<code>" . $odder['money'] . "</code>" . "\r\n" . "处理方法:充值余额后手动提交给机器人代替客户开通");
                                    //return true;
                                    $this->telegram->sendMessage($odder['userid'], "订单提交成功！请联系客服,尽快处理！单号:<code>" . $order_no . "</code>");
                                } else {
                                    $this->telegram->sendMessage($odder['userid'], $reqx['msg']);
                                    //return true;
                                }
                            } else {
                                $this->telegram->sendMessage($this->bots['botad'], $req['msg']);
                                // return true;
                            }
                        }
                        if ($odder['moth'] == "pkaihuiy") {
                            //防止多次点击 
                            if(!cache::get($odder['moth'].$order_no)){
                               $tmuid = Db::name("conf")->where("key", 'tmuid')->value('value');
                            $tmkey = Db::name("conf")->where("key", 'tmkey')->value('value');
                            $cycleMap = ['3' => '0', '6' => '1', '12' => '2'];
                            $mappedValue = $cycleMap[$odder['tancan']] ?? '3';
                            $req = $this->Customer->ChangeMoney($odder['userid'], 'member', "-", $odder['money'], $odder['botid'], "消费" . $odder['username']);
                            cache::set($odder['moth'].$order_no,$odder['userid'],5);
                            if ($req['code'] == true) {
                                
                                $dakai = json_decode($odder['username'], true);
                             foreach ($dakai as $key => $value) {
                                 $reqx = $this->Customer->submitkt($tmuid, $tmkey,$this->bots['botid'],$odder['danprice'], "@" . $value, $mappedValue, $this->bots['apiurl']);
                                if ($reqx['code'] == 0) {
                                    $this->telegram->sendMessage($this->bots['botad'], $reqx['msg'] . "\r\n" . "单号:<code>" . $order_no . "</code>" . "\r\n" . "用户名:<code>" . $odder['username'] . "</code>" . "\r\n" . "开通时间:<code>" . $odder['tancan'] . "</code>" . "\r\n" . "消费金额:<code>" . $odder['money'] . "</code>" . "\r\n" . "处理方法:充值余额后手动提交给机器人代替客户开通");
                                    //return true;
                                    $this->telegram->sendMessage($odder['userid'], "订单提交成功！请联系客服,尽快处理！单号:<code>" . $order_no . "</code>");
                                } else {
                                    $this->telegram->sendMessage($odder['userid'], $reqx['msg']);
                                    //return true;
                                }
                             }
                                
                                
                                
                            } else {
                                $this->telegram->sendMessage($this->bots['botad'], $req['msg']);
                                // return true;
                            } 
                            }else{
                                $this->telegram->answerCallback($callback_id, "消息已处理", true); 
                            }
                            
                            
                        }
                            if(!empty($odder['smessage_id'])){
                                $this->telegram->deleteMessage($chat['id'], $odder['smessage_id']); 
                            }
                            if(!empty($odder['message_id'])){
                                $this->telegram->deleteMessage($chat['id'], $odder['smessage_id']); 
                            }
                        //  Db::name('order')->where(['order_no' => $order_no])->update(['start' => 1, "remakess" => "余额支付"]);

                        $this->telegram->deleteMessage($chat['id'], $message_id);
                    } else {

                        $msg = "请联系客服 索要地址 充值余额！" . "\r\n" .
                            "您的ID:<code>" . $callback_from['id'] . "</code>" . "\r\n" . "把id 加速充值速度 或者发送充值 进行转账充值";
                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => '关闭',
                                            'callback_data' => 'close'
                                        ),
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );
                        $this->telegram->answerCallback($callback_id, "余额不足以支持本次消费", true);
                        $this->telegram->sendMessage($chat['id'], $msg, "", $button, 'HTML', true);
                    }







                    return true;
                } elseif ($moth == "usdt") {


                    $odder = Db::name('order')->where("order_no", $order_no)->find();

                    $xzssss = date("m-d H:i:s");
                    $msg = "订单号：{$order_no}
商品名称：<strong>{$odder['tancan']}个月Telegram Premium会员</strong>
开通用户：{$odder['username']}
用户昵称：<strong>{$odder['nichen']}</strong>

订单金额：<strong>{$odder['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";

                    //  $jzsj=$user[0]['username']+create_time
                    $sysss = (int) $odder['end_time'] - time();
                    if ($sysss <= 0) {
                        // 订单已超时 
                        $this->telegram->answerCallback($callback_id, "订单已超时!请重新发起", true);
                        $this->telegram->deleteMessage($chat['id'], $message_id);
                        if (isset($odder['message_id'])) {
                            $this->telegram->deleteMessage($chat['id'], $odder['message_id']);
                        }
                    } else {

                        $order_deadline = $odder['end_time']; // 订单截止时间
                        $current_time = time(); // 当前时间

                        $remaining_time = $order_deadline - $current_time; // 剩余时间（单位：秒）

                        // 将剩余时间转换为天、小时、分钟和秒
                        $days = floor($remaining_time / (60 * 60 * 24));
                        $hours = floor(($remaining_time % (60 * 60 * 24)) / (60 * 60));
                        $minutes = floor(($remaining_time % (60 * 60)) / 60);
                        $seconds = $remaining_time % 60;



                        $cchu = "剩余时间:{$hours}小时{$minutes}分钟{$seconds}秒";
                    }
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '截至时间:' . date("m-d H:i:s", $odder['end_time']),
                                        'callback_data' => 'closeww'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => $cchu,
                                        'callback_data' => 'closeww'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    ),
                                    array(
                                        'text' => '取消订单',
                                        'callback_data' => 'checkodder ' . $order_no
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->editMessageCaption($chat['id'], $message_id, null, $msg);
                    $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);
                    //创建支付图片
                    $res = $this->Customer->CreateQRcode($odder['trxadd'], $odder['paymoney']); //$trxadd,$money
                    //var_dump($res);
                    // $this->telegram->sendMessage($chat['id'], json_encode($res));
                    $isid = $this->telegram->sendDocumentphoto($chat['id'], $res, "订单号：{$order_no}
商品名称：<strong>{$odder['tancan']}个月Telegram Premium会员</strong>
开通用户：{$odder['username']}
用户昵称：<strong>{$odder['nichen']}</strong>

支付方式：USDT-TRC20
过期时间：" . date("Y-m-d H:i:s", $odder['end_time']) . "

请使用支持USDT-TRC20的App扫码
或手动转账
下方地址和金额都支持点击复制

转账金额：{$odder['paymoney']} USDT
转账地址：<code>{$odder['trxadd']}</code>

🔴请务必按照上方【转账金额】支付，否则您错误支付的金额将无法找回！！！", $message_id);


                    // $this->db->update('order',['message_id' => $isid],['order_no' => $order_no]);  

                    Db::name('order')->where("order_no", $order_no)->update(['message_id' => $isid]);
                } elseif ($moth == "re") {
                    //$user=  $this->db->select ('order',"*",['order_no' => $order_no]);


                    $odder = Db::name('order')->where("order_no", $order_no)->find();


                    if ($odder['start'] == 0 and $odder['end_time'] >= time()) {
                        $this->telegram->answerCallback($callback_id, "未支付 还在支付时间内 等待支付", true);
                    }
                    if ($odder['start'] == 1) {
                        $this->telegram->answerCallback($callback_id, "订单已支付", true);
                        $this->telegram->deleteMessage($chat['id'], $message_id);
                        if (isset($odder['message_id'])) {
                            $this->telegram->deleteMessage($chat['id'], $odder['message_id']);
                        }
                    }

                    if ($odder['start'] == 0 and $odder['end_time'] <= time()) {
                        $this->telegram->deleteMessage($chat['id'], $message_id);
                        if (isset($odder['message_id'])) {
                            $this->telegram->deleteMessage($chat['id'], $odder['message_id']);
                        }
                        $this->telegram->answerCallback($callback_id, "订单支付超时！", true);
                    }
                }  elseif ($moth == "payue") {

                    //  $user=  $this->db->select ('order',"*",['order_no' => $order_no]);

                    $odder = Db::name('order')->where("order_no", $order_no)->find();

                    $xzssss = date("m-d H:i:s");
                    $msg = "订单号：{$odder['order_no']}
商品名称：<strong>{$odder['name']}</strong>
订单金额：<strong>{$odder['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";


                    //  $jzsj=$user[0]['username']+create_time
                    $sysss = (int) $odder['end_time'] - time();
                    if ($sysss <= 0) {
                        // 订单已超时 
                        $this->telegram->answerCallback($callback_id, "订单已超时!请重新发起", true);
                        $this->telegram->deleteMessage($chat['id'], $message_id);
                        if (isset($odder['message_id'])) {
                            $this->telegram->deleteMessage($chat['id'], $odder['message_id']);
                        }
                    } else {

                        $order_deadline = $odder['end_time']; // 订单截止时间
                        $current_time = time(); // 当前时间

                        $remaining_time = $order_deadline - $current_time; // 剩余时间（单位：秒）

                        // 将剩余时间转换为天、小时、分钟和秒
                        $days = floor($remaining_time / (60 * 60 * 24));
                        $hours = floor(($remaining_time % (60 * 60 * 24)) / (60 * 60));
                        $minutes = floor(($remaining_time % (60 * 60)) / 60);
                        $seconds = $remaining_time % 60;


                        $jizhisji = date("m-d H:i:s", $odder['end_time']);
                        $cchu = "剩余时间:{$hours}小时{$minutes}分钟{$seconds}秒";
                    }
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '截至时间:' . $jizhisji,
                                        'callback_data' => 'closeww'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => $cchu,
                                        'callback_data' => 'closeww'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '更换支付方式',
                                        'callback_data' => 'changepay ' . $order_no
                                    ),
                                    array(
                                        'text' => '取消订单',
                                        'callback_data' => 'checkodder ' . $order_no
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '联系客服',
                                        'callback_data' => 'CUSTOMER_SERVICE'
                                    )

                                )


                            )
                        )
                    );


                    //$this->telegram->editMessageCaption($chat['id'],$callback_id,null,$msg);
                    $this->telegram->editMessage($chat['id'], $message_id, $msg, $button, 'HTML', true);
                    $res = $this->Customer->CreateQRcode($odder['trxadd'], $odder['paymoney']);
                    $isid = $this->telegram->sendDocumentphoto($chat['id'], $res, "订单号：{$order_no}
商品名称：<strong>{$odder['name']}</strong>
支付方式：USDT-TRC20
过期时间：{$jizhisji}
请使用支持USDT-TRC20的App扫码
或手动转账
下方地址和金额都支持点击复制

转账金额：{$odder['paymoney']} USDT
转账地址：<code>{$odder['trxadd']}</code>

🔴请务必按照上方【转账金额】支付，否则您错误支付的金额将无法找回！！！", $message_id);


                    // $this->db->update('order',['message_id' => $isid],['order_no' => $order_no]);  

                    Db::name('order')->where("order_no", $order_no)->update(['message_id' => $isid]);
                }
            }
            if ($tou == "otherpay") {
                $order_no = $callbackExplode[1];
                if ($this->bots['yzfpay'] != 1) {
                    $this->telegram->answerCallback($callback_id, "支付渠道已关闭", true);
                    return true;
                }

                $user = Db::name('order')->where("order_no", $order_no)->find();
                $s = ['3' => 1, '6' => 2, '12' => 3];

                $yzfkey = 'taocan' . $s[$user['tancan']] . "_r";  //123 对应 3612 
                $yzfprice = isset($this->bots[$yzfkey]) ? $this->bots[$yzfkey] : 500;
                if ($yzfprice <= 0) {
                    $this->telegram->answerCallback($callback_id, "渠道 套餐价格 未配置", true);
                    return true;
                }
                if (empty($this->bots['yzf_gateway'])) {
                    $this->telegram->answerCallback($callback_id, "网关未配置", true);
                    return true;
                }
                if (empty($this->bots['yzf_uid'])) {
                    $this->telegram->answerCallback($callback_id, "支付uid未配置", true);
                    return true;
                }
                if (empty($this->bots['yzf_key'])) {
                    $this->telegram->answerCallback($callback_id, "支付key未配置", true);
                    return true;
                }
                Db::name('order')->where("order_no", $order_no)->update(['rmbprice' => $yzfprice, 'uid' => $this->bots['yzf_uid'], "key" => $this->bots['yzf_key'], 'yzf_gateway' => $this->bots['yzf_gateway']]);
                $payurl = 'http://' . env('proxyDomain', 'false') . '/customer/Bapi/other_pzy?order_no=' . $order_no;

                $button = json_encode(
                    array(
                        'inline_keyboard' => array(
                            array(
                                array(
                                    'text' => '(￥' . $yzfprice . ')支付宝',
                                    'url' => $payurl . '&type=alipay'
                                )

                            ),
                            array(
                                array(
                                    'text' => '(￥' . $yzfprice . ')微信支付',
                                    'url' => $payurl . '&type=wxpay'
                                )

                            ),
                            array(
                                array(
                                    'text' => '(￥' . $yzfprice . ')QQ钱包',
                                    'url' => $payurl . '&type=qqpay'
                                )

                            ),

                            array(
                                array(
                                    'text' => '刷新',
                                    'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                ),
                                array(
                                    'text' => '取消订单',
                                    'callback_data' => 'checkodder ' . $order_no
                                )

                            ),
                            array(
                                array(
                                    'text' => '联系客服',
                                    'callback_data' => 'CUSTOMER_SERVICE'
                                )

                            )


                        )
                    )
                );

                $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);

                $this->telegram->answerCallback($callback_id, "请选择适合您的支付", true);
            }
            if ($tou == "otherzyf") {
                $type = $callbackExplode[1];
                $money = $callbackExplode[2];
                $order_no = $callbackExplode[3];
                $this->telegram->answerCallback($callback_id, $type . " " . $money, true);
            }
            if ($tou == "changetrxadd") {
                $this->telegram->answerCallback($callback_id, "請直接输入trc20_usdt地址，即可替换之前付款地址", true);
            }



            


            

            


            

            if ($tou == "checkodder") {
                $order_no = $callbackExplode[1];
                $odder = Db::name('order')->where("order_no", $order_no)->find();

                Db::name('order')->where("order_no", $order_no)->delete();

                // $this->db->delete('user_odder',['order_no' =>$order_no]);
                $this->telegram->deleteMessage($chat['id'], $message_id);
                if (isset($odder['message_id'])) {
                    $this->telegram->deleteMessage($chat['id'], $odder['message_id']);
                }
                return true;
            }

           
           
            if ($tou == "chakan") {
                $type = $callbackExplode[1];
                if ($type == "yaoqing") {
                    $msg = "您的邀请地址:<code>https://t.me/" . $this->bots['botuser'] . '?start=user_' . $callback_from['id'] . "</code>";
                    $this->telegram->sendMessage($chat['id'], $msg);
                    $this->telegram->sendPhoto($chat['id'], "https://www.freeimg.cn/i/2023/11/20/655b0966d829a.jpg");
                }
                if ($type == "daili") {
                    $msg = "您的代理地址:<code>https://t.me/" . $this->bots['botuser'] . '?start=puser_' . $callback_from['id'] . "</code>" . "您邀请的下级成为代理 您可以获得20u的直推奖励，成功推荐2名代理。您自动升级成为合伙人。从第三名代理开始您将获得20+40的直推和建单奖励 共60 u 详情 请联系客服获取相关机制!";
                    $this->telegram->sendMessage($chat['id'], $msg);
                }
                if ($type == "wodeyaoqing") {
                    //查询下级 1-10  支持下一页
                    $yaoqinglist = Db::name('daikaihuiyuan_user_money')->where(["botid" => (int) $this->bots['botid'], 'invite' => $callback_from['id']])->order("_id desc")->page(1, 10)->select();
                    $gong = Db::name('daikaihuiyuan_user_money')->where(["botid" => (int) $this->bots['botid'], 'invite' => $callback_from['id']])->count();

                    $msq = "<strong>我的下级</strong>" . "\r\n" . "<strong>总计邀请" . $gong . "人</strong>" . "\r\n";
                    foreach ($yaoqinglist as $key => $value) {
                        // code...
                        $nickname = isset($value['nickname']) ? $value['nickname'] : "";
                        if (empty($nickname)) {
                            $msq .= '<code>' . $value['userid'] . "</code>" . "\r\n";
                        } else {
                            $msq .= '<code>' . $value['userid'] . "==" . $nickname . '</code>' . "\r\n";
                        }
                    }
                    //编辑按钮
                    $button = json_encode(
                        array(
                            'inline_keyboard' => array(
                                array(
                                    array(
                                        'text' => '下一页',
                                        'callback_data' => 'wodeyaoqingxiaycc 1'
                                    ),
                                    array(
                                        'text' => '上一页',
                                        'callback_data' => 'wodeyaoqingxiaycc 2'
                                    )

                                ),
                                array(
                                    array(
                                        'text' => '关闭',
                                        'callback_data' => 'close'
                                    )

                                )


                            )
                        )
                    );
                    $this->telegram->editMessage($chat['id'], $message_id, $msq, $button, 'HTML', true);
                }



                $this->telegram->answerCallback($callback_id);
                return true;
            }
           
           
            if ($tou == "yindan") {
                $type = $callbackExplode[1]; //huiyuan kami nengliang
                if ($type == "huiyuan") {
                    $ruhao = isset($userinfo['tghao']) ? $userinfo['tghao'] : $this->bots['kefutelegram'];
                    $ruhao = str_replace('_', '\_', $ruhao);
                    $this->telegram->sendMessage($chat['id'], "请发送你需要开通/续费会员的Telegram用户名：
1️⃣@开头，如 @{$ruhao}
2️⃣http开头，如 https://t.me/{$ruhao}

批量下单，可一次发送多个用户名，每行一个，或者 以 空格 逗号 间隔：
🔔例如：
@用户名1
@用户名2

🔔或
@用户名1 @用户名2", "", null, 'Markdown', true);
                }






                $this->telegram->answerCallback($callback_id);
                return true;
            }
           
           

           

            
            
            
            
            
            if ($tou == "pkguehunyu") {
                $money = $callbackExplode[2] * 1;
                $plorder = $callbackExplode[4]; //缓存订单号
                $yue = $callbackExplode[1];
                $usernames = cache::get($this->bots['botid'] . $plorder);
                if (!empty($usernames)) {
                    $kkmsg = "";
                    $knomsg = "";
                    $zmoney = 0;
                    foreach ($usernames as $key => $value) {
                        // code...
                        if (1 == 1) {
                            $kkmsg .= "<strong>@" . $value . "</strong>" . "\r\n";
                        } else {
                            unset($usernames[$key]);
                            $knomsg .= "<strong>@" . $value . "</strong>" . "\r\n";
                        }
                    }
                    $zmoney = count($usernames) * $money;

                    $msg = "可开通用户名:" . "\r\n" . $kkmsg . "不可开通用户名:" . "\r\n" . $knomsg . "\r\n" . "本次开通需要花费:" . "\r\n" . "<strong>" . $zmoney . "</strong>U";

                    $paymoney = $this->paymoney($zmoney);
                    $jzsh = time() + 60 * 15;
                    $order_no = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
                    $lstime = time() - 600;
                    $lsdingdan = Db::name('order')->where(['userid' => $callback_from['id'], "botid" => $this->bots['botid'], 'start' => 0])->where('create_time', '>=', $lstime)->count();
                    if ($lsdingdan >= 5) {
                        $url = "https://img.picui.cn/free/2024/07/10/668e1ed44a8cd.jpg";
                        $button = json_encode(
                            array(
                                'inline_keyboard' => array(
                                    array(
                                        array(
                                            'text' => '联系客服',
                                            'callback_data' => 'CUSTOMER_SERVICE'
                                        )

                                    )


                                )
                            )
                        );

                        $msgg = "订单建立失败了！" . "\r\n" . "如果您无法支付！请联系客服哟！" . "\r\n" . "感谢您的支持";
                        $media = array(
                            'type' => 'photo',
                            'media' => $url,
                            'caption' => $msgg,
                            "parse_mode" => 'HTML',
                        );

                        $this->telegram->editMessageMedia($chat['id'], $message_id, NULL, json_encode($media), $button);
                    } else {
                         
                        $pricelist= $this->Customer->Get_shoujia($this->bots,$yue);
                        $lirun = $pricelist['botlirun']*count($usernames);
                        $slirun= $pricelist['ptlirun']*count($usernames);
                        
                        Db::name('order')->insert(['userid' => $callback_from['id'], 'money' => $zmoney, 'paymoney' => $paymoney, 'username' => json_encode($usernames), 'create_time' => time(), "trxadd" => $this->bots['trxadd'], "botid" => $this->bots['botid'], "nichen" => "批量开通" . count($usernames) . "个会员", "name" => "批量购买会员" . count($usernames) . "个会员", 'tancan' => $yue, 'start' => 0, 'order_no' => $order_no, 'end_time' => $jzsh, 'smessage_id' => $message_id, "mode" => "Daikaihuiyuan", "moth" => "pkaihuiy", 'danprice' => $money, 'lirun' => $lirun, 'slirun' => $slirun]);
                        if ($lsdingdan > 1) {
                            //不在监听 减少资源浪费
                        } else {
                            //添加到支付监听
                            $job = 'app\bapi\syservice\Getzhandam';
                            $queueName = 'zhandam';
                            $data = array("end_time" => time() + 60 * 15, "trxadd" => $this->bots['trxadd'], 'order_no' => $order_no);
                            // Queue::push($job, $data, $queueName);
                        }
                        $user = Db::name('order')->where("order_no", $order_no)->find();

                        $xzssss = date("m-d H:i:s");
                        if ($user) {
                            if ($this->bots['botad'] == $callback_from['id']) {
                                $button = json_encode(
                                    array(
                                        'inline_keyboard' => array(
                                            array(
                                                array(
                                                    'text' => '👤管理员直接开通',
                                                    'callback_data' => 'payodderkkkssskkk admin ' . $order_no
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '刷新',
                                                    'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                                ),
                                                array(
                                                    'text' => '取消订单',
                                                    'callback_data' => 'checkodder ' . $order_no
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '联系客服',
                                                    'callback_data' => 'CUSTOMER_SERVICE'
                                                )

                                            )


                                        )
                                    )
                                );
                            } else {
                                $button = json_encode(
                                    array(
                                        'inline_keyboard' => array(
                                            array(
                                                array(
                                                    'text' => 'USDT支付',
                                                    'callback_data' => 'payodderkkkssskkk usdt ' . $order_no
                                                ),
                                                array(
                                                    'text' => '余额支付',
                                                    'callback_data' => 'payodderkkkssskkk yue ' . $order_no
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '刷新',
                                                    'callback_data' => 'payodderkkkssskkk re ' . $order_no
                                                ),
                                                array(
                                                    'text' => '取消订单',
                                                    'callback_data' => 'checkodder ' . $order_no
                                                )

                                            ),
                                            array(
                                                array(
                                                    'text' => '联系客服',
                                                    'callback_data' => 'CUSTOMER_SERVICE'
                                                )

                                            )


                                        )
                                    )
                                );
                            }

                            $msg = "订单号：{$user['order_no']}
商品名称：<strong>{$user['name']}</strong>
订单金额：<strong>{$user['paymoney']}</strong> USDT
当前时间：{$xzssss}

请选择支付方式：";


                            $this->telegram->editMessageCaption($chat['id'], $message_id, null, $msg);
                            $this->telegram->editMessageReplyMarkup($chat['id'], $message_id, null, $button);
                        }
                    }
                } else {
                    $this->telegram->sendMessage($chat['id'], "批量开通 处理超时 请 重新发起");
                }
            }


        }
    }

    public function keepThreeFirstAndLast($string)
    {
        return substr($string, 0, 3) . '...' . substr($string, -3);
    }


    public function paymoney($money)
    {   //获取位数 
        $weishu = isset($this->bots['wallet_price_digit']) ? (int) $this->bots['wallet_price_digit'] : 4;
        $chu = $this->multiplyByTen(1, $weishu);
        $beijia = 1 / $this->multiplyByTen(1, $weishu);
        if ($weishu <= 2) {
            $suiji = 9;
        } else {
            $suiji = 99;
        }

        $paymoney = $money + (mt_rand(1, $suiji) / $chu);
        $ok = Cache::get("chongzhijinedandizhi" . $this->bots['botid'] . $paymoney);
        if ($ok) {
            return $this->paymoney($money + $beijia);
        } else {
            Cache::set("chongzhijinedandizhi" . $this->bots['botid'] . $paymoney, $paymoney, 900);
            return $paymoney;
        }
    }
    public function multiplyByTen($number, $times)
    {
        // 计算结果
        $result = $number;

        for ($i = 0; $i < $times; $i++) {
            $result *= 10; // 每次乘以 10
        }

        return $result;
    }
    public function inline_query($query, $offset, $inline_id, $from) {}
    public function pd_reply_to_message($message, $message_id, $sender_chat, $chat, $date, $reply_to_message) {}
    public function new_member($new_member, $message_id, $from, $chat, $date) {}
    public function left_member($left_member, $message_id, $from, $chat, $date) {}
    //解析邀请进群 
    public function my_chat_member($chat, $from, $date, $old_chat_member, $new_chat_member) {}
    // 请求加入群聊
    public function chat_join_request($chat, $from, $invite_link, $date) {}

    // 更新了群头像 new_chat_photo 头像数据
    public function new_chat_photo($chat, $from, $new_chat_photo, $date) {}
}
