--incom_tgmessage
local json = require('cjson')
local cURL = require ('cURL')
local puremagic = require('puremagic')
local msgg

function callback(msg, success, result)
 if success then
    print('File downloaded to:', result)
	print('Extra var msg:', msg)
	print('Extra GLOBAL var msgg:', msgg)
	send_info_to_server(msg,result)
  else
    print('Error downloading: '..msg)
  end
end

function on_msg_receive (msg)
--    if msg.out then
--        return
--    end
    if (msg.text=='ping') then
        send_msg (msg.from.print_name, 'pong', ok_cb, false)
    end
	
	if msg.media then
		if msg.media.type == 'photo' then
			print('Loading photo'..msg.id.."\n")
			local loadans = load_photo(msg.id,callback,msg)

			if loadans then
				print('Sent?')
				print(loadans)
				msgg=msg
			end
			
			--postpone (cb_function, cb_extra, 100);
		end
		if msg.media.type == 'document' then
			print('Loading document'..msg.id.."\n")

			local loadans = load_document(msg.id,callback,msg)

			if loadans then
				print('Sent?')
				print(loadans)
				msgg=msg
			end
			
			--postpone (cb_function, cb_extra, 100);
		end
		if msg.media.type == 'video' then
			print('Loading video'..msg.id.."\n")

			local loadans = load_video(msg.id,callback,msg)

			if loadans then
				print('Sent?')
				print(loadans)
				msgg=msg
			end
			
			--postpone (cb_function, cb_extra, 100);
		end
		if msg.media.type == 'audio' then
			print('Loading audio'..msg.id.."\n")

			local loadans = load_audio(msg.id,callback,msg)

			if loadans then
				print('Sent?')
				print(loadans)
				msgg=msg
			end
			
			--postpone (cb_function, cb_extra, 100);
		end
	
	else 
		send_info_to_server(msg,false)
	end 
end

function wait(seconds)
  print("Waiting "..seconds.." seconds...\n")
  local start = os.time()
  repeat until os.time() > start + seconds
  print("Waited "..seconds.." !\n")
end

function send_info_to_server (info,loadans)
	mtb = json.encode(info)
	
	local spost = cURL.form()
	
	if loadans == false then
		spost = {
			tgevent = "incom_tgmessage",
			datas = mtb,
		}
	else
		local filemimetype = puremagic.via_path(loadans)
		spost = {
			tgevent = "incom_tgmessage",
			datas = mtb,
			ifile = {file=loadans,type=filemimetype}
		}
	end
	print("Sending to server....")
	crl = cURL.easy_init()
	--crl:setopt_url('https://sms.4u.uz/bx24/')
	crl:setopt_url('http://127.0.0.1/bx24/index.php')
	crl:post(spost)
	crl:perform()
	crl:close()
	print("Done")
end


function load_media_from_incom (id)
--	if msg.media.type == 'document' then
--		local loadans = load_document(msg)
--		prans = 'Receiving Document ('..loadans..')\n"'..'"'
--		print(prans)
--		send_info_to_server(msg,loadans)
--		
	--elseif msg.media.type == 'photo' then
--		print('Loading photo'..msg.id.."\n")
--
--		local loadans = load_photo(msg)
--
--		print('Receiving Photo:[')
--		print(loadans)
--		print(']')
--		send_info_to_server(msg,loadans)
		--
--	else
--		--sfile = false
--		send_info_to_server(msg,false)
--	end
end

function on_our_id (id)
end

function on_secret_chat_created (peer)
end

function on_user_update (user)
end

function on_chat_update (user)
end

function on_get_difference_end ()
end

function on_binlog_replay_end ()
end