#include <amxmodx>
#include <grip>
#pragma dynamic 32768
new const VK_API_VERSION[] = "5.130";
new const VK_ACCESS_TOKEN[] = "";
new const VK_GROUP_ID[] = "";
const VK_WAIT_TIME = 25;
enum VkTypes
{
VK_MESSAGE_NONE = -1,
VK_MESSAGE_NEW,
VK_MESSAGE_REPLY,
VK_MESSAGE_EDIT,
VK_MESSAGE_EVENT
};
new const VK_TYPES[][] =
{
"message_new",
"message_reply",
"message_edit",
"message_event"
// ...
};
enum requestId
{
REQUEST_GET_LONG_POLL_SERVER = 0,
REQUEST_LISTEN_LONG_POLL_SERVER
};
enum longpoll_s
{
m_server[512],
m_key[512],
m_ts[64]
};
new longpoll_s:g_longPollServer[longpoll_s];
public plugin_init()
{
register_plugin("VK: Bot Long Poll API", __DATE__, "Denzer");
getLongPollServer();
}
getLongPollServer()
{
new formData[512]; formatex(formData, charsmax(formData), "access_token=%s&group_id=%s&v=%s", VK_ACCESS_TOKEN, VK_GROUP_ID, VK_API_VERSION);
new GripBody:body = grip_body_from_string(formData);
grip_request("https://api.vk.com/method/groups.getLongPollServer", body, GripRequestTypePost, "requestHandler", _, requestId:REQUEST_GET_LONG_POLL_SERVER);
grip_destroy_body(body);
}
listenLongPollServer()
{
//server_print("POPERLI");
new formData[512]; formatex(formData, charsmax(formData), "act=a_check&key=%s&wait=%d&mode=2&ts=%s", g_longPollServer[m_key], VK_WAIT_TIME, g_longPollServer[m_ts]);
new GripRequestOptions:options = grip_create_default_options();
new GripBody:body = grip_body_from_string(formData);
grip_request(g_longPollServer[m_server], body, GripRequestTypePost, "requestHandler", options, requestId:REQUEST_LISTEN_LONG_POLL_SERVER);
grip_destroy_body(body);
grip_destroy_options(options);
}
public requestHandler(requestId:reqId)
{
if (grip_get_response_state() == GripResponseStateError)
{
return;
}
if (grip_get_response_status_code() != GripHTTPStatusOk)
{
return;
}
new errorBuffer[128];
new GripJSONValue:json = grip_json_parse_response_body(errorBuffer, charsmax(errorBuffer));
if (json == Invalid_GripJSONValue)
{
log_amx(errorBuffer);
return;
}
switch (reqId)
{
case REQUEST_GET_LONG_POLL_SERVER:
{
new GripJSONValue:response = grip_json_object_get_value(json, "response");
grip_destroy_json_value(json);
grip_json_object_get_string(response, "key", g_longPollServer[m_key], charsmax(g_longPollServer[m_key]));
grip_json_object_get_string(response, "server", g_longPollServer[m_server], charsmax(g_longPollServer[m_server]));
grip_json_object_get_string(response, "ts", g_longPollServer[m_ts], charsmax(g_longPollServer[m_ts]));
server_print("SERVER: %s^nKEY: %s^nTS: %s", g_longPollServer[m_server], g_longPollServer[m_key], g_longPollServer[m_ts]);
grip_destroy_json_value(response);
}
case REQUEST_LISTEN_LONG_POLL_SERVER:
{
//server_print("OP");
grip_json_object_get_string(json, "ts", g_longPollServer[m_ts], charsmax(g_longPollServer[m_ts]));
// array
new GripJSONValue:updates = grip_json_object_get_value(json, "updates");
new updatesCount = grip_json_array_get_count(updates);
for (new i = 0; i < updatesCount; i++)
{
new GripJSONValue:arrayElem = grip_json_array_get_value(updates, i);
new GripJSONValue:type = grip_json_object_get_value(arrayElem, "type");
new GripJSONValue:object = grip_json_object_get_value(arrayElem, "object");
grip_destroy_json_value(arrayElem);
new typeBuffer[128]; grip_json_get_string(type, typeBuffer, charsmax(typeBuffer));
grip_destroy_json_value(type);
new VkTypes:vkType = parseType(typeBuffer);
if (vkType == VK_MESSAGE_NEW)
{
new GripJSONValue:message = grip_json_object_get_value(object, "message");
new messageBuffer[512]; grip_json_object_get_string(message, "text", messageBuffer, charsmax(messageBuffer));
grip_destroy_json_value(message);
server_print(messageBuffer);
}
grip_destroy_json_value(object);
//grip_json_serial_to_file(arrayElem, "test.json", true);
}
grip_destroy_json_value(updates);
grip_destroy_json_value(json);
}
}
listenLongPollServer();
}
VkTypes:parseType(type[])
{
for (new i; i < sizeof(VK_TYPES); i++)
{
if (equal(VK_TYPES[i], type))
{
return VkTypes:i;
}
}
return VK_MESSAGE_NONE;
}