2016-05-22 22:08:45 +02:00
|
|
|
--[[
|
|
|
|
drua-tg
|
|
|
|
A fork of JuanPotato's lua-tg (https://github.com/juanpotato/lua-tg),
|
|
|
|
modified to work more naturally from an API bot.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
drua = require('drua-tg')
|
|
|
|
drua.IP = 'localhost'
|
|
|
|
drua.PORT = 4567
|
|
|
|
drua.message(chat_id, text)
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local SOCKET = require('socket')
|
|
|
|
|
|
|
|
local comtab = {
|
|
|
|
add = { 'chat_add_user %s %s', 'channel_invite %s %s' },
|
|
|
|
kick = { 'chat_del_user %s %s', 'channel_kick %s %s' },
|
|
|
|
rename = { 'rename_chat %s "%s"', 'rename_channel %s "%s"' },
|
|
|
|
link = { 'export_chat_link %s', 'export_channel_link %s' },
|
|
|
|
photo_set = { 'chat_set_photo %s %s', 'channel_set_photo %s %s' },
|
|
|
|
photo_get = { [0] = 'load_user_photo %s', 'load_chat_photo %s', 'load_channel_photo %s' },
|
|
|
|
info = { [0] = 'user_info %s', 'chat_info %s', 'channel_info %s' }
|
|
|
|
}
|
|
|
|
|
|
|
|
local format_target = function(target)
|
|
|
|
target = tonumber(target)
|
|
|
|
if target < -1000000000000 then
|
|
|
|
target = 'channel#' .. math.abs(target) - 1000000000000
|
|
|
|
return target, 2
|
|
|
|
elseif target < 0 then
|
|
|
|
target = 'chat#' .. math.abs(target)
|
|
|
|
return target, 1
|
|
|
|
else
|
|
|
|
target = 'user#' .. target
|
|
|
|
return target, 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local escape = function(text)
|
|
|
|
text = text:gsub('\\', '\\\\')
|
|
|
|
text = text:gsub('\n', '\\n')
|
|
|
|
text = text:gsub('\t', '\\t')
|
|
|
|
text = text:gsub('"', '\\"')
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
local drua = {
|
|
|
|
IP = 'localhost',
|
|
|
|
PORT = 4567
|
|
|
|
}
|
|
|
|
|
|
|
|
drua.send = function(command, do_receive)
|
2016-05-25 15:01:54 +02:00
|
|
|
local s = SOCKET.connect(drua.IP, drua.PORT)
|
|
|
|
assert(s, '\nUnable to connect to tg session.')
|
|
|
|
s:send(command..'\n')
|
|
|
|
local output
|
2016-05-22 22:08:45 +02:00
|
|
|
if do_receive then
|
2016-05-25 15:01:54 +02:00
|
|
|
output = string.match(s:receive('*l'), 'ANSWER (%d+)')
|
2016-05-27 04:35:45 +02:00
|
|
|
output = s:receive(tonumber(output)):gsub('\n$', '')
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
s:close()
|
2016-05-25 15:01:54 +02:00
|
|
|
return output
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
drua.message = function(target, text)
|
2016-05-31 01:10:58 +02:00
|
|
|
target = format_target(target)
|
|
|
|
text = escape(text)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = 'msg %s "%s"'
|
|
|
|
command = command:format(target, text)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.send_photo = function(target, photo)
|
2016-05-31 01:10:58 +02:00
|
|
|
target = format_target(target)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = 'send_photo %s %s'
|
|
|
|
command = command:format(target, photo)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.add_user = function(chat, target)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
target = format_target(target)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.add[a]:format(chat, target)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.kick_user = function(chat, target)
|
|
|
|
-- Get the group info so tg will recognize the target.
|
|
|
|
drua.get_info(chat)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
target = format_target(target)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.kick[a]:format(chat, target)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.rename_chat = function(chat, name)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.rename[a]:format(chat, name)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.export_link = function(chat)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.link[a]:format(chat)
|
|
|
|
return drua.send(command, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.get_photo = function(chat)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.photo_get[a]:format(chat)
|
|
|
|
local output = drua.send(command, true)
|
2016-05-31 01:10:58 +02:00
|
|
|
if output:match('FAIL') then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return output:match('Saved to (.+)')
|
|
|
|
end
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
drua.set_photo = function(chat, photo)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.photo_set[a]:format(chat, photo)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.get_info = function(target)
|
2016-05-31 01:10:58 +02:00
|
|
|
local a
|
|
|
|
target, a = format_target(target)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = comtab.info[a]:format(target)
|
|
|
|
return drua.send(command, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.channel_set_admin = function(chat, user, rank)
|
2016-05-31 01:10:58 +02:00
|
|
|
chat = format_target(chat)
|
|
|
|
user = format_target(user)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = 'channel_set_admin %s %s %s'
|
|
|
|
command = command:format(chat, user, rank)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.channel_set_about = function(chat, text)
|
2016-05-31 01:10:58 +02:00
|
|
|
chat = format_target(chat)
|
|
|
|
text = escape(text)
|
2016-05-22 22:08:45 +02:00
|
|
|
local command = 'channel_set_about %s "%s"'
|
|
|
|
command = command:format(chat, text)
|
|
|
|
return drua.send(command)
|
|
|
|
end
|
|
|
|
|
otouto 3.11
"things occurred"
Added some utilities (id_from_username, id_from_message), removed some utilities (latcyr, others?).
Removed cycle-wasting "shortcuts" -- no more automatic id_str or name; text_lower remains.
Moved userdata (nicknames, lastfm, etc) to a different tree in the database (automatic migration will occur). /me now returns userdata.
Speaking of migration, database now stores the latest version run to make future automigration easy.
Database now saves hourly rather than minutely.
Changed readme and some plugins to reflect above changes.
Removed broken rockspec (Brayden, feel free to re-add once it's working).
Added option to automatically block people (via drua) when blacklisted.
Fixed about.lua trigger problems.
administration 1.11 - Removed /kickme and /broadcast. Users should leave manually, and announcements should be made via channel rather than spam. /setqotd now handles forwarded messages correctly. /kick, /ban, /hammer,
/mod, /admin now support multiple arguments. Added get_targets function. No migration is necessary.
2016-07-05 09:29:11 +02:00
|
|
|
drua.block = function(user)
|
|
|
|
return drua.send('block_user user#' .. user)
|
|
|
|
end
|
|
|
|
|
|
|
|
drua.unblock = function(user)
|
|
|
|
return drua.send('unblock_user user#' .. user)
|
|
|
|
end
|
|
|
|
|
2016-05-22 22:08:45 +02:00
|
|
|
return drua
|