Skip to content

Lightweight VK API library for Lua 5.1 named after Touhou character.

License

Notifications You must be signed in to change notification settings

VanderCat/Seiran

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Seiran*

Lightweight VK API library for Lua 5.1+ named after Touhou character.

NOTICE: This library only tested on linux!

Table of Content

Usage

local VK = require "seiran"

-- if using a file:
local at = io.open("accesstoken.vk", "r")
local vk1 = VK:new(at:read())
at:close()

-- Non-Secure way:
local vk2 = VK:new("VK.a.b19c3403c42a6d0d85d86efa1784be286ff6e6fc94c18e82c421b906cc33aeea")

local user = vk1.api.users.get{
    user_ids="vander_cat"
}[1]
print(user.first_name.." "..user.last_name)

You can also execute the example:

$ lua examples/basic.lua vander_cat

You will need to create an accesstoken.vk file contains your token though

Features

  • Longpoll
  • No special API code
    • there's actually no method like users.get, so if vk will rename something, you will only need to rename same in code
  • lightweight
    • this library is only about 200 lines
  • You only use a token to do everything (not like in some python vk api libraries)

Requirements

Lua-cURLv3*
  • You now can (!!!) write a new post function and dont use a Lua-cURLv3! just redefine seiran:post(url, data, headers)!

Optional:

Lua-cJSON

If you're not using cJSON, then you must setup json handlers:

local vk = require "seiran"
local json = require "json"

seiran:setJsonHandler(json.encode, json.decode)

TODO:

  • Add callback support
  • Login with credentials
  • Cleanup a code (?)
  • Test on other platforms

API

seiran.api.<GROUP>.<METHOD>{..}

  • Returns decoded json response from server

Descirpton:

this is how you access the api

Usage:

local user = seiran.api.users.get{
    user_ids="vander_cat"
}.response[1]

print(user.first_name.." "..user.last_name)

seiran:setJsonHandler(encode, decode)

  • function encode - Function used for encoding to json
  • function decode - Function used for decoding from json

Descirpton:

Use this function if you don't have cJSON installed or do not want to use it

Usage:

local json = require "json"

seiran:setJsonHandler(json.encode, json.decode)

seiran:longPollStart{...}

See:

https://dev.vk.com/method/messages.getLongPollServer

Usage:

seiran:longPollStart{
    need_pts = 1, --default
    group_id = 12345 -- Only for group's token
    lp_version=3 --default
}

seiran:longPollListen{}

See:

https://dev.vk.com/api/user-long-poll/getting-started#%D0%9F%D0%BE%D0%B4%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%BD%D0%B8%D0%B5

Usage:

seiran:longPollStart()

while true do
    for i, v in ipairs(seiran:longPollListen()) do
        if math.tointeger(v[1]) == 4 then
            local msg = seiran.api.messages.getById{
                message_ids=math.tointeger(v[2])
            }.response.items[1]
            local reciever = seiran.api.users.get{
                user_ids=math.tointeger(v[4])..","..math.tointeger(msg.from_id)
            }.response
            reciever[2] = reciever[2] or reciever[1]
            io.write(reciever[2].first_name.." "..reciever[2].last_name.." ( ID:"..math.tointeger(reciever[2].id)..") Wrote message \""..msg.text.."\" ")
            io.write("In chat "..reciever[1].first_name.." "..reciever[1].last_name.." ( ID:"..math.tointeger(reciever[1].id)..")\n")
        end
    end
end