-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbot.lua
More file actions
475 lines (401 loc) · 12.9 KB
/
bot.lua
File metadata and controls
475 lines (401 loc) · 12.9 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
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')
dofile('utilities.lua')
----config----
local bot_api_key = "" --BOT TOKEN تو کن ربات خود را در اینجا قرار دهید
local You = 188548712 --ID ADMIN ایدی خود را اینجا قرار دهید
local BASE_URL = "https://api.telegram.org/bot"..bot_api_key
local BASE_FOLDER = ""
local start = [[ ]]
-------
----utilites----
local help = [[
➖➖➖➖➖➖➖➖➖➖➖
*commands:*`for admin`
`/ban` ✴️
بن کردن یک شخص
`/unban` ✴️
ان بن کردن یک شخص
`/users` ✴️
تعداد کاربران
`/broadcast` ✴️
شروع پیام همگانی
`/unbroadcast` ✴️
پایان ارسال پیام همگانی
`/start` ✴️
شروع
`/id` ✴️
ایدی
➖➖➖➖➖➖➖➖➖➖➖
M.KH @cruel0098
]]--
-------
----utilites----
function is_admin(msg)-- Check if user is admin or not
local var = false
local admins = {You}
for k,v in pairs(admins) do
if msg.from.id == v then
var = true
end
end
return var
end
function is_add(msg)-- Check if user is ADD or not
local var = false
local add = add.id
for k,v in pairs(add) do
if msg.from.id == v then
var = true
end
end
return var
end
function is_ban(msg)-- Check if user is ban or not
local var = false
local add = ban.id
for k,v in pairs(add) 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()
local url = BASE_URL .. '/getMe'
return sendRequest(url)
end
function getUpdates(offset)
local url = BASE_URL .. '/getUpdates?timeout=20'
if offset then
url = url .. '&offset=' .. offset
end
return sendRequest(url)
end
function sendSticker(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
function sendPhoto(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
function forwardMessage(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)
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)
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)
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 sendPhotoID(chat_id, file_id, caption, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendPhoto?chat_id=' .. chat_id .. '&photo=' .. file_id
if caption then
url = url .. '&caption=' .. URL.escape(caption)
end
if reply_to_message_id then
url = url .. '&reply_to_message_id=' .. reply_to_message_id
end
if disable_notification then
url = url .. '&disable_notification=true'
end
return sendRequest(url)
end
function getUserProfilePhotos(user_id)
if user_id == nil then
return
else
local url = BASE_URL .. '/getUserProfilePhotos?user_id='..user_id
print(url)
return sendRequest(url)
end
end
function download_to_file(url, file_name, file_path)
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 = file_name
print("Saved to: "..file_path)
file = io.open(file_path, "w+")
file:write(table.concat(respbody))
file:close()
return file_path
end
-------- @cruel0098 M.KH
function bot_run()
bot = nil
while not bot do -- Get bot info
bot = getMe()
end
bot = bot.result
if not add then
add = load_data('mico.db')
end
if not ban then
ban = load_data('ban.db')
end
local bot_info = "Username = @"..bot.username.."\nName = "..bot.first_name.."\nID = "..bot.id.." \n[jon the cruel channel](https://telegram.me/joinchat/Cz0GaD5yj9_VLaNXOOg3ag)"
print(bot_info)
for k,v in pairs(add.id) do
print(k.." :"..v)
end
last_update = last_update or 0
currect_folder = ""
is_running = true
math.randomseed(os.time())
math.random()
last_cron = last_cron or os.date('%M', os.time()) -- the time of the last cron job,
is_started = true -- and whether or not the bot should be running.
add.id = add.id or {} --TABLE FUCKERRRRRRRRRRRRRRRRRRRRRRRRRRR
ban.id = ban.id or {}
add.broadcast = add.broadcast or {} --
end
function msg_processor(msg)
if msg.date < os.time() - 5 then -- Ignore old msgs
return
end
if msg.text:match('/p (.*)')then
local matches = {string.match(msg.text,('/p (.*)'))}
local input = get_word(matches[1])
local lid = resolve_username(input)
local phl = getUserProfilePhotos(tonumber(lid))
local file = phl.result.photos[1][1].file_id
print(file)
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
local caption = "his id :- "..lid
sendPhotoID(msg.chat.id,file,caption)
elseif msg.reply_to_message and msg.text == '/p' then
local lid = msg.reply_to_message.from.id
local phl = getUserProfilePhotos(tonumber(lid))
local file = phl.result.photos[1][1].file_id
print(file)
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
local caption = "his id :- "..lid
sendPhotoID(msg.chat.id,file,caption)
elseif msg.text == '/p' then
local ph = getUserProfilePhotos(msg.from.id)
local file = ph.result.photos[1][1].file_id
local url = BASE_URL .. '/getFile?file_id='..file
local res = HTTPS.request(url)
local jres = JSON.decode(res)
if msg.from.username ~= nil then
msg.from.username = '@'..msg.from.username
elseif msg.from.username == nil then
msg.from.username = "you don't have"
end
local caption = 'your nam :- '..msg.from.first_name..'\nyour id :-'..msg.from.id..'\nyour username :- '..msg.from.username
sendPhotoID(msg.chat.id,file,caption)
elseif msg.text:match('/id (.*)$') then
local matches = {string.match(msg.text,('/id (.*)'))}
local input = get_word(matches[1])
id = resolve_username(input)
sendMessage(msg.chat.id,id)
elseif msg.text:match('/w (.*) (.*)') then
local matches = {string.match(msg.text,('/w (.*) (.*)'))}
local lt = matches[1]:gsub("_"," ")
local lr = matches[2]:gsub("_"," ")
add.talk[lt]= lr
elseif msg.text and add.talk[msg.text] ~= nil then
local ion = add.talk[msg.text]
local out = ion:gsub("_"," ")
sendMessage(msg.chat.id,out,true,msg.message_id)
elseif msg.from.username then
add.usernames[msg.from.username:lower()] = msg.from.id
end
end
function msg_processor(msg)
if msg.text == "/start" and not is_add(msg) then
table.insert(add.id,msg.from.id)
print("adding.....")
local user = ""
if msg.from.username == nil then
user = bot.username
else
user = msg.from.username
end
local text = "سلام ["..msg.from.first_name.."](www.telegram.me/"..user..")\n\n[ربات خود را بسازید](http://opizo.com/3AGyRT)"
sendMessage(msg.chat.id,text.."\n"..start,true,false,true)
elseif msg.text == "/start" and is_add(msg) then
print(#add.id)
local user = ""
if msg.from.username == nil then
user = bot.username
else
user = msg.from.username
end
local text = "سلام ["..msg.from.first_name.."](www.telegram.me/"..user..")\n\n[ربات خود را بسازید](http://opizo.com/3AGyRT)"
sendMessage(msg.chat.id,text.."\n"..start,true,false,true)
elseif is_admin(msg) and msg.text == "/users" then
local r = tostring(#add.id)
local t = string.gsub(r," ","")
sendMessage(You,t,true,false,true)
end
if msg.text == '/help' then
sendMessage(msg.chat.id,help,true,false,true)
end
if is_ban(msg) then return end
if msg.date < os.time() - 5 then -- Ignore old msgs
return
end
if msg.text == "/unbroadcast" then
add.broadcast = "broadcast"
save_data('mico.db', add)
sendMessage(msg.chat.id,"*I will not broadcast any more*",true,false,true)
end
if msg.text ~= "/broadcast" and add.broadcast ~= "broadcast" then
if is_admin(msg) then
if msg.text and msg.text ~= "/unbroadcast" then
for k,v in pairs(add.id) do
sendMessage(v,msg.text,true,false,true)
end
elseif not msg.text then
for k,v in pairs(add.id) do
forwardMessage(v,You,msg.message_id)
end
end
end
elseif msg.text ~= "/" then
if is_admin(msg) and msg.reply_to_message and msg.reply_to_message.forward_from ~= nil and msg.text == "/ban" then
table.insert(ban.id,msg.reply_to_message.forward_from.id)
sendMessage(msg.reply_to_message.forward_from.id,"*YOU HAVE BEEN BANNED*",true,false,true)
elseif is_admin(msg) and msg.reply_to_message and msg.reply_to_message.forward_from ~= nil and msg.text == "/unban" then
for k, v in pairs(ban["id"]) do
if ( v == msg.reply_to_message.forward_from.id ) then
table.remove(ban["id"], k)
end
end
sendMessage(msg.reply_to_message.forward_from.id,"*YOU HAVE BEEN unBANNED*",true,false,true)
elseif is_admin(msg) and msg.reply_to_message and msg.text == "/id" then
if msg.reply_to_message.forward_from.last_name ~= nil then
last_name = msg.reply_to_message.forward_from.last_name
else
last_name = ""
end
if msg.reply_to_message.forward_from.username ~= nil then
usernme = "\nUSERNAME : @"..msg.reply_to_message.forward_from.username
else
usernme = ""
end
local E = "NAME : "..msg.reply_to_message.forward_from.first_name.." "..last_name..usernme.."\nID :"..msg.reply_to_message.forward_from.id
sendMessage(msg.chat.id,E)
elseif is_admin(msg) and msg.text == "/broadcast" then
add.broadcast = "unbroadcast"
sendMessage(msg.chat.id,"*I will send every thing u want*",true,false,true)
save_data('mico.db', add)
elseif not is_ban(msg) and msg.text ~= "/start" and msg.text ~= "/id" and msg.text ~= "/unban" and msg.text ~= "/ban" then
if is_admin(msg) and msg.reply_to_message then
forwardMessage(msg.reply_to_message.forward_from.id,msg.from.id,msg.message_id)
--print(msg.from.id)
print(msg.reply_to_message.from.id)
print(msg.reply_to_message.forward_from.id)
--print(msg.reply_to_message.from.id)
print(msg.reply_to_message.message_id)
elseif not is_admin(msg) then
forwardMessage(You,msg.chat.id,msg.message_id)
end
end
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
if last_cron ~= os.date('%M', os.time()) then -- Run cron jobs every minute.
last_cron = os.date('%M', os.time())
save_data('mico.db', add) -- Save the database.
save_data('ban.db', ban)
end
end
save_data('mico.db', add)
save_data('ban.db', ban)
print("Bot halted")