-
Notifications
You must be signed in to change notification settings - Fork 0
/
luafather.lua
223 lines (216 loc) · 6.07 KB
/
luafather.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
local lru = require("lru")
local ltn12 = require("ltn12")
local cjson = require("cjson")
local unpack = table.unpack or unpack
local function request(self, method, chat_id, data)
data = data or {}
local http = require(self.http)
local url = string.format(self.api, self.token, string.gsub(method, "_([^_]+)", "%1"))
if chat_id and data.chat_id == nil then
data.chat_id = chat_id
end
local body = cjson.encode(data)
local headers = {
["content-type"] = "application/json",
["content-length"] = #body
}
if self.headers then
for key, value in pairs(self.headers) do
headers[string.lower(key)] = value
end
end
local out = {}
local source = ltn12.source.string(body)
local sink = ltn12.sink.table(out)
local _, status = http.request({
sink = sink,
source = source,
url = url,
headers = headers
})
local response = table.concat(out)
if status ~= 200 then
return nil, response, status
end
local ok, result = pcall(cjson.decode, response)
if ok and type(result) == "table" then
if result.ok and result.result then
return result.result, result
else
return false, result, result.error_code
end
end
return nil, result
end
local function trigger(self, keys)
return setmetatable({}, {
__call = function(_, ...)
local value = ...
if keys.n == 1 and value == self then
return request(self, keys[1], _, select(2, ...))
end
self.triggers[#self.triggers + 1] = {keys, value}
return self.triggers[#self.triggers]
end,
__index = function(_, key)
keys.n = keys.n + 1
keys[keys.n] = key
return trigger(self, keys)
end
})
end
local function match(values, keys, matches, current)
if current > #keys then
return values, unpack(matches, 1, matches.n)
end
local key = keys[current]
current = current + 1
if type(key) == "table" then
local source = key
key = function(options)
if type(options) == "table" then
for index, value in pairs(source) do
local option = options[value]
if option and type(index) == "number" then
return option, value
else
option = options[index]
if option == value then
return option, index
elseif type(value) == "table" then
for current = 1, #value do
if option == value[current] then
return option, index
end
end
end
end
end
end
end
end
if type(key) == "function" then
local value = (function(...)
local total = select("#", ...)
if total == 0 then
return
end
for index = 2, total do
matches[matches.n + index - 1] = select(index, ...)
end
matches.n = matches.n + total - 1
return ...
end)(key(values, unpack(matches, 1, matches.n)))
if value ~= nil then
return match(value, keys, matches, current)
end
else
if type(values) == "table" then
local value = values[key]
if value then
return match(value, keys, matches, current)
end
end
end
end
local function object(self, value, chat_id)
return setmetatable({}, {
value = value,
__index = function(this, key)
local index = rawget(getmetatable(this).value, key)
if index then
return index
end
return function(parent, ...)
if parent == this then
request(self, key, chat_id, select(2, ...))
return parent
end
end
end,
__call = function()
return coroutine.yield()
end
})
end
local function session(self, value, id)
return setmetatable({}, {
__call = function(_, ...)
local chat_id, fn = ...
if type(chat_id) == "function" then
fn = chat_id
chat_id = id
end
local session = self.sessions:get(tostring(chat_id))
if session then
local thread = session.thread
local status = coroutine.status(thread)
if status == "suspended" then
coroutine.resume(thread, session.chat)
elseif status == "dead" then
self.sessions:delete(tostring(chat_id))
session = nil
end
end
if not session then
session = {
id = tostring(chat_id),
thread = coroutine.create(fn),
chat = object(self, value, chat_id)
}
self.sessions:set(tostring(chat_id), session)
coroutine.resume(session.thread, session.chat)
end
end,
__index = value,
__tostring = function()
return cjson.encode(value)
end
})
end
return function(...)
local self = {}
self.token, self.options = ...
if type(self.token) == "table" then
self.options = self.token
self.token = self.options.token
end
assert(type(self.token) == "string", "bot token required")
self.options = self.options or {}
self.cache = self.options.cache or 64
self.http = self.options.http or (_G.ngx and "lapis.nginx.http" or "ssl.https")
self.api = self.options.api or "https://api.telegram.org/bot%s/%s"
self.headers = self.options.headers or {}
self.triggers = {}
self.sessions = lru.new(self.cache)
return setmetatable(self, {
__index = function(this, key)
local index = rawget(this, key)
if index then
return index
end
return trigger(this, {key, n = 1})
end,
__call = function(_, value)
if type(value) == "function" then
self.triggers[#self.triggers + 1] = {{n = 0}, value}
elseif type(value) == "table" then
for _, trigger in pairs(self.triggers) do
local values
if (function(...)
if select("#", ...) > 0 then
values = (function(...)
return {n = select("#", ...), ...}
end)(trigger[2](session(self, value, ...), ...))
if values.n > 0 then
return true
end
end
end)(match(value, trigger[1], {n = 0}, 1)) then
return unpack(values, 1, values.n)
end
end
end
end
})
end