forked from vysheng/tg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vysheng#9 from koter84/scripted
Scripted
- Loading branch information
Showing
10 changed files
with
450 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ config.log | |
config.status | ||
.idea/ | ||
nbproject/ | ||
scripts/mysql.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
function vardump(value, depth, key) | ||
local linePrefix = "" | ||
local spaces = "" | ||
|
||
if key ~= nil then | ||
linePrefix = "["..key.."] = " | ||
end | ||
|
||
if depth == nil then | ||
depth = 0 | ||
else | ||
depth = depth + 1 | ||
for i=1, depth do spaces = spaces .. " " end | ||
end | ||
|
||
if type(value) == 'table' then | ||
mTable = getmetatable(value) | ||
if mTable == nil then | ||
print(spaces ..linePrefix.."(table) ") | ||
else | ||
print(spaces .."(metatable) ") | ||
value = mTable | ||
end | ||
for tableKey, tableValue in pairs(value) do | ||
vardump(tableValue, depth, tableKey) | ||
end | ||
elseif type(value) == 'function' or | ||
type(value) == 'thread' or | ||
type(value) == 'userdata' or | ||
value == nil | ||
then | ||
print(spaces..tostring(value)) | ||
else | ||
print(spaces..linePrefix.."("..type(value)..") "..tostring(value)) | ||
end | ||
end | ||
|
||
function escapeCSV (value) | ||
if type(value) ~= 'string' | ||
then | ||
value = tostring(value) | ||
end | ||
if string.find(value, '[,"]') then | ||
value = '"' .. string.gsub(value, '"', '""') .. '"' | ||
end | ||
return value | ||
end | ||
|
||
function toCSV (value) | ||
local s = "" | ||
for _,p in pairs(value) do | ||
if type(p) == 'table' | ||
then | ||
s = s .. "," .. toCSV(p) | ||
else | ||
s = s .. "," .. escapeCSV(p) | ||
end | ||
end | ||
return string.sub(s, 2) -- remove first comma | ||
end | ||
|
||
function fromCSV (value) | ||
value = value .. ',' -- ending comma | ||
local t = {} -- table to collect fields | ||
local fieldstart = 1 | ||
repeat | ||
-- next field is quoted? (start with `"'?) | ||
if string.find(value, '^"', fieldstart) then | ||
local a, c | ||
local i = fieldstart | ||
repeat | ||
-- find closing quote | ||
a, i, c = string.find(value, '"("?)', i+1) | ||
until c ~= '"' -- quote not followed by quote? | ||
if not i then error('unmatched "') end | ||
local f = string.sub(value, fieldstart+1, i-1) | ||
table.insert(t, (string.gsub(f, '""', '"'))) | ||
fieldstart = string.find(s, ',', i) + 1 | ||
else -- unquoted; find next comma | ||
local nexti = string.find(value, ',', fieldstart) | ||
table.insert(t, string.sub(value, fieldstart, nexti-1)) | ||
fieldstart = nexti + 1 | ||
end | ||
until fieldstart > string.len(value) | ||
return t | ||
end | ||
|
||
function file_exists (name) | ||
local f = io.open( name, "r" ) | ||
if f ~= nil then | ||
io.close(f) | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
-- no need to have all the functions in every script | ||
function on_msg_receive (msg) end | ||
function on_our_id (id) end | ||
function on_secret_chat_created (peer) end | ||
function on_user_update (user) end | ||
function on_user_status_update (user, online) end | ||
function on_chat_update (user) end | ||
function on_get_difference_end () end | ||
function on_binlog_replay_end () end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
our_id = 0 | ||
inbox = "/tmp/telegram_inbox" | ||
outbox = "/tmp/telegram_outbox" | ||
|
||
require "scripts.base_functions"; | ||
|
||
print () | ||
print (">---> Telegram-CLI Lua Script!") | ||
print (">---> read from " .. inbox .. " for incoming messages") | ||
print (">---> write to " .. outbox .. " for outgoing messages") | ||
print () | ||
|
||
|
||
-- (table) | ||
-- [date] = (number) 1393973625 | ||
-- [from] = (table) | ||
-- [id] = (number) 32778144 | ||
-- [type] = (string) user | ||
-- [print_name] = (string) user#32778144 | ||
-- [unread] = (boolean) true | ||
-- [flags] = (number) 16 | ||
-- [service] = (boolean) false | ||
-- [id] = (string) 119 | ||
-- [text] = (string) 💋 | ||
-- [to] = (table) | ||
-- [id] = (number) 25928291 | ||
-- [type] = (string) user | ||
-- [print_name] = (string) user#25928291 | ||
-- [out] = (boolean) false | ||
-- Message # 119 (flags 16) | ||
-- date, from_id, from_type, from_print_name, unread, flags, service, id, text, to_id, to_type, to_print_name, out | ||
-- 1393973625, 32778144, user, user#32778144, true, 16, false, 119, bericht, 25928291, user, user#25928291, false | ||
-- | ||
--> >---> on_msg_receive | ||
--(table) | ||
--[to] = (table) | ||
-- [phone] = (string) 31629735850 | ||
-- [print_name] = (string) Dennis | ||
-- [flags] = (number) 144 | ||
-- [first_name] = (string) Dennis | ||
-- [type] = (string) user | ||
-- [id] = (number) 25928291 | ||
--[date] = (number) 1394041546 | ||
--[flags] = (number) 16 | ||
--[from] = (table) | ||
-- [phone] = (string) 31641903990 | ||
-- [last_name] = (string) Litchidova | ||
-- [print_name] = (string) Anna_Litchidova | ||
-- [flags] = (number) 528 | ||
-- [first_name] = (string) Anna | ||
-- [type] = (string) user | ||
-- [id] = (number) 32778144 | ||
--[text] = (string) Ik ben op de terugweg | ||
--[service] = (boolean) false | ||
--[unread] = (boolean) true | ||
--[id] = (string) 120 | ||
--[out] = (boolean) false | ||
|
||
|
||
function on_msg_receive (msg) | ||
if msg.out | ||
then | ||
print (">---> on_msg_send"); | ||
--vardump (msg) | ||
|
||
else | ||
print (">---> on_msg_receive"); | ||
--vardump (msg) | ||
|
||
csv = | ||
msg.id .. ";" .. | ||
msg.date .. ";" .. | ||
msg.from.id .. ";" .. | ||
msg.from.phone .. ";" .. | ||
msg.from.print_name .. ";" .. | ||
msg.to.id .. ";" .. | ||
msg.to.phone .. ";" .. | ||
msg.to.print_name .. ";" .. | ||
msg.text .. ";" .. | ||
tostring(msg.out) | ||
print ( csv ) | ||
end | ||
end | ||
|
||
function on_our_id (id) | ||
print (">---> on_our_id"); | ||
vardump (id) | ||
our_id = id | ||
end | ||
|
||
function on_secret_chat_created (peer) | ||
print (">---> on_secret_chat_created"); | ||
vardump (peer) | ||
end | ||
|
||
function on_user_update (user) | ||
print (">---> on_user_update"); | ||
vardump (user) | ||
end | ||
|
||
function on_user_status_update (user, online) | ||
print (">---> on_user_status_update"); | ||
vardump (user) | ||
vardump (online) | ||
end | ||
|
||
function on_chat_update (user) | ||
print (">---> on_chat_update"); | ||
vardump (user) | ||
end | ||
|
||
function on_get_difference_end () | ||
print (">---> on_get_difference_end"); | ||
end | ||
|
||
function on_binlog_replay_end () | ||
print (">---> on_binlog_replay_end"); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
require "scripts.base_functions"; | ||
|
||
print () | ||
print (">---> Telegram-CLI Lua Script!") | ||
print (">---> read from and write to MySQL database/tables for incoming and outgoing messages") | ||
print () | ||
|
||
-- check for mysql-conf file | ||
if not file_exists("./scripts/mysql.conf") then | ||
f=io.open( "./scripts/mysql.conf", "w" ) | ||
f:write( 'db_db = "telegram"\n' ) | ||
f:write( 'db_user = "telegram"\n' ) | ||
f:write( 'db_pass = "telegram"\n' ) | ||
f:write( 'db_host = "127.0.0.1"\n' ) | ||
f:write( 'db_port = "3306"\n' ) | ||
f:close() | ||
end | ||
|
||
-- http://www.keplerproject.org/luasql/examples.html | ||
luasql = require "luasql.mysql" | ||
dofile "./scripts/mysql.conf" | ||
env = assert (luasql.mysql()) | ||
con = assert (env:connect( db_db, db_user, db_pass, db_host, db_port )) | ||
|
||
res = assert (con:execute[[ | ||
CREATE TABLE IF NOT EXISTS `users` ( | ||
`uid` int(11) NOT NULL, | ||
`phone` varchar(25) NOT NULL, | ||
`first_name` varchar(255) NOT NULL, | ||
`last_name` varchar(255) NOT NULL, | ||
`print_name` varchar(255) NOT NULL, | ||
PRIMARY KEY (`uid`) | ||
) | ||
]]) | ||
res = assert (con:execute[[ | ||
CREATE TABLE IF NOT EXISTS `online` ( | ||
`online_id` int(11) NOT NULL AUTO_INCREMENT, | ||
`uid` int(11) NOT NULL, | ||
`moment` datetime NOT NULL, | ||
`online` int(11) NOT NULL, | ||
PRIMARY KEY (`online_id`) | ||
) | ||
]]) | ||
res = assert (con:execute[[ | ||
CREATE TABLE IF NOT EXISTS `inbox` ( | ||
`mid` int(11) NOT NULL, | ||
`uid` int(11) NOT NULL, | ||
`moment` datetime NOT NULL, | ||
`msg` text NOT NULL, | ||
PRIMARY KEY (`mid`, `uid`) | ||
) | ||
]]) | ||
res = assert (con:execute[[ | ||
CREATE TABLE IF NOT EXISTS `outbox` ( | ||
`mid` int(11) NOT NULL, | ||
`uid` int(11) NOT NULL, | ||
`moment` datetime NOT NULL, | ||
`msg` text NOT NULL, | ||
PRIMARY KEY (`mid`, `uid`) | ||
) | ||
]]) | ||
res = assert (con:execute[[ | ||
CREATE TABLE IF NOT EXISTS `sendbox` ( | ||
`send_id` int(11) NOT NULL AUTO_INCREMENT, | ||
`uid` int(11) NOT NULL, | ||
`msg` text NOT NULL, | ||
PRIMARY KEY (`send_id`) | ||
) | ||
]]) | ||
|
||
|
||
function on_msg_receive (msg) | ||
if msg.out | ||
then | ||
print (">---> on_msg_send"); | ||
--vardump (msg) | ||
res = con:execute("INSERT INTO `outbox` ( mid, uid, moment, msg ) VALUES ( '" .. msg.id .. "', '" .. msg.to.id .. "', FROM_UNIXTIME(" .. msg.date .. "), '" .. msg.text .. "' );"); | ||
else | ||
print (">---> on_msg_receive"); | ||
--vardump (msg) | ||
res = con:execute("INSERT INTO `inbox` ( mid, uid, moment, msg ) VALUES ( '" .. msg.id .. "', '" .. msg.from.id .. "', FROM_UNIXTIME(" .. msg.date .. "), '" .. msg.text .. "' );"); | ||
end | ||
end | ||
|
||
function on_user_update (user) | ||
print (">---> on_user_update"); | ||
res = con:execute("INSERT INTO `users` ( uid, phone, first_name, last_name, print_name ) VALUES ( '" .. user.id .. "', '" .. user.phone .. "', '" .. user.first_name .. "', '" .. user.last_name .. "', '" .. user.print_name .. "' );"); | ||
end | ||
|
||
function on_user_status_update (user, online) | ||
print (">---> on_user_status_update"); | ||
if online == -1 | ||
then | ||
online = 0; | ||
end | ||
res = con:execute("INSERT INTO `online` ( uid, moment, online ) VALUES ( '" .. user.id .. "', NOW(), '" .. online .. "' );"); | ||
end | ||
|
||
function on_chat_update (user) | ||
print (">---> on_chat_update"); | ||
-- vardump (user) | ||
end |
Oops, something went wrong.