A
A
Andrew Kovacz2016-11-21 22:53:48
PHP
Andrew Kovacz, 2016-11-21 22:53:48

Why is it not sending messages correctly (messages.send)?

In general, I have a database with questions and answers, if someone wrote me a message in VK, then I read this message, check whether it matches any of the questions, and in fact, if so, then I substitute the answer from my I send the database and send it ..
Everything seems to work, but sometimes it happens that it can just send a random message to a random dude from those who wrote to me, regardless of whether there are read messages and if someone wrote to me, I just can’t understand why?
Here is the code:

<?php

require_once "vk.api.php";
    
define('token', 'e495f5565168873062688ff988a83e5e39a45e3fb2a3ba7344cdb772545e7157a97259af8f1b3d3865932');
$vk = new VK(token);
    
$link = mysqli_connect("localhost","root","","digi") or die("Ошибка " . mysqli_error($link));
    
if (!$link) {
    echo "Ошибка: Невозможно установить соединение с MySQL." . PHP_EOL;
    echo "Код ошибки errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Текст ошибки error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
 
echo "Соединение с MySQL установлено!" . PHP_EOL;
echo "Информация о сервере: " . mysqli_get_host_info($link) . PHP_EOL;

$messages = $vk->request('messages.get', [
    'count' => '10',
]);
file_get_contents($messages);
    
$resp = $messages['response'];
    
foreach ($resp as $message): 
            
    if (!$message['read_state']) {
        
        $vk->request('account.setOnline');
                
        $query = "SELECT input, output FROM basis";
            
        $result = mysqli_query($link, $query); 
        while ($row = mysqli_fetch_assoc($result)) { 
            if ($message['body'] == $row["input"]) {
                $reading = $vk->request('messages.markAsRead', [
                    'peer_id' => $message['uid'],
                ]);
                $typing = $vk->request('messages.setActivity', [
                    'user_id' => $message['uid'], 
                    'type' => 'typing',
                ]);
                $send = $vk->request('messages.send', [
                    'message' => $row["output"],
                     'uid' => $message['uid'],
                ]);
                file_get_contents($send); //если не писать так, сообщение попросту не отсылается
                }
            }
        }
endforeach;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Zhilyaev, 2016-11-22
@kirill_782

Write the json object to a file, how the bot gives answers. Or, if possible, switch to LongPoll. Or on the 5th version of the API.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question