A
A
Arkady Baganin2019-07-03 21:52:28
PHP
Arkady Baganin, 2019-07-03 21:52:28

How to get rid of "such" code?

I have a code, I need criticism and advice to improve my "shit code" ...
This is the code for the VK bot.
Essence: auto-response to user questions The bot.php
code itself

<?php
require 'config.php';

$data = json_decode(file_get_contents('php://input'));

if(!isset($data)) exit;

if(strcmp($data->secret, VK_SECRET_KEY) !== 0 && strcmp($data->type, VK_TYPE_CONFIRMATION) !== 0) return;

/*
* __init__
*/

switch ($data->type) {
  case VK_TYPE_CONFIRMATION:
    echo VK_CONFIRMATION_TOKEN;
    break;

  case VK_TYPE_NEW_MESSAGE:
    $GROUP_ID = $data->group_id;
    $USER_ID = $data->object->user_id;
    $PEER_ID = $data->object->peer_id;
    $RANDOM_ID = $data->object->random_id;
    $USER_TEXT = mb_strtolower($data->object->text);
    $USER_INFO = json_decode(file_get_contents('https://api.vk.com/method/users.get?user_ids='.$USER_ID.'&access_token='.VK_TOKEN.'&v='.VK_API_VERSION.''));
    $USER_NAME = $USER_INFO->response[0]->first_name;
    $BOT_ANSWER = 'Бот не нашёл ответа на ваш вопрос!';
    $BOT_COMMANDS = [
      '!{user_name}' => $USER_NAME,
      '!{bot_name}' => BOT_NAME,
      '!{bot_version}' => BOT_VERSION
    ];

    foreach ($VK_CONFIG_FAQ as $k => $v) {
      $res = strpos(mb_strtolower($k), mb_strtolower($USER_TEXT));
      if($res !== false){
        $BOT_ANSWER = $v[array_rand($v)];
      }
    }

    if($BOT_ANSWER != 'Бот не нашёл ответа на ваш вопрос!' and strpos($BOT_ANSWER, '!{') != false){
      foreach ($BOT_COMMANDS as $command => $val) {
        $BOT_ANSWER = str_replace($command, $val, $BOT_ANSWER);
      }
    }

    $params = http_build_query([
      'message' => $BOT_ANSWER,
      'peer_id' => $PEER_ID,
      'user_id' => $USER_ID,
      'random_id' => $RANDOM_ID,
      'access_token' => VK_TOKEN,
      'v' => VK_API_VERSION
    ]);

    file_get_contents('https://api.vk.com/method/messages.send?' . $params);

    echo 'ok';
    break;
}

config.php
// TYPES
define('VK_TYPE_CONFIRMATION', 'confirmation');
define('VK_TYPE_NEW_MESSAGE', 'message_new');

// BOT PARAMS
define('BOT_NAME', 'NameBot');
define('BOT_VERSION', '0.0.1');

// FAQ
$VK_CONFIG_FAQ = [
  'Привет | Hi' => [
    'Привет, !{user_name}!!!! Тебя приветствует бот !{bot_name} версии !{bot_version}',
    'Опять вы.... Ну, hi....'
  ],
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Karo, 2019-07-03
@ark_yt

I would advise:
1) Break the code into functions with meaningful names.
Short sections of code are much better readable.
For example, I would take out the functions validateRequest, sendMessage, findAnswer. Transfer requests to external services (vk) to separate functions/methods of classes
2) Remove unused variables. Why is $GROUP_ID here?
3) Do not use single-letter variables.
Instead of `foreach ($VK_CONFIG_FAQ as $k => $v) {` better something like `foreach ($VK_CONFIG_FAQ as $questionPattern => $answers) {`
4) Don't duplicate string constants like 'The bot didn't find an answer to your question!'. It is better to put it into a separate constant of the NOT_FOUND_ANSWER type.
Well, they already wrote about PSR

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question