非常感谢您阅读。
我有一个开放源代码的bot,每天都会介绍学校的时间表。 从Viber键盘中选择天数:
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;
}
按下一天的按钮会从SQL查询中生成数据集,其中包含选定日期的类,并保存到数组中。
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;
}
然后使用以下for循环创建一个Viber响应,并通过REST API发送给Viber:
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);
}
出现问题的原因是,每行两次作为消息发送-不是连续两次,而是以交错的方式发送,即first,second,third,first,第四更像random,或者可能是一分钟后的第三次或
通过查看ngrok日志,我可以看到有两个或三个条目包含相同的时间戳记,并且例如,似乎两次按下了星期一按钮,尽管没有发生这种情况。 / p>
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
}
有什么想法吗?
更新:通过将数组内容组合到一条Viber消息中,问题就消失了。即
$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);
//}