2

I'm working on a Content Management System using PHP for controlling over my Telegram Bot. Basically what I have done till now is that I can read the messages that people has sent to my Telegram Bot and answer to them.

In order to do that, I coded this:

<?php 
$botToken = '423495534:asdsadsadasdsadsa';
$website = 'https://api.telegram.org/bot'.$botToken;
$update = file_get_contents($website."/getUpdates");
$updateArray = json_decode($update, TRUE);
$info = file_get_contents($website."/getme");
$infoArray = json_decode($info, TRUE);
$num = count($updateArray["result"]);
$sender_ids = array();
$sender_infos = array();
for($i=0;$i<$num;$i++){
    $sender_id    = $updateArray["result"][$i]["message"]["from"]["id"];
    $sender_isbot = $updateArray["result"][$i]["message"]["from"]["is_bot"];
    $sender_fname = $updateArray["result"][$i]["message"]["from"]["first_name"];
    $sender_lname = $updateArray["result"][$i]["message"]["from"]["last_name"];
    $sender_uname = $updateArray["result"][$i]["message"]["from"]["username"];
    $sender_type  = $updateArray["result"][$i]["message"]["chat"]["type"];
    $sender_msg   = $updateArray["result"][$i]["message"]["text"];
    $sender_date  = $updateArray["result"][$i]["message"]["date"];
    if(false === $key = array_search($sender_id, $sender_ids)){
        $sender_ids[] = $sender_id;
        $sender_infos[] = [
            'sender_id' => $sender_id,
            'sender_isbot' => $sender_isbot,
            'sender_fname' => $sender_fname,
            'sender_lname' => $sender_lname,
            'sender_uname' => $sender_uname,
            'sender_type' => $sender_type,
            'sender_msg' => [$sender_msg],
            'sender_date' => [$sender_date]
        ];
    }else{
        $sender_infos[$key]['sender_msg'][] = $sender_msg;
        $sender_infos[$key]['sender_date'][] = $sender_date;
    }
}
$num2 = count($sender_ids); 
for($j=0;$j<$num2;$j++){
    $id = $sender_infos[$j]["sender_id"];
    $first_name = $sender_infos[$j]["sender_fname"];
    $last_name = $sender_infos[$j]["sender_lname"];
    $username = $sender_infos[$j]["sender_uname"];
    $messages = $sender_infos[$j]["sender_msg"];
    $acc_type = $sender_infos[$j]["sender_type"];
    $isbot = $sender_infos[$j]["sender_isbot"];
    $num1 = count($messages);

    echo '                          
    <div class="col-md-3">
        <div class="box box-danger direct-chat direct-chat-danger">
            <div class="box-header with-border">
            <h3 class="box-title">'.$first_name.'</h3>
            <sup>'.$acc_type.' - '.$isbot.'</sup>
            <div class="box-tools pull-right">
                <span data-toggle="tooltip" title="'.$num1.' New Messages" class="badge bg-red">'.$num1.'</span>
                    <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Contacts" data-widget="chat-pane-toggle">
                    <i class="fa fa-comments"></i></button>
                <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
            </div>
        </div>
        <div class="box-body">
            <div class="direct-chat-messages">
                <div class="direct-chat-msg">
                    <div class="direct-chat-info clearfix">
                        <span class="direct-chat-name pull-left">';
                            echo '<strong>'.$first_name.'</strong>';
                            echo '
                        </span>
                    </div>';
                    for($i=0;$i<$num1;$i++){
                        echo '<div class="direct-chat-text">';
                        $text = $sender_infos[$j]["sender_msg"][$i];
                        if($text[0] === '/') {
                            echo '<a href="#" style="color:blue">'.$text.'</a>';
                        }else{
                            echo $text;
                        }
                        echo '<span class="direct-chat-timestamp pull-right">';
                        // echo date('l', $sender_infos[$j]["sender_date"][$i]);
                        echo '</span>';
                        echo '</div>';
                    }
                echo '
                </div>
                <div class="direct-chat-msg right">
                    <div class="direct-chat-info clearfix">
                        <span class="direct-chat-name pull-right">'.$bot_fname.'</span>
                    </div>
                    <img class="direct-chat-img" src="
                    ';
                    if ($dataSet->GetAvatar() != NULL){
                            echo $dataSet->GetAvatar();
                    }else{
                        echo "img/noprofilepic.jpg";
                    }
                    echo '
                    " alt="Message User Image">
                    ';
                    $num3 = count($request_params["text"]);
                    foreach($_SESSION['messages'] as $reply){
                        echo '<div class="direct-chat-text">';
                        echo $reply;
                        echo '</div>';
                    }
                    echo '
                    </div>
                </div>
                <div class="direct-chat-contacts">
                    <ul class="contacts-list">
                        <li>
                            <a href="#">
                                <div class="contacts-list-info">
                                    <span class="contacts-list-name">
                                        ';
                                        echo $first_name = $sender_infos[$j]["sender_fname"];  
                                        echo ' </br> ';
                                        echo $last_name = $sender_infos[$j]["sender_lname"];
                                        echo ' </br> ';
                                        echo ' <a target="_blank" href="http://www.t.me/'.$username.'">'.$username.'</a>';
                                        echo ' </br> ';
                                        echo ' <small class="contacts-list-date pull-right">'.$id.'</small>
                                        </span>
                                    <span class="contacts-list-msg">QUOTE</span>
                                </div>
                            </a>
                        </li>
                    </ul>
                </div>
            </div>';
            if (isset($_POST['send'])){
                $pm = $_POST['message'];
                array_push($_SESSION['messages'], $pm); 
                $request_params = [
                    'chat_id' => $id,
                    'text' => $pm
                ];
                echo "<META HTTP-EQUIV='Refresh' Content='0; URL=telegrambots.php?user_name=".$user_name."'>";
                $request_url = 'https://api.telegram.org/bot' . $botToken . '/sendMessage?' . http_build_query($request_params);
                $response = file_get_contents($request_url);
            }
            echo '
            <div class="box-footer">
                <form action="" method="post">
                    <div class="input-group">
                        <input type="text" name="message" placeholder="Write your direct message" class="form-control">
                        <span class="input-group-btn">
                            <input name="send" type="submit" class="btn btn-danger btn-flat"/>
                        </span>
                    </div>
                </form>
            </div>
        </div>
    </div>
    ';
}
?>

Basically what it does is that it grabs the latest updates of my Bot and count the number of results and save it in $num. Then with a for loop I tried to divide every information about the user who has sent message. After that I store all the required information of sender in a separated array called $sender_infos. And the next for loop shows a basic Chat Box depending on the number of users.

This whole thing makes this:

print screen

So it works fine and perfect but the problem is that, whenever I try answering to one conversation, it sends the message to all the available users. This issue comes from the for loop which divides every users by the Chat Box.

However what I want to do is to send a SINGLE DIRECT MESSAGE to a CUSTOM user and NOT ALL OF THEM.

I hope I have explained my problem well, so you could understand. If not, please comment me for more information and additional updates.

NOTE: I don't want a quick answer to this question. Because I'm facing this for several days and I don't know how to solve it. Please make sure you understand what I'm asking for and then add your suggest.

Thanks in advance...

0 Answers0