forked from yagop/telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisX.lua
72 lines (66 loc) · 1.98 KB
/
isX.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
local https = require "ssl.https"
local ltn12 = require "ltn12"
local function request(imageUrl)
-- Edit data/mashape.lua with your Mashape API key
-- http://docs.mashape.com/api-keys
local mashape = load_from_file('data/mashape.lua', {
api_key = ''
})
local api_key = mashape.api_key
if api_key:isempty() then
return nil, 'Configure your Mashape API Key'
end
local api = "https://sphirelabs-advanced-porn-nudity-and-adult-content-detection.p.mashape.com/v1/get/index.php?"
local parameters = "&url="..(URL.escape(imageUrl) or "")
local url = api..parameters
local respbody = {}
local headers = {
["X-Mashape-Key"] = api_key,
["Accept"] = "Accept: application/json"
}
print(url)
local body, code, headers, status = https.request{
url = url,
method = "GET",
headers = headers,
sink = ltn12.sink.table(respbody),
protocol = "tlsv1"
}
if code ~= 200 then return "", code end
local body = table.concat(respbody)
return body, code
end
local function parseData(data)
local jsonBody = json:decode(data)
local response = ""
print(data)
if jsonBody["Error Occured"] ~= nil then
response = response .. jsonBody["Error Occured"]
elseif jsonBody["Is Porn"] == nil or jsonBody["Reason"] == nil then
response = response .. "I don't know if that has adult content or not."
else
if jsonBody["Is Porn"] == "True" then
response = response .. "Beware!\n"
end
response = response .. jsonBody["Reason"]
end
return jsonBody["Is Porn"], response
end
local function run(msg, matches)
local data, code = request(matches[1])
if code ~= 200 then return "There was an error. "..code end
local isPorn, result = parseData(data)
return result
end
return {
description = "Does this photo contain adult content?",
usage = {
"!isx [url]",
"!isporn [url]"
},
patterns = {
"^!is[x|X] (.*)$",
"^!is[p|P]orn (.*)$"
},
run = run
}