Viber bot messages triggered multiple times when send within a for loop though REST API

1.1k Views Asked by At

Many thanks for reading.

I have an open source bot presenting for each day the timetable of a school. Days are chosen from a Viber keyboard:

function createWeekdayMenu()
{
   $keyboard_array['Type']='keyboard';
   $keyboard_array['DefaultHeight']=false;
   $keyboard_array['BgColor']="#FFFFFF";
   $keyboard=$keyboard_array;
    $weekdays_title = ['Δευτέρα', 'Τρίτη', 'Τετάρτη', 'Πέμπτη', 'Παρασκευή'];
    $weekdays_payload = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'];
    $keyboard_components = ["Columns", "Rows", "BgColor","TextVAlign","TextHAlign","TextOpacity","Text","TextSize","ActionType","ActionBody"];
  
    for ($i = 0; $i < count($weekdays_title); $i++) {
        $values_to_combine = array('2','2', "#2db9b9",'center','center','100', $weekdays_title[$i],'regular','reply', $weekdays_payload[$i]);
        $keyboard['Buttons'][]= array_combine($keyboard_components, $values_to_combine);

    }
return $keyboard;

}

Pressing a day's button produces the dataset from an SQL query containing the classes of the selected day,saved into an array.

while ($row = $result->fetch_assoc()) {

                $greekName = $row['greekName'];
                $hourStart = $row['hoursStart'];
                $description = $row['description'];
                $classCode = $row['code'];
                $place = $row['placeName'];
                $teacherName = $row['surname'];
                $mandatory = $row['mandatory'];
                $semesterName = $row['semesterName'];
                $comments = $row['comments'];
                
                

                $result_to_send = '' . $greekName . chr(10) . '⌚' . $hourStart . ':00' . chr(10) . ''
                . $description . chr(10) . '' . $classCode . chr(10) . '‍' . $teacherName . chr(10) . 'Αίθουσα: ' . $place . chr(10) .
                $print_status . chr(10) . $lex . $semesterName . chr(10) . '' .  $comments ;


                $result_array[] = $result_to_send;
}

Then a Viber response created with the following for loop and send to Viber through REST API:

for ($i = 0; $i < count($result_array); $i++) {
  
  $data['receiver'] = $senderId;
  $data['type'] = 'text';
 
  $data['text']=$result_array[$i];

send_to_viber($data);
  }
 
 
 function send_to_viber($response)
{

$ch = curl_init("https://chatapi.viber.com/pa/send_message");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response));
  curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json','charset=utf-8','X-Viber-Auth-Token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx']);
  $result = curl_exec($ch);

 
    curl_close($ch);
}

Problem arises as each row is sent as a message twice -not two consecutive times but in a interleaved manner i.e. first , second , third ,first , forth more like random , and maybe a third time after a minute or so.

By looking the ngrok log I can see that there are two or three entries containing the same timestamp and it seems like, for example, Monday button is pressed twice although no such thing has happened.

First entry:
{
    "event": "message",
    "timestamp": 1601307724076,
    "chat_hostname": "SN-CHAT-02_",
    "message_token": 5491392294943973000,
    "sender": {
        "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
        "name": " xxxxxxxxxxxxxxxxxxxxxx",
        "language": "el-GR",
        "country": "GR",
        "api_version": 8
    },
    "message": {
        "text": "monday",
        "type": "text"
    },
    "silent": false
}

Entry after lets say 5 seconds:
{
    "event": "message",
    "timestamp": 1601307724076,
    "chat_hostname": "SN-CHAT-02_",
    "message_token": 5491392294943973000,
    "sender": {
        "id": "xxxxxxxxxxxxxxxxxxxxxx",
        "name": "xxxxxxxxxxxxxxxxxxxxxx",
        "language": "el-GR",
        "country": "GR",
        "api_version": 8
    },
    "message": {
        "text": "monday",
        "type": "text"
    },
    "silent": false
}

Any ideas?

UPDATE: By combining the array contents into one Viber message the problem dissapear's. i.e.

$result_array = sendDayProgram($senderId, $text);
  
  //for ($i = 0; $i < count($result_array); $i++) {
  
  $data['receiver'] = $senderId;
  $data['type'] = 'text';
 
  $data['text']=  implode(chr(10),$result_array);//($result_array[$i];

 

 
   send_to_viber($data);
//}
0

There are 0 best solutions below