Seiran*
Lightweight VK API library for Lua 5.1+ named after Touhou character.
NOTICE: This library only tested on linux!
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
- 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)
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)
- Add callback support
- Login with credentials
- Cleanup a code (?)
- Test on other platforms
- Returns decoded json response from server
this is how you access the api
local user = seiran.api.users.get{
user_ids="vander_cat"
}.response[1]
print(user.first_name.." "..user.last_name)
- function
encode
- Function used for encoding to json - function
decode
- Function used for decoding from json
Use this function if you don't have cJSON
installed or do not want to use it
local json = require "json"
seiran:setJsonHandler(json.encode, json.decode)
https://dev.vk.com/method/messages.getLongPollServer
seiran:longPollStart{
need_pts = 1, --default
group_id = 12345 -- Only for group's token
lp_version=3 --default
}
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