2016-05-22 22:08:45 +02:00
|
|
|
--[[
|
2016-08-23 06:16:32 +02:00
|
|
|
drua-tg.lua
|
2016-08-15 07:08:55 +02:00
|
|
|
Based on JuanPotato's lua-tg (https://github.com/juanpotato/lua-tg),
|
|
|
|
modified to work more naturally from an API bot.
|
2016-05-22 22:08:45 +02:00
|
|
|
|
2016-08-15 07:08:55 +02:00
|
|
|
Usage:
|
|
|
|
drua = require('drua-tg')
|
2016-08-23 06:16:32 +02:00
|
|
|
drua.IP = 'localhost'
|
|
|
|
drua.PORT = 4567
|
2016-08-15 07:08:55 +02:00
|
|
|
drua.message(chat_id, text)
|
2016-07-25 11:03:35 +02:00
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
To run multiple commands on the same TCP session:
|
|
|
|
s = drua.sopen()
|
|
|
|
drua.message(chat_id, text, s)
|
|
|
|
drua.send_photo(chat_id, filename, s)
|
|
|
|
s:close()
|
|
|
|
|
2016-08-23 06:16:32 +02:00
|
|
|
Copyright 2015-2016 Juan Potato
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to
|
|
|
|
deal in the Software without restriction, including without limitation the
|
|
|
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
IN THE SOFTWARE.
|
2016-07-25 11:03:35 +02:00
|
|
|
]]
|
2016-05-22 22:08:45 +02:00
|
|
|
|
2016-08-25 13:28:59 +02:00
|
|
|
local socket = require('socket')
|
2016-05-22 22:08:45 +02:00
|
|
|
|
|
|
|
local comtab = {
|
2016-08-15 07:08:55 +02:00
|
|
|
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' }
|
2016-05-22 22:08:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-15 07:00:24 +02:00
|
|
|
local function format_target(target)
|
2016-08-15 07:08:55 +02:00
|
|
|
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
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-15 07:00:24 +02:00
|
|
|
local function escape(text)
|
2016-08-15 07:08:55 +02:00
|
|
|
text = text:gsub('\\', '\\\\')
|
|
|
|
text = text:gsub('\n', '\\n')
|
|
|
|
text = text:gsub('\t', '\\t')
|
|
|
|
text = text:gsub('"', '\\"')
|
|
|
|
return text
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local drua = {
|
2016-08-15 07:08:55 +02:00
|
|
|
IP = 'localhost',
|
|
|
|
PORT = 4567
|
2016-05-22 22:08:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.sopen()
|
2016-08-25 13:28:59 +02:00
|
|
|
local s = socket.connect(drua.IP, drua.PORT)
|
2016-08-15 07:08:55 +02:00
|
|
|
assert(s, '\nUnable to connect to tg session.')
|
2016-08-24 19:29:26 +02:00
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
2016-08-25 13:28:59 +02:00
|
|
|
function drua.simple(command, do_receive, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
s:send(command..'\n')
|
|
|
|
local output
|
|
|
|
if do_receive then
|
|
|
|
output = string.match(s:receive('*l'), 'ANSWER (%d+)')
|
|
|
|
output = s:receive(tonumber(output)):gsub('\n$', '')
|
2016-08-24 19:29:26 +02:00
|
|
|
s:receive('*l')
|
2016-08-15 07:08:55 +02:00
|
|
|
end
|
|
|
|
return output
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-25 13:28:59 +02:00
|
|
|
function drua.send(command, do_receive, s)
|
2016-08-24 19:29:26 +02:00
|
|
|
if s then
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.simple(command, do_receive, s)
|
2016-08-24 19:29:26 +02:00
|
|
|
else
|
|
|
|
s = drua.sopen()
|
2016-08-25 13:28:59 +02:00
|
|
|
local output = drua.simple(command, do_receive, s)
|
2016-08-24 19:29:26 +02:00
|
|
|
s:close()
|
|
|
|
return output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function drua.message(target, text, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
target = format_target(target)
|
|
|
|
text = escape(text)
|
|
|
|
local command = 'msg %s "%s"'
|
|
|
|
command = command:format(target, text)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.send_photo(target, photo, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
target = format_target(target)
|
|
|
|
local command = 'send_photo %s %s'
|
|
|
|
command = command:format(target, photo)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.add_user(chat, target, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
target = format_target(target)
|
|
|
|
local command = comtab.add[a]:format(chat, target)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.kick_user(chat, target, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
-- Get the group info so tg will recognize the target.
|
2016-08-25 13:28:59 +02:00
|
|
|
drua.get_info(chat, _, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
target = format_target(target)
|
|
|
|
local command = comtab.kick[a]:format(chat, target)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.rename_chat(chat, name, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
local command = comtab.rename[a]:format(chat, name)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.export_link(chat, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
local command = comtab.link[a]:format(chat)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, true, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.get_photo(chat, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
local command = comtab.photo_get[a]:format(chat)
|
2016-08-25 13:28:59 +02:00
|
|
|
local output = drua.send(command, true, s)
|
2016-08-15 07:08:55 +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
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.set_photo(chat, photo, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
chat, a = format_target(chat)
|
|
|
|
local command = comtab.photo_set[a]:format(chat, photo)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.get_info(target, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
local a
|
|
|
|
target, a = format_target(target)
|
|
|
|
local command = comtab.info[a]:format(target)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, true, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.channel_set_admin(chat, user, rank, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
chat = format_target(chat)
|
|
|
|
user = format_target(user)
|
|
|
|
local command = 'channel_set_admin %s %s %s'
|
|
|
|
command = command:format(chat, user, rank)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-05-22 22:08:45 +02:00
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.channel_set_about(chat, text, s)
|
2016-08-15 07:08:55 +02:00
|
|
|
chat = format_target(chat)
|
|
|
|
text = escape(text)
|
|
|
|
local command = 'channel_set_about %s "%s"'
|
|
|
|
command = command:format(chat, text)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send(command, _, s)
|
2016-08-15 07:00:24 +02:00
|
|
|
end
|
2016-05-22 22:08:45 +02:00
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.block(user, s)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send('block_user user#' .. user, _, s)
|
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
|
|
|
end
|
|
|
|
|
2016-08-24 19:29:26 +02:00
|
|
|
function drua.unblock(user, s)
|
2016-08-25 13:28:59 +02:00
|
|
|
return drua.send('unblock_user user#' .. user, _, s)
|
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
|
|
|
end
|
|
|
|
|
2016-05-22 22:08:45 +02:00
|
|
|
return drua
|