-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.lua
277 lines (237 loc) · 9.1 KB
/
bot.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
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 = "364257894:AAFs7Q3XZZ6R3KIZ3xuzefzfyNi7-Dv_tBE" --pit your api key of api telegram bot
local BASE_URL = "https://api.telegram.org/bot"..bot_api_key
-- Base folder like
-- local BASE_FOLDER = "/home/imandaneshi/files/"
local BASE_FOLDER = ""
-------
----utilites----
function is_admin(msg)-- Check if user is admin or not
local var = false
local admins = {158955285 , 180191663}-- put your id and cli pika launcher bot
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
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.." \n"
print(bot_info)
last_update = last_update or 0
is_running = true
currect_folder = ""
end
function msg_processor(msg)
if msg == nil then return end
if not is_admin(msg) then return end-- Admins only !
if msg.date < os.time() - 5 then -- Ignore old msgs
return
end
if msg.text then
if msg.text:match("^/ls$") then
local action = io.popen('ls "'..BASE_FOLDER..currect_folder..'"'):read("*all")
sendMessage(msg.chat.id, action)
end
if msg.text:match("^/cd (.*)$") then
local matches = { string.match(msg.text, "^/cd (.*)$") }
currect_folder = matches[1]
sendMessage(msg.chat.id, "Currect folder = "..BASE_FOLDER..currect_folder)
end
if msg.text:match("^/mkdir (.*)$") then
local matches = { string.match(msg.text, "^/mkdir (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && mkdir \''..matches[1]..'\''):read("*all")
sendMessage(msg.chat.id, "Created folder ")
end
if msg.text:match("^/rm (.*)$") then
local matches = { string.match(msg.text, "^/rm (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && rm -f \''..matches[1]..'\''):read("*all")
sendMessage(msg.chat.id, "Deleted "..matches[1])
end
if msg.text:match("^/cat (.*)$") then
local matches = { string.match(msg.text, "^/cat (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && cat \''..matches[1]..'\''):read("*all")
sendMessage(msg.chat.id, action)
end
if msg.text:match("^/rmdir (.*)$") then
local matches = { string.match(msg.text, "^/rmdir (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && rmdir \''..matches[1]..'\''):read("*all")
sendMessage(msg.chat.id, "Deleted "..matches[1])
end
if msg.text:match("^/touch (.*)$") then
local matches = { string.match(msg.text, "^/touch (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && touch \''..matches[1]..'\''):read("*all")
sendMessage(msg.chat.id, "Created file "..matches[1])
end
if msg.text:match("^/tofile ([^%s]+) (.*)$") then
local matches = { string.match(msg.text, "^/tofile ([^%s]+) (.*)$") }
local file = io.open(BASE_FOLDER..currect_folder..matches[1], "w")
file:write(matches[2])
file:flush()
file:close()
sendMessage(msg.chat.id, "Done !")
end
if msg.text:match("^/shell (.*)$") then
local matches = { string.match(msg.text, "^/shell (.*)$") }
local text = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && '..matches[1]:gsub('—', '--')):read('*all')
sendMessage(msg.chat.id, text)
end
if msg.text:match("^/cp (.*) (.*)$") then
local matches = { string.match(msg.text, "^/cp (.*) (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && cp -r \''..matches[1]..'\' \''..matches[2]..'\''):read("*all")
sendMessage(msg.chat.id, "Copied file "..matches[1])
end
if msg.text:match("^/mv (.*) (.*)$") then
local matches = { string.match(msg.text, "^/mv (.*) (.*)$") }
local action = io.popen('cd "'..BASE_FOLDER..currect_folder..'" && mv \''..matches[1]..'\' \''..matches[2]..'\''):read("*all")
sendMessage(msg.chat.id, "Moved file "..matches[1])
end
if msg.text:match("^/upload (.*)$") then
local matches = { string.match(msg.text, "^/upload (.*)$") }
if io.popen('find '..BASE_FOLDER..currect_folder..matches[1]):read("*all") == '' then
sendMessage(msg.chat.id, "File does not exist")
else
sendMessage(msg.chat.id, "Uploading file "..matches[1])
sendDocument(msg.chat.id, matches[1])
end
end
if msg.text:match("^/download (.*)$") or msg.text:match("^/download$") then--Turn your bot privacy off or it won't work on chats(groups and supergroups)
if not msg.reply_to_message then
sendMessage(msg.chat.id, "Reply to something !")
return
end
local file = ""
local filename = ""
if msg.reply_to_message.photo then
file = msg.reply_to_message.photo[1].file_id
filename = "somepic.jpg"
elseif msg.reply_to_message.video then
file = msg.reply_to_message.video.file_id
filename = "somevideo.mp4"
if msg.reply_to_message.video.file_size > 19000000 then
return
end
elseif msg.reply_to_message.document then
file = msg.reply_to_message.document.file_id
filename = msg.reply_to_message.document.file_name
if msg.reply_to_message.document.file_size > 19000000 then
return
end
elseif msg.reply_to_message.audio then
filename = msg.reply_to_message.audio.title..".mp3"
file = msg.reply_to_message.audio.file_id
elseif msg.reply_to_message.sticker then
filename = "somevoice.png"
file = msg.reply_to_message.sticker.file_id
elseif msg.reply_to_message.voice then
filename = "somevoice.ogg"
file = msg.reply_to_message.voice.file_id
else
return
end
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
if string.match(msg.text, "^/download (.*)$") then
local matches = { string.match(msg.text, "^/download (.*)$") }
filename = matches[1]
end
local download = download_to_file("https://api.telegram.org/file/bot"..bot_api_key.."/"..jres.result.file_path, filename)
sendMessage(msg.chat.id, "file downloaded")
end
end
return
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")