2

I'm making a Control Panel to manage my Telegram Bot using Telegram Bot API & PHP.

Basically, I want to display every message by a single user in a small chat box.

Because there are potentially more users who had sent message to the Bot, I have to check that if the user_id of sender is not duplicated and repeated again then make a new chat box for the new sender.

In order to do this I grabbed the number of arrays in the result and did this:

PRIVATE CODE

As you can see at the beginning of the code I made the variable store_id to save the FIRST sender_id and if this sender_id is repeated again then continue with the for loop till the $i is less than $num.

But the problem is it does not show anything at all. I mean no error and no result!

What is going wrong here, what should I do?

UPDATE:

PRIVATE CODE

But again the result is:

enter image description here

  • 3
    *"`$store_id = $sender_id; if($sender_id == $store_id){`"* -- can't you spot any problem with this code? Also, the curly bracket that closes the `for` block is missing but I guess it is placed after the code you posted. – axiac Dec 15 '17 at 17:15
  • What do u mean by `cant spot any problem` ? –  Dec 15 '17 at 17:24
  • When will $sender_id ever not be equal to $store_id? – DragonYen Dec 15 '17 at 17:32
  • Well, you probably can't and that's why you are here. I thought it's obvious that if you copy the value of `$sender_id` to `$store_id` in the first statement, they are equal and the `if` expression always evaluates to `TRUE` on the second statement. – axiac Dec 15 '17 at 17:35

4 Answers4

3

The problem is because first you are assigning

$sender_id = $updateArray["result"][$i]["message"]["from"]["id"];

then you are assigning

$store_id = $sender_id;

means $store_id will have exact content of $sender_id

then you are checking

if($sender_id == $store_id)

which will always be true and loop is getting continue each time.

That's why nothing is getting displayed on the screen and its not a syntax error.

Your are forgetting to assign correct store_id in $store_id.

Hope I helped you. Good Luck.

Gaurav Kumar
  • 334
  • 1
  • 7
2

The problem is because you only check for a sender is already exists or its a new sender. If its new then you add sender_id to the array and print the chat box. And if its an already existing sender then you do nothing and continue.

Means you are skipping to print msg for the sender whose id is already existed in array.

so now instead of doing continue use array_search() like

$sender_ids = array();
for($i=0;$i<$num;$i++)
{
    $sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
    if(!(in_array($sender_ids, $sender_ids)))
    {
        $sender_ids[] = $sender_id;
        echo ' CHAT BOX ';
    }
    else
    {
        $key = array_search($sender_ids, $sender_ids);
        // here this $key will be now same for all the messages from same sender id.
        // and you can make logic to group them.
    }
} 

hope i helped you. :)

Gaurav Kumar
  • 334
  • 1
  • 7
1

Why are you comparing sender_id with store_id. For comparison they should be come from different source and then you will check whether they are equal or not. There should be a way you should implement to check whether the sender_id is already their. You may create an array then keep storing sender_id in the array, but before assigning you will check the sender_id should not already exist in array. If it exists then continue.

$sender_ids = array();

for($i=0;$i<$num;$i++)
{
    $sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
    if(!(in_array($sender_ids, $sender_ids)))
    {
        $sender_ids[] = $sender_id;
        echo ' CHAT BOX ';
    }
    else
    {
        continue;
    }
} 
Gaurav Kumar
  • 334
  • 1
  • 7
  • Great, but now I get these errors: `Notice: Use of undefined constant haystack - assumed 'haystack'` & `in_array() expects parameter 2 to be array, string given` –  Dec 15 '17 at 17:44
  • 1
    i have updated the code. use if(!(in_array($sender_ids, $sender_ids))) put array name at the place of haystack i.e $sender_ids – Gaurav Kumar Dec 15 '17 at 17:48
  • write $sender_ids at the place of haystack inside in_array(). This will fix your error. – Gaurav Kumar Dec 15 '17 at 17:52
  • Well, a [so] answer is just a starting base (or just a pointing in the right direction). Use it to solve your immediate problem and do your research to adapt it to your needs and improve your code. You cannot expect me (or somebody else on [so]) to fix your code. If I do it then it is not your code any more, isn't it? – axiac Dec 15 '17 at 17:55
  • @GauravKumar Thanks for the effort, I updated the question about your answer, please see the **UPDATE 2** –  Dec 15 '17 at 17:59
0

It's not very clear what you want to achieve but I guess the following code will do it:

$updateArray = json_decode($update, TRUE);

// Initialize $store_id with a value that doesn't appear in the input data
$store_id = NULL;

// foreach is better that for(;;) to iterate over collections
foreach ($updateArray["result"] as $item) {
   // Get the sender of the current item
   $sender_id = $item["message"]["from"]["id"];
   // Use strict comparison to make sure it doesn't match when it shouldn't
   // (f.e. if `$sender_id` is the empty string it is == to NULL but not ===)
   if ($sender_id === $store_id) {
      // It is the same sender as of the previous message; skip this message
      continue;
   } else {
      // This is a new sender; process it...
      echo ' CHAT BOX ';
      // ... and remember it for the next items
      $store_id = $sender_id;
   }
}
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Better answer, But it shows all the boxes for each message and does not mind the `if ($sender_id === $store_id) { // It is the same sender as of the previous message; skip this message continue; } ` condition!!! –  Dec 15 '17 at 17:48
  • I posted an image about NEW result in the updated question... check it out. –  Dec 15 '17 at 17:52