Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. require('../vendor/autoload.php');
  4.  
  5. $app = new Silex\Application();
  6. $app['debug'] = true;
  7.  
  8. use FormulaParser\FormulaParser;
  9.  
  10. // Register the monolog logging service
  11. $app->register(new Silex\Provider\MonologServiceProvider(), array(
  12.   'monolog.logfile' => 'php://stderr',
  13. ));
  14.  
  15. // Register view rendering
  16. $app->register(new Silex\Provider\TwigServiceProvider(), array(
  17.     'twig.path' => __DIR__.'/views',
  18. ));
  19.  
  20. // Our web handlers
  21.  
  22. $app->get('/', function() use($app) {
  23.   return 'VK Bot';
  24. });
  25.  
  26. $app->post('/bot', function() use($app) {
  27.   $data = json_decode(file_get_contents('php://input'));
  28.   if (!$data)
  29.     return 'No data';
  30.  
  31.   $count = json_decode( file_get_contents('https://api.vk.com/method/messages.getConversations?offset=0&count=100&filter=unanswered&access_token='.getenv('VK_TOKEN').'&v=5.103'))->response->count;
  32.   if ($data->type == 'confirmation')
  33.     return getenv('VK_CONFIRMATION_CODE');
  34.   if ($count != 0)
  35.   {
  36.       $formula = $data->object->message->text;
  37.       $precision = 2;
  38.  
  39.       $request_params = array(
  40.         'peer_id' => $data->object->message->from_id,
  41.         'random_id' => rand(1, 2147483647),
  42.         'message' => 'Тестирование работы PHP VK бота на Heroku',
  43.         'access_token' => getenv('VK_TOKEN'),
  44.         'v' => '5.103'
  45.       );
  46.  
  47.       try {
  48.           $parser = new FormulaParser($formula, $precision);
  49.           $result = $parser->getResult(); // [0 => 'done', 1 => 16.38]
  50.           $request_params['message'] = 'Ответ: ' . number_format($result['1'], $precision, '.',
  51.                  ',');
  52.       } catch (\Exception $e) {
  53.             $request_params['message'] = 'Неверно!';
  54.       }
  55.  
  56.      
  57.       file_get_contents('https://api.vk.com/method/messages.send?' . http_build_query($request_params));
  58.       echo 'ok';
  59.   }
  60.   return 'Error';
  61. });
  62.  
  63. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement