This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/bot/bot.lua

532 lines
13 KiB
Lua
Raw Normal View History

2014-07-02 14:30:18 +02:00
http = require("socket.http")
2014-09-25 15:24:52 +02:00
URL = require("socket.url")
2014-07-02 14:30:18 +02:00
json = (loadfile "./bot/JSON.lua")()
2014-10-10 15:25:01 +02:00
VERSION = 'v0.5'
2014-06-30 12:42:28 +02:00
function on_msg_receive (msg)
if msg_valid(msg) == false then
return
end
-- Check if command starts with ! eg !echo
if msg.text:sub(0,1) == '!' then
do_action(msg)
else
2014-09-24 15:31:25 +02:00
if is_image_url(msg.text) then
send_image_from_url (msg)
2014-10-11 13:54:43 +02:00
elseif is_youtube_url(msg.text) then
send_youtube_thumbnail(msg.text)
2014-09-24 15:31:25 +02:00
else
if is_file_url(msg.text) then
2014-10-11 13:54:43 +02:00
send_file_from_url(msg)
2014-09-24 15:31:25 +02:00
end
end
end
mark_read(get_receiver(msg), ok_cb, false)
-- write_log_file(msg)
end
function ok_cb(extra, success, result)
end
function msg_valid(msg)
2014-06-30 12:42:28 +02:00
if msg.out then
return false
2014-06-30 12:42:28 +02:00
end
2014-07-01 22:06:40 +02:00
if msg.date < now then
return false
2014-07-01 22:06:40 +02:00
end
2014-06-30 12:42:28 +02:00
if msg.text == nil then
return false
2014-06-30 12:42:28 +02:00
end
if msg.unread == 0 then
return false
end
end
2014-09-24 15:31:25 +02:00
function send_file_from_url (msg)
last = string.get_last_word(msg.text)
2014-09-25 23:28:04 +02:00
file = download_to_file(last)
2014-09-24 15:31:25 +02:00
send_document(get_receiver(msg), file, ok_cb, false)
end
function send_image_from_url (msg)
last = string.get_last_word(msg.text)
2014-09-25 23:28:04 +02:00
file = download_to_file(last)
send_photo(get_receiver(msg), file, ok_cb, false)
2014-06-30 12:42:28 +02:00
end
function is_image_url(text)
last = string.get_last_word(text)
extension = string.get_extension_from_filename(last)
2014-09-24 15:31:25 +02:00
if extension == 'jpg' or extension == 'png' then
return true
end
return false
end
2014-10-11 13:54:43 +02:00
function is_youtube_url(text)
-- http://stackoverflow.com/questions/19377262/regex-for-youtube-url
not_full_yt_url = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]*)") == nil
not_short_yt_url = string.match(text, "youtu.be/([A-Za-z0-9-]*)") == nil
yt = full_yt_url or short_yt_url
return yt
end
function send_youtube_thumbnail(msg)
yt_thumbnail = "http://img.youtube.com/vi/".. string.match(msg.text, "([A-Za-z0-9-]*)").."/hqdefault.jpg"
file = download_to_file(yt_thumbnail)
send_photo(get_receiver(msg), file, ok_cb, false)
end
2014-09-24 15:31:25 +02:00
function is_file_url(text)
last = string.get_last_word(text)
extension = string.get_extension_from_filename(last)
if extension == 'gif' then
return true
end
return false
end
2014-07-01 22:06:40 +02:00
-- Where magic happens
2014-06-30 12:42:28 +02:00
function do_action(msg)
local receiver = get_receiver(msg)
2014-08-05 14:07:25 +02:00
if string.starts(msg.text, '!sh') then
2014-07-04 13:41:44 +02:00
text = run_sh(msg)
send_msg(receiver, text, ok_cb, false)
return
2014-07-04 13:41:44 +02:00
end
if string.starts(msg.text, '!uc3m') then
2014-07-03 21:18:07 +02:00
text = get_fortunes_uc3m()
send_msg(receiver, text, ok_cb, false)
return
2014-07-03 21:18:07 +02:00
end
2014-09-25 15:24:52 +02:00
if string.starts(msg.text, '!img') then
2014-09-25 23:28:04 +02:00
text = msg.text:sub(6,-1)
2014-09-25 15:24:52 +02:00
url = getGoogleImage(text)
2014-09-25 23:28:04 +02:00
file_path = download_to_file(url)
print(file_path)
send_photo(receiver, file_path, ok_cb, false)
2014-09-25 15:24:52 +02:00
return
end
2014-10-04 17:02:29 +02:00
if string.starts(msg.text, '!rae') then
text = msg.text:sub(6,-1)
meaning = getDulcinea(text)
send_msg(receiver, meaning, ok_cb, false)
end
2014-10-11 13:54:43 +02:00
if string.starts(msg.text, '!9gag') then
url, title = get_9GAG()
file_path = download_to_file(url)
send_photo(receiver, file_path, ok_cb, false)
send_msg(receiver, title, ok_cb, false)
return
end
if string.starts(msg.text, '!fortune') then
2014-07-01 22:06:40 +02:00
text = run_bash('fortune')
send_msg(receiver, text, ok_cb, false)
return
2014-07-03 21:18:07 +02:00
end
if string.starts(msg.text, '!forni') then
text = msg.text:sub(8,-1)
send_msg('Fornicio_2.0', text, ok_cb, false)
return
end
if string.starts(msg.text, '!fwd') then
fwd_msg (receiver, msg.id, ok_cb, false)
return
2014-07-07 10:30:27 +02:00
end
if string.starts(msg.text, '!cpu') then
2014-07-02 14:30:18 +02:00
text = run_bash('uname -snr') .. ' ' .. run_bash('whoami')
2014-06-30 12:42:28 +02:00
text = text .. '\n' .. run_bash('top -b |head -2')
send_msg(receiver, text, ok_cb, false)
return
2014-06-30 12:42:28 +02:00
end
if string.starts(msg.text, '!ping') then
print('receiver: '..receiver)
send_msg (receiver, 'pong', ok_cb, false)
return
2014-06-30 12:42:28 +02:00
end
if string.starts(msg.text, '!weather') then
2014-07-07 10:30:27 +02:00
if string.len(msg.text) <= 9 then
city = 'Madrid,ES'
else
city = msg.text:sub(10,-1)
2014-07-07 10:30:27 +02:00
end
text = get_weather(city)
send_msg(receiver, text, ok_cb, false)
return
2014-07-02 14:30:18 +02:00
end
if string.starts(msg.text, '!echo') then
echo = msg.text:sub(7,-1)
send_msg(receiver, echo, ok_cb, false)
return
2014-06-30 12:42:28 +02:00
end
2014-10-09 15:09:06 +02:00
if string.starts(msg.text, '!eur') then
local eur = getEURUSD( )
send_msg(receiver, eur, ok_cb, false)
return
end
if string.starts(msg.text, '!version') then
2014-09-25 23:28:04 +02:00
text = 'James Bot '.. VERSION .. [[
Licencia GNU v2, código disponible en http://git.io/6jdjGg
Al Bot le gusta la gente solidaria.
Puedes hacer una donación a la ONG que decidas y ayudar a otras personas.]]
send_msg(receiver, text, ok_cb, false)
return
2014-06-30 12:42:28 +02:00
end
if string.starts(msg.text, '!help') then
2014-07-01 22:06:40 +02:00
text = [[!help : print this help
2014-06-30 12:42:28 +02:00
!ping : bot sends pong
2014-07-04 13:41:44 +02:00
!sh (text) : send commands to bash (only privileged users)
2014-07-02 14:30:18 +02:00
!echo (text) : echo the msg
2014-06-30 12:42:28 +02:00
!version : version info
2014-07-02 14:30:18 +02:00
!cpu : status (uname + top)
!fwd : forward msg
!forni : send text to group Fornicio
!fortune : print a random adage
2014-07-07 10:30:27 +02:00
!weather [city] : weather in that city (Madrid if not city)
2014-07-04 13:41:44 +02:00
!9gag : send random url image from 9gag
2014-10-10 15:25:01 +02:00
!rae (word): Spanish dictionary
!eur : EURUSD market value
2014-09-25 23:39:35 +02:00
!img (text) : search image with Google API and sends it
2014-08-05 14:07:25 +02:00
!uc3m : fortunes from Universidad Carlos III]]
send_msg(receiver, text, ok_cb, false)
return
2014-06-30 12:42:28 +02:00
end
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
2014-07-04 13:41:44 +02:00
function load_config()
local f = assert(io.open('./bot/config.json', "r"))
local c = f:read "*a"
local config = json:decode(c)
if config.torrent_path then
print ("!sh command is enabled")
for v,user in pairs(config.sudo_users) do
print("Allowed user: " .. user)
end
end
2014-07-07 10:30:27 +02:00
-- print("Torrent path: " .. config.torrent_path)
2014-07-04 13:41:44 +02:00
f:close()
return config
end
2014-07-03 21:18:07 +02:00
function is_sudo(msg)
2014-07-04 13:41:44 +02:00
local var = false
-- Check users id in config
for v,user in pairs(config.sudo_users) do
if user == msg.from.id then
var = true
end
end
return var
end
2014-07-08 20:55:17 +02:00
function write_log_file(msg)
name = get_name(msg)
ret = name .. ' > ' .. msg.text
write_to_file(config.log_file, ret)
end
-- Saves a string to file
function write_to_file(filename, value)
if (value) then
2014-07-08 21:00:52 +02:00
local file = io.open(filename,"a")
2014-07-08 21:05:01 +02:00
file:write(value, "\n")
2014-07-08 20:55:17 +02:00
file:close()
end
end
function get_name(msg)
2014-07-04 13:41:44 +02:00
local name = msg.from.first_name
if name == nil then
2014-07-08 20:55:17 +02:00
name = msg.from.id
2014-07-04 13:41:44 +02:00
end
2014-07-08 20:55:17 +02:00
return name
end
function run_sh(msg)
name = get_name(msg)
2014-07-04 13:41:44 +02:00
text = ''
if config.sh_enabled == false then
text = '!sh command is disabled'
2014-07-03 21:18:07 +02:00
else
2014-07-04 13:41:44 +02:00
if is_sudo(msg) then
bash = msg.text:sub(4,-1)
text = run_bash(bash)
else
text = name .. ' you have no power here!'
end
2014-07-03 21:18:07 +02:00
end
2014-07-04 13:41:44 +02:00
return text
2014-07-03 21:18:07 +02:00
end
2014-06-30 12:42:28 +02:00
function run_bash(str)
local cmd = io.popen(str)
local result = cmd:read('*all')
cmd:close()
return result
end
2014-07-03 21:18:07 +02:00
function get_fortunes_uc3m()
2014-07-08 20:55:17 +02:00
math.randomseed(os.time())
2014-07-04 13:41:44 +02:00
local i = math.random(0,178) -- max 178
local web = "http://www.gul.es/fortunes/f"..i
b, c, h = http.request(web)
2014-07-03 21:18:07 +02:00
return b
end
2014-10-04 17:02:29 +02:00
function getDulcinea( text )
local api = "http://dulcinea.herokuapp.com/api/?query="
b = http.request(api..text)
dulcinea = json:decode(b)
2014-10-04 17:36:33 +02:00
if dulcinea.status == "error" then
return "Error: " .. dulcinea.message
end
2014-10-04 17:02:29 +02:00
while dulcinea.type == "multiple" do
text = dulcinea.response[1].id
b = http.request(api..text)
dulcinea = json:decode(b)
end
vardump(dulcinea)
2014-10-04 17:36:33 +02:00
local text = ""
local responses = #dulcinea.response
if (responses > 5) then
responses = 5
end
for i = 1, responses, 1 do
text = text .. dulcinea.response[i].word .. "\n"
local meanings = #dulcinea.response[i].meanings
if (meanings > 5) then
meanings = 5
end
for j = 1, meanings, 1 do
local meaning = dulcinea.response[i].meanings[j].meaning
text = text .. meaning .. "\n\n"
end
2014-10-04 17:02:29 +02:00
end
return text
end
2014-09-25 15:24:52 +02:00
function getGoogleImage(text)
text = URL.escape(text)
2014-10-04 17:02:29 +02:00
for i = 1, 5, 1 do -- Try 5 times
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
b = http.request(api..text)
local google = json:decode(b)
if (google.responseStatus == 200) then -- OK
math.randomseed(os.time())
i = math.random(#google.responseData.results) -- Random image from results
return google.responseData.results[i].url
end
end
2014-09-25 15:24:52 +02:00
end
2014-08-06 01:15:54 +02:00
function get_9GAG()
b = http.request("http://api-9gag.herokuapp.com/")
2014-07-03 21:18:07 +02:00
local gag = json:decode(b)
2014-08-05 14:20:53 +02:00
math.randomseed(os.time())
2014-08-06 01:15:54 +02:00
i = math.random(#gag) -- random max json table size (# is an operator o.O)
local link_image = gag[i].src
local title = gag[i].title
if link_image:sub(0,2) == '//' then
link_image = msg.text:sub(3,-1)
end
return link_image, title
2014-07-03 21:18:07 +02:00
end
2014-10-09 15:09:06 +02:00
function getEURUSD( )
b = http.request("http://webrates.truefx.com/rates/connect.html?c=EUR/USD&f=csv&s=n")
local rates = b:split(", ")
local symbol = rates[1]
local timestamp = rates[2]
local sell = rates[3]..rates[4]
local buy = rates[5]..rates[6]
return symbol..'\n'..'Buy: '..buy..'\n'..'Sell: '..sell
end
2014-09-25 23:28:04 +02:00
function download_to_file( url )
print("url a descargar: "..url)
req, c, h = http.request(url)
htype = h["content-type"]
vardump(c)
print("content-type: "..htype)
if htype == "image/jpeg" then
file_name = string.random(5)..".jpg"
file_path = "/tmp/"..file_name
else
if htype == "image/gif" then
file_name = string.random(5)..".gif"
file_path = "/tmp/"..file_name
else
if htype == "image/png" then
file_name = string.random(5)..".png"
file_path = "/tmp/"..file_name
else
file_name = url:match("([^/]+)$")
file_path = "/tmp/"..file_name
end
end
end
file = io.open(file_path, "w+")
file:write(req)
file:close()
return file_path
end
2014-07-02 14:30:18 +02:00
function get_weather(location)
b, c, h = http.request("http://api.openweathermap.org/data/2.5/weather?q=" .. location .. "&units=metric")
weather = json:decode(b)
2014-07-07 10:30:27 +02:00
local city = weather.name
local country = weather.sys.country
temp = 'The temperature in ' .. city .. ' (' .. country .. ')'
temp = temp .. ' is ' .. weather.main.temp .. '°C'
2014-07-02 14:30:18 +02:00
conditions = 'Current conditions are: ' .. weather.weather[1].description
if weather.weather[1].main == 'Clear' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Clouds' then
2014-07-03 21:18:07 +02:00
conditions = conditions .. ' ☁☁'
elseif weather.weather[1].main == 'Rain' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Thunderstorm' then
conditions = conditions .. ' ☔☔☔☔'
2014-07-02 14:30:18 +02:00
end
return temp .. '\n' .. conditions
end
2014-07-04 13:41:44 +02:00
function string.random(length)
math.randomseed(os.time())
local str = "";
for i = 1, length do
math.random(97, 122)
str = str..string.char(math.random(97, 122));
end
return str;
end
function string.get_extension_from_filename( filename )
return filename:match( "%.([^%.]+)$" )
end
function string.get_last_word( words )
local splitted = split_by_space ( words )
return splitted[#splitted]
end
function split_by_space ( text )
words = {}
for word in string.gmatch(text, "[^%s]+") do
table.insert(words, word)
end
return words
end
2014-10-09 15:09:06 +02:00
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
2014-06-30 12:42:28 +02:00
function vardump(value, depth, key)
local linePrefix = ""
local spaces = ""
if key ~= nil then
linePrefix = "["..key.."] = "
end
if depth == nil then
depth = 0
else
depth = depth + 1
for i=1, depth do spaces = spaces .. " " end
end
if type(value) == 'table' then
mTable = getmetatable(value)
if mTable == nil then
print(spaces ..linePrefix.."(table) ")
else
print(spaces .."(metatable) ")
value = mTable
end
for tableKey, tableValue in pairs(value) do
vardump(tableValue, depth, tableKey)
end
elseif type(value) == 'function' or
type(value) == 'thread' or
type(value) == 'userdata' or
value == nil
then
print(spaces..tostring(value))
else
print(spaces..linePrefix.."("..type(value)..") "..tostring(value))
end
end
2014-07-04 13:41:44 +02:00
-- Start and load values
config = load_config()
our_id = 0
now = os.time()
function get_receiver(msg)
if msg.to.type == 'user' then
return 'user#id'..msg.from.id
end
if msg.to.type == 'chat' then
return 'chat#id'..msg.to.id
end
end
function on_our_id (id)
our_id = id
end
function on_user_update (user, what)
--vardump (user)
end
function on_chat_update (chat, what)
--vardump (chat)
end
function on_secret_chat_update (schat, what)
--vardump (schat)
end
function on_get_difference_end ()
end
function on_binlog_replay_end ()
started = 1
2014-10-11 13:54:43 +02:00
end