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 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 him, he starts to send another message, and so it goes, then goes spamming the chat.
The function readUpdate
is for the script to get a unique ID of the message and save it in a .txt file for the bot to mark it as a "read message", so as not to spell it, but it still spams the chat. >
I looked for everything on the internet, how to read line by line from the .txt file and see if that string is on the line, etc., to try to solve this error, but I did not succeed.
Thank you in advance.