Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require('../vendor/autoload.php');
- $app = new Silex\Application();
- $app['debug'] = true;
- use FormulaParser\FormulaParser;
- // Register the monolog logging service
- $app->register(new Silex\Provider\MonologServiceProvider(), array(
- 'monolog.logfile' => 'php://stderr',
- ));
- // Register view rendering
- $app->register(new Silex\Provider\TwigServiceProvider(), array(
- 'twig.path' => __DIR__.'/views',
- ));
- // Our web handlers
- $app->get('/', function() use($app) {
- return 'VK Bot';
- });
- $app->post('/bot', function() use($app) {
- $data = json_decode(file_get_contents('php://input'));
- if (!$data)
- return 'No data';
- $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;
- if ($data->type == 'confirmation')
- return getenv('VK_CONFIRMATION_CODE');
- if ($count != 0)
- {
- $formula = $data->object->message->text;
- $precision = 2;
- $request_params = array(
- 'peer_id' => $data->object->message->from_id,
- 'random_id' => rand(1, 2147483647),
- 'message' => 'Тестирование работы PHP VK бота на Heroku',
- 'access_token' => getenv('VK_TOKEN'),
- 'v' => '5.103'
- );
- try {
- $parser = new FormulaParser($formula, $precision);
- $result = $parser->getResult(); // [0 => 'done', 1 => 16.38]
- $request_params['message'] = 'Ответ: ' . number_format($result['1'], $precision, '.',
- ',');
- } catch (\Exception $e) {
- $request_params['message'] = 'Неверно!';
- }
- file_get_contents('https://api.vk.com/method/messages.send?' . http_build_query($request_params));
- echo 'ok';
- }
- return 'Error';
- });
- $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement