Skip to content

Instantly share code, notes, and snippets.

@0BRWUEN0
Created February 22, 2017 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0BRWUEN0/daf3c4d16b1aa43f75e1fb36a04cbdab to your computer and use it in GitHub Desktop.
Save 0BRWUEN0/daf3c4d16b1aa43f75e1fb36a04cbdab to your computer and use it in GitHub Desktop.
package.path = package.path .. ';.luarocks/share/lua/5.2/?.lua'
..';.luarocks/share/lua/5.2/?/init.lua'
package.cpath = package.cpath .. ';.luarocks/lib/lua/5.2/?.so'
URL = require('socket.url')
JSON = require('dkjson')
HTTPS = require('ssl.https')
----config----
local bot_api_key = "366405814:AAG5uAI2FY2gqjQ_WwC8qp37xYPqtWhBVSo" --👈put token in "heretoken"
local BASE_URL = "https://api.telegram.org/bot"..bot_api_key
local BASE_FOLDER = ""
local start = [[ انا روبوت التلغرام
احول الصيغ
احول الصوره الى ملصق
احول الملصق الى صوره
احول الصوره المتحركه الى فيديو
احول البصمات الى ملف mp3
فقط ارسل لي الملف الذي تريد تحويله
[Developer](https://telegram.me/dev_90) 🕵
]]
-------
----utilites----
function is_admin(msg)
local var = false
local admins = {141657340}-- ايدي
for k,v in pairs(admins) do
if msg.from.id == v then
var = true
end
end
return var
end
function sendRequest(url)
local dat, res = HTTPS.request(url)
local tab = JSON.decode(dat)
if res ~= 200 then
return false, res
end
if not tab.ok then
return false, tab.description
end
return tab
end
function getMe()--https://core.telegram.org/bots/api#getfile
local url = BASE_URL .. '/getMe'
return sendRequest(url)
end
function getUpdates(offset)--https://core.telegram.org/bots/api#getupdates
local url = BASE_URL .. '/getUpdates?timeout=20'
if offset then
url = url .. '&offset=' .. offset
end
return sendRequest(url)
end
sendSticker = function(chat_id, sticker, reply_to_message_id)
local url = BASE_URL .. '/sendSticker'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "sticker=@' .. sticker .. '"'
if reply_to_message_id then
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
end
io.popen(curl_command):read("*all")
return end
sendPhoto = function(chat_id, photo, caption, reply_to_message_id)
local url = BASE_URL .. '/sendPhoto'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "photo=@' .. photo .. '"'
if reply_to_message_id then
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
end
if caption then
curl_command = curl_command .. ' -F "caption=' .. caption .. '"'
end
io.popen(curl_command):read("*all")
return end
forwardMessage = function(chat_id, from_chat_id, message_id)
local url = BASE_URL .. '/forwardMessage?chat_id=' .. chat_id .. '&from_chat_id=' .. from_chat_id .. '&message_id=' .. message_id
return sendRequest(url)
end
function sendMessage(chat_id, text, disable_web_page_preview, reply_to_message_id, use_markdown)--https://core.telegram.org/bots/api#sendmessage
local url = BASE_URL .. '/sendMessage?chat_id=' .. chat_id .. '&text=' .. URL.escape(text)
if disable_web_page_preview == true then
url = url .. '&disable_web_page_preview=true'
end
if reply_to_message_id then
url = url .. '&reply_to_message_id=' .. reply_to_message_id
end
if use_markdown then
url = url .. '&parse_mode=Markdown'
end
return sendRequest(url)
end
function sendDocument(chat_id, document, reply_to_message_id)--https://github.com/topkecleon/otouto/blob/master/bindings.lua
local url = BASE_URL .. '/sendDocument'
local curl_command = 'cd \''..BASE_FOLDER..currect_folder..'\' && curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "document=@' .. document .. '"'
if reply_to_message_id then
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
end
io.popen(curl_command):read("*all")
return
end
function download_to_file(url, file_name, file_path)--https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
print("url to download: "..url)
local respbody = {}
local options = {
url = url,
sink = ltn12.sink.table(respbody),
redirect = true
}
-- nil, code, headers, status
local response = nil
options.redirect = false
response = {HTTPS.request(options)}
local code = response[2]
local headers = response[3]
local status = response[4]
if code ~= 200 then return nil end
local file_path = BASE_FOLDER..currect_folder..file_name
print("Saved to: "..file_path)
file = io.open(file_path, "w+")
file:write(table.concat(respbody))
file:close()
return file_path
end
--------
function bot_run()
bot = nil
while not bot do -- Get bot info
bot = getMe()
end
bot = bot.result
local bot_info = "Username = @"..bot.username.."\nName = "..bot.first_name.."\nId = "..bot.id.." \nbased on linux-file-manager :D\nthx to @imandaneshi\neditor: @@unfriendly\nDeveloper @amody6"
print(bot_info)
last_update = last_update or 0
is_running = true
currect_folder = ""
end
function msg_processor(msg)
if msg.new_chat_participant or msg.new_chat_title or msg.new_chat_photo or msg.left_chat_participant then return end
if msg.audio or msg.document or msg.video or msg.voice then return end -- Admins only !
if msg.date < os.time() - 5 then -- Ignore old msgs
return
end
if msg.sticker then
local matches = { (msg.sticker) }
file = msg.sticker.file_id
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
filename = "sticker.png"
file = download_to_file("https://api.telegram.org/file/bot"..bot_api_key.."/"..jres.result.file_path, filename)
sendPhoto(msg.chat.id, file)
elseif msg.photo then
local matches = { (msg.photo) }
file = msg.photo[3].file_id
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
filename = "photo.jpg"
file = download_to_file("https://api.telegram.org/file/bot"..bot_api_key.."/"..jres.result.file_path, filename)
sendSticker(msg.chat.id, file)
elseif msg.audio then
local matches = { (msg.audio) }
file = msg.audio.file_id
local url = BASE_URL .. '/getfile?
file id =' ..file
local res = HTTS.request(url)
local jres = JSON.decode(res)
filename = "iq_team.mp3"
file = download_to_file("https://api.telegram.org/file/bot".. bot_api_key.."/"..jres.result.file_path, filename)
sendAudio(msg.chat.id, file)
elseif msg.gif then
local matches = { (msg.gif) }
file = msg.gif.file_id
local url = BASE_URL .. '/getfile?
file id =' ..file
local res = HTTS.request(url)
local jres = JSON.decode(res)
filename = "iq_team.mp4"
file = download_to_file("https://api.telegram.org/file/bot".. bot_api_key.."/"..jres.result.file_path, filename)
sendGif(msg.chat.id, file)
if msg.text then return end
elseif msg.text:match("^/[sS]tart") or msg.text:match("^/[Hh]elp") then
sendMessage(msg.chat.id, start, true, false, true)
return end
end
bot_run() -- Run main function
while is_running do -- Start a loop witch receive messages.
local response = getUpdates(last_update+1) -- Get the latest updates using getUpdates method
if response then
for i,v in ipairs(response.result) do
last_update = v.update_id
msg_processor(v.message)
end
else
print("Conection failed")
end
end
print("Bot halted")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment