Answer the question
In order to leave comments, you need to log in
How to make a site (server) that sends a POST response via API in VK?
I'm going to take a VPS to host a VK bot on it, but I also need a callback API, what code is needed, or what needs to be done so that the server sends the code?
Answer the question
In order to leave comments, you need to log in
Have you already written code on your home machine that works?
VPS is not something that will do anything without code. It's just a round-the-clock computer available on the Internet.
If you want to make a bot that works on callback (server notification about events occurring in the bot) from VK, then here's an article for you on how to do it. See how the callback is configured there. And here is the minimum code that you need to upload to vps (this library is used ) and specify a link to this script in the community settings:
<?php
require_once('vk_api/autoload.php'); //подключение новой библиотеки
use DigitalStar\vk_api\vk_api as vk_api;
//**********CONFIG**************
const VK_KEY = "your_key"; //тот самый длинный ключ доступа сообщества
const ACCESS_KEY = "your_key"; //например c40b9566, введите свой
const VERSION = "5.80"; //ваша версия используемого api желательно использовать 5.80+ чтобы не было багов
$vk = new vk_api(VK_KEY, VERSION); // создание экземпляра класса работы с api, принимает ключ и версию api
$data = json_decode(file_get_contents('php://input')); //Получает и декодирует JSON пришедший из ВК
if ($data->type == 'confirmation') { //Если vk запрашивает ключ
exit(ACCESS_KEY); //Завершаем скрипт отправкой ключа
}
$vk->sendOK(); //Говорим vk, что мы приняли callback
if (isset($data->type) and $data->type == 'message_new') { //Проверяем, если это сообщение от пользователя
$id = $data->object->from_id; //Получаем id пользователя, который написал сообщение
$message = $data->object->text;
$vk->sendMessage($id, "Вот твое сообщение: $message");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question