X
X
XtReLL2019-02-24 19:07:07
In contact with
XtReLL, 2019-02-24 19:07:07

The messages.send method in VkApi does not work. The bot does not send anything to the received message. How to decide?

Hello. I'm trying to implement a VK chat bot, following the example from the documentation.
Here is my code -

<?php

require('../vendor/autoload.php');

$app = new Silex\Application();
$app['debug'] = true;

// Register the monolog logging service
$app->register(new Silex\Provider\MonologServiceProvider(), array(
  'monolog.logfile' => 'php://stderr',
));



// Our web handlers

$app->get('/', function() use($app) {
  return "Hi World";
});

$app->post('/bot', function() use($app) {
  
  
  $data = json_decode(file_get_contents('php://input'));
  
  if(!$data)
    return 'error1';
  
  if($data->secret !== getenv('VK_SECRET_TOKEN') && $data->type !== 'confirmation')
    return 'error1';
  
  switch($data->type) {
    case 'confirmation':
      return getenv('VK_CONFIRMATION_CODE');
      break;
    case 'message_new':
      $request_params = array(
        'user_id' => $data->object->from_id,
        'message' => 'Тест',
        'access_token' => getenv('VK_TOKEN'),
        'v' => '5.92'
      );
      file_get_contents('https://api.vk.com/method/messages.send?' . http_build_query($request_params));
      return 'ok';
      break;
  }
  
  return "error1";
});

$app->run();

All rights issued, the server connected. The bot itself is hosted on heroku.
For any word, the bot should send "Test". but nothing happens.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2019-02-24
@XtReLL

It's nice that I was invited as an expert, but I did not encounter such issues. I can advise you to turn on your head and debug:
1. Does the sending code work separately?
2. If the sending code works, are VK_TOKEN from the environment and $data->object->from_id sent correctly? Is it the same user ID (and in the right format)?
3. is it in $data->type -> 'message_new' ?
4. Does the request come to /bot?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question