3
Hello,
I have the following code:
<?php
function readUpdate($updateId) {
$fh = fopen(basename(__FILE__, '.php').'.txt', 'a');
fwrite($fh, $updateId.' ');
fclose($fh);
}
function sendMessage($chatId, $text) {
if(!empty($chatId) && !empty($text)) {
file_get_contents('https://api.telegram.org/botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?text='.$text.'&chat_id='.$chatId);
}
}
for($i = 0; $i = 1; $i++) {
$getUpdates = json_decode(file_get_contents('https://api.telegram.org/botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/getUpdates'), true);
$count = count($getUpdates['result']) - 1;
readUpdate($getUpdates['result'][$count]['message']['message_id']);
if(strpos(file_get_contents(basename(__FILE__, '.php').'.txt'), $getUpdates['result'][$count]['message']['message_id']) !== true) {
foreach($getUpdates['result'] as $result) {
$updateId = $result['update_id'];
$messageId = $result['message']['message_id'];
$userId = $result['message']['from']['id'];
$firstName = $result['message']['from']['first_name'];
$lastName = $result['message']['from']['last_name'];
$userName = $result['message']['from']['username'];
$chatId = $result['message']['chat']['id'];
$type = $result['message']['chat']['type'];
$text = $result['message']['text'];
if($text == '/start') {
sendMessage($chatId, 'Olá '.$firstName);
echo '[START] Mensagem de boas vindas enviada.<br>';
continue;
}
}
} else {
}
$i--;
}
?>
The return of /getUpdates
is:
{"ok":true,"result":[{"update_id":444612040,
"message":{"message_id":1,"from":{"id":330782177,"first_name":"Brayan","last_name":"Noxious","username":"STRILEXLIVE"},"chat":{"id":330782177,"first_name":"Brayan","last_name":"Noxious","username":"STRILEXLIVE","type":"private"},"date":1492270909,"text":"/start","entities":[{"type":"bot_command","offset":0,"length":6}]}},{"update_id":444612041,
"message":{"message_id":2,"from":{"id":330782177,"first_name":"Brayan","last_name":"Noxious","username":"STRILEXLIVE"},"chat":{"id":330782177,"first_name":"Brayan","last_name":"Noxious","username":"STRILEXLIVE","type":"private"},"date":1492270972,"text":"Ol\u00e1"}}]}
The functions are working perfectly, everything is okay, however, when I access the script’s URL, the script starts sending the messages to the people who sent messages to the bot, and after it sends the messages to the people who sent the messages to it, he starts sending another message, and so on, so he’s spamming the chat.
The function readUpdate
serves for the script to take a unique message ID and save it to a file. txt for the bot to mark as "message read", not to spam, but still it spams the chat.
I looked for everything on the Internet, how to read the file line by line. txt and see if such string is on the line, etc, to try to resolve this error, but I was not successful.
Thanks in advance.
I tested locally and did not return anything or send message.
– STRILEXLIVE
Use insecure CURL if not configured correctly by modifying the
CURLOPT_SSL_VERIFYPEER
for0
and theCURLOPT_SSL_VERIFYHOST
for0
. Adjust (or remove) theCURLOPT_LOW_SPEED_LIMIT
if your connection (or target server) is slow.– Inkeliz