Home » Php » php – Implementing a function from another one

php – Implementing a function from another one

Posted by: admin February 25, 2020 Leave a comment

Questions:

this is my first post and im really new to php world, Sorry if i use wrong words or terms to describe my problem.

I have a script that was written in php for telegram, and i have two commands that is almost the same.

with this one it is possible to add to multiple groups using this separator “,”

/madd
groupid, groupid

this one it is not possible to do

/wadd
groupid

I want to be able to use the second command with a separator between the group ids

I can see in the code how it is written to be able to do that in /madd but I cant figure out how to implement this in /wadd

I will put here the two scripts for the commands, would appreciate any help <3
Thanks.

This is the one with the separator

 if(strpos($text,"/madd") === 0){
    $e = explode("\n",$text);
    if(!isset($e[1]) || !isset($e[2])) exit($bot->sendMessage($user['id'],"/madd\ntelegram_username or ID\ngroup, ids\nmax_posts per day (default 1)\nnumber of reposts (default 0)\n\nAdds new telegram username to the manual autodrop users list.\n\nReplace group_id with * to add to all groups"));
    $group = $e[2]; $username = strtolower(ltrim($e[1],"@"));

    $addto = [];
    if($group == "*"){
        $addto = $groups;
    }else{
        $ex = explode(",",$group);
        foreach($ex AS $id){
            $id = trim($id);
            if(isset($groups[$id])){
                $addto[$id] = $groups[$id];
            }
        }
    }

    $add = mysqli_fetch_assoc($conn->query("SELECT * FROM utenti WHERE username LIKE '%".mysqli_real_escape_string($conn,$username)."%'"));
    if(!isset($add['id'])){
        $add = mysqli_fetch_assoc($conn->query("SELECT * FROM utenti WHERE id = ".mysqli_real_escape_string($conn,$username)));
    }
    if(!isset($add['id'])){ exit($bot->sendMessage($user['id'],"😔 User $username not found! They need to start the bot, or send a simple message in one of the groups.")); }

    $result = "❕ <b>Selected User:</b> <a href='tg://user?id=".$add['id']."'>".$add['username']." (".$add['id'].")</a>";
    $manual = [
        'max_posts' => $e[3] ?? 1,
        'reposts' => $e[4] ?? 0,
        'groups' => []
    ];

    foreach($addto AS $id => $r){
        $manual['groups'][] = $r['group_id'];
    }

    $conn->query("UPDATE utenti SET manual = '".mysqli_real_escape_string($conn,json_encode($manual,true))."' WHERE id = ".$add['id']);
    $bot->sendMessage($user['id'],$result."\n\n".json_encode($manual,JSON_PRETTY_PRINT));
    exit();
}

and this is without the separator

if(strpos($text,"/wadd") === 0){
    $e = explode("\n",$text);
    if(!isset($e[1]) || !isset($e[2])) exit($bot->sendMessage($user['id'],"/wadd\ngroup_id\ntelegram_username or ID\nmax_posts per day (default 5)\nnumber of reposts (default 0)\n\nAdds new telegram username to the whitelisted users list.\n\nReplace group_id with * to add to all groups"));
    $group = $e[1]; $username = strtolower(ltrim($e[2],"@"));
    if($group != "*" && !isset($groups[$group])) exit($bot->sendMessage($user['id'],"👥 <b>Group $group</b> not whitelisted."));

    $add = mysqli_fetch_assoc($conn->query("SELECT * FROM utenti WHERE username LIKE '%".mysqli_real_escape_string($conn,$username)."%'"));
    if(!isset($add['id'])){
        $add = mysqli_fetch_assoc($conn->query("SELECT * FROM utenti WHERE id = ".mysqli_real_escape_string($conn,$username)));
    }
    if(!isset($add['id'])){ exit($bot->sendMessage($user['id'],"😔 User $username not found! They need to start the bot, or send a simple message in one of the groups.")); }

    $result = "❕ <b>Selected User:</b> <a href='tg://user?id=".$add['id']."'>".$add['username']." (".$add['id'].")</a>";
    $premiums = json_decode($add['premiums'],true);
    if(!is_array($premiums)) $premiums = [];

    $groups_to_update = []; if($group == "*"){ foreach($groups AS $group => $data){ $groups_to_update[] = $group; } }else{ $groups_to_update[] = "-".abs($group); }
    $max_posts = $e[3] ?? 5; $reposts = $e[4] ?? 0;
    foreach($groups_to_update AS $group_id){
        if(isset($premiums[$group_id])){
            if($premiums[$group_id]['max_posts'] == $max_posts && $premiums[$group_id]['reposts'] == $reposts){
                $result.="\n\n🕐 <i>No changes for group</i> <code>$group_id</code>";
            }else{
                $result.="\n\n♻️ <b>Updated</b> for group <code>$group_id</code>\nMax. Posts: $max_posts | Reposts: $reposts";
                $premiums[$group_id] = ['max_posts' => $max_posts,'reposts' => $reposts];
            }
        }else{
            $result.="\n\n➕ <b>Added</b> for group <code>$group_id</code>\nMax. Posts: $max_posts | Reposts: $reposts";
            $premiums[$group_id] = ['max_posts' => $max_posts,'reposts' => $reposts];
        }
    }
    $conn->query("UPDATE utenti SET premiums = '".mysqli_real_escape_string($conn,json_encode($premiums,true))."' WHERE id = ".$add['id']);
    $bot->sendMessage($user['id'],$result);
    exit();
}
How to&Answers: