forked from TiagoDanin/SiD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.lua
209 lines (164 loc) · 4.34 KB
/
utilities.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
-- utilities.lua
-- Functions shared among plugins.
function get_word(s, i) -- get the indexed word in a string
local t = {}
for w in s:gmatch('%g+') do
table.insert(t, w)
end
return t[i] or false
end
function markdown_url(title, url) -- Filter Markdown
local markdown = '['.. HTML.decode(title:gsub('%[.+%]', ''):gsub('%(.+%)', ''):gsub('&.-;', '')) ..']('.. url .. ')'
return markdown
end
function inline_block(title, text) -- Inline Block
local ran = math.random(1 ,100)
local inline = '{"type":"article", "id":"'.. ran ..'", "title":"'.. title ..'", "message_text": "'.. text ..'", "parse_mode":"Markdown"}'
return inline
end
function sendLang(text, lang)
lg = dofile("lang/lang.lua")
local l = string.gsub(text, "$.-*", lg[tostring(lang)])
return l
end
function string:input() -- Returns the string after the first space.
if not self:find(' ') then
return false
end
return self:sub(self:find(' ')+1)
end
-- I swear, I copied this from PIL, not yago! :)
function string:trim() -- Trims whitespace from a string.
local s = self:gsub('^%s*(.-)%s*$', '%1')
return s
end
local lc_list = {
-- Latin = 'Cyrillic'
['A'] = 'А',
['B'] = 'В',
['C'] = 'С',
['E'] = 'Е',
['I'] = 'І',
['J'] = 'Ј',
['K'] = 'К',
['M'] = 'М',
['H'] = 'Н',
['O'] = 'О',
['P'] = 'Р',
['S'] = 'Ѕ',
['T'] = 'Т',
['X'] = 'Х',
['Y'] = 'Ү',
['a'] = 'а',
['c'] = 'с',
['e'] = 'е',
['i'] = 'і',
['j'] = 'ј',
['o'] = 'о',
['s'] = 'ѕ',
['x'] = 'х',
['y'] = 'у',
['!'] = 'ǃ'
}
function latcyr(str) -- Replaces letters with corresponding Cyrillic characters.
for k,v in pairs(lc_list) do
str = string.gsub(str, k, v)
end
return str
end
load_data = function(filename)
local f = io.open('data/'..filename)
if not f then
return {}
end
local s = f:read('*all')
f:close()
local data = JSON.decode(s)
return data
end
-- Saves a table to a JSON file.
save_data = function(filename, data, mode)
local s = JSON.encode(data)
if not mode then
mode = 'w'
end
local f = io.open('data/'..filename, mode)
f:write(s)
f:close()
end
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua.
get_coords = function(input)
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
if res ~= 200 then
return config.errors.connection
end
local jdat = JSON.decode(jstr)
if jdat.status == 'ZERO_RESULTS' then
return config.errors.results
end
return {
lat = jdat.results[1].geometry.location.lat,
lon = jdat.results[1].geometry.location.lng,
acc = jdat.results[1].geometry.location_type,
types= jdat.results[1].types,
}
end
-- Get the number of values in a key/value table.
table_size = function(tab)
local i = 0
for k,v in pairs(tab) do
i = i + 1
end
return i
end
resolve_username = function(target)
-- If $target is a known username, returns associated ID.
-- If $target is an unknown username, returns nil.
-- If $target is a number, returns that number.
-- Otherwise, returns false.
local input = tostring(target):lower()
if input:match('^@') then
local uname = input:gsub('^@', '')
return usernames[uname]
else
return tonumber(target) or false
end
end
handle_exception = function(err, message)
if not err then err = '' end
local output = err .. '\n' .. message .. '\n'
if config.debug and config.debug.chat then
sendMessage(config.debug.chat, '*[' .. os.date('%F %T', os.time()) .. ']* ' .. bot.username .. ':', true, nil, true)
sendMessage(config.debug.chat, output, false, false, false)
else
print('!!!ERRO!!! ' .. os.date('%F %T', os.time()) .. ' LOG CHAT\n'..output..'\n\n')
end
end
-- Okay, this one I actually did copy from yagop.
-- https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
download_file = function(url, filename)
local respbody = {}
local options = {
url = url,
sink = ltn12.sink.table(respbody),
redirect = true
}
local response = nil
if url:match('^https') then
options.redirect = false
response = { HTTPS.request(options) }
else
response = { HTTP.request(options) }
end
local code = response[2]
local headers = response[3]
local status = response[4]
if code ~= 200 then return false end
filename = filename or os.time()
local file_path = 'data/'..filename
file = io.open(file_path, 'w+')
file:write(table.concat(respbody))
file:close()
return file_path
end