Skip to content

Instantly share code, notes, and snippets.

@Mattze96
Created March 30, 2015 06:47
Show Gist options
  • Save Mattze96/0276890d4de14431b422 to your computer and use it in GitHub Desktop.
Save Mattze96/0276890d4de14431b422 to your computer and use it in GitHub Desktop.
telegram-cli lua file api
print ("bot v1.0")
now = os.time()
home_dir = "/home/matthias/"
function send_msg_cb(cb_extra, success, result)
if success then
return
end
end
function postpone_cb(cb_extra, success, result)
pull_outbox()
postpone (postpone_cb, "", 1)
end
function save_media(a, success, file)
if success then
local f = io.open(home_dir..'inbox', 'a')
f:write('#sender: '..a[0]..'\n')
f:write('#type: '..a[3]..'\n')
f:write('#message_start'..'\n')
f:write(file..'\n')
f:write('#message_end'..'\n')
f:flush()
f:close()
end
end
function on_msg_receive (msg)
---send_msg (msg.from.print_name, msg.text, send_msg_cb, "")
if msg.date < now then
return
end
if msg.out then
return
end
mark_read (msg.from.print_name)
if msg.text then
local f = io.open(home_dir..'inbox', 'a')
f:write('#sender: '..msg.from.print_name..'\n')
f:write('#type: '..'text'..'\n')
f:write('#message_start'..'\n')
f:write(msg.text..'\n')
f:write('#message_end'..'\n')
f:flush()
f:close()
end
if msg.media then
a = {}
a[0] = msg.from.print_name
a[1] = msg.id
a[2] = msg.date
a[3] = msg.media
if msg.media == 'photo' then
load_photo(msg.id, save_media, a)
end
if msg.media == 'video' then
load_video(msg.id, save_media, a)
end
if msg.media == 'audio' then
load_audio(msg.id, save_media, a)
end
if msg.media == 'document' then
load_document(msg.id, save_media, a)
end
end
end
function on_our_id (id)
print(id)
postpone (postpone_cb, "", 1)
end
function on_user_update (user, what)
end
function on_chat_update (chat, what)
end
function on_secret_chat_update (schat, what)
end
function on_get_difference_end ()
end
function on_binlog_replay_end ()
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function string:split( inSplitPattern, outResults )
if not outResults then
outResults = { }
end
local theStart = 1
local theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
while theSplitStart do
table.insert( outResults, string.sub( self, theStart, theSplitStart-1 ) )
theStart = theSplitEnd + 1
theSplitStart, theSplitEnd = string.find( self, inSplitPattern, theStart )
end
table.insert( outResults, string.sub( self, theStart ) )
return outResults
end
lfs = require "lfs"
function pull_outbox ()
if (os.rename(home_dir..'outbox', home_dir..'outbox_tmp')) then
local file = io.open(home_dir..'outbox_tmp', 'r')
local message = {}
local receiver = ''
for line in file:lines() do
if string.starts(line, '#receiver') then
receiver = line:split(": ")[2]
print ("Der Receiver ist: "..receiver)
end
if (line == '#message_start') then
message = {}
elseif (line == '#message_end') then
if (receiver~='') then
send_msg (receiver, table.concat(message, "\n"), send_msg_cb, "")
end
else
table.insert (message, line)
end
end
file:close()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment