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

586 lines
15 KiB
Lua
Raw Normal View History

2014-11-03 11:54:03 +01:00
http = require("socket.http")
URL = require("socket.url")
json = (loadfile "./bot/JSON.lua")()
2014-07-02 14:30:18 +02:00
2014-11-03 22:36:15 +01:00
VERSION = 'v0.6'
2014-06-30 12:42:28 +02:00
2014-11-03 11:54:03 +01:00
function on_msg_receive (msg)
2014-11-03 11:54:03 +01:00
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)
2014-09-24 15:31:25 +02:00
else
2014-11-03 11:54:03 +01:00
if is_image_url(msg.text) then
send_image_from_url (msg)
elseif get_youtube_code(msg.text) then
send_youtube_thumbnail(msg)
else
if is_file_url(msg.text) then
send_file_from_url(msg)
end
2014-09-24 15:31:25 +02:00
end
end
2014-11-03 11:54:03 +01:00
mark_read(get_receiver(msg), ok_cb, false)
-- write_log_file(msg)
end
2014-11-03 11:54:03 +01:00
function ok_cb(extra, success, result)
end
function msg_valid(msg)
if msg.out then
return false
end
if msg.date < now then
return false
end
if msg.text == nil then
return false
end
if msg.unread == 0 then
return false
end
end
function send_file_from_url (msg)
last = string.get_last_word(msg.text)
file = download_to_file(last)
send_document(get_receiver(msg), file, ok_cb, false)
end
function send_image_from_url (msg)
last = string.get_last_word(msg.text)
file = download_to_file(last)
send_photo(get_receiver(msg), file, ok_cb, false)
end
function is_image_url(text)
last = string.get_last_word(text)
extension = string.get_extension_from_filename(last) -- TODO: Change it please
if extension == 'jpg' or extension == 'png' or extension == 'jpeg' then
return true
end
return false
end
function get_youtube_code( text )
local yt_normal = string.match(text, "youtube.com/watch%?v=([A-Za-z0-9-]+)")
if yt_normal then
return yt_normal
end
yt_short = string.match(text, "youtu.be/([A-Za-z0-9-]+)")
if yt_short then
return yt_short
end
return false
end
function send_youtube_thumbnail(msg)
yt_code = get_youtube_code(msg.text)
yt_thumbnail = "http://img.youtube.com/vi/".. yt_code .."/hqdefault.jpg"
file = download_to_file(yt_thumbnail)
send_photo(get_receiver(msg), file, ok_cb, false)
end
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
-- Where magic happens
function do_action(msg)
local receiver = get_receiver(msg)
if string.starts(msg.text, '!sh') then
text = run_sh(msg)
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!uc3m') then
text = get_fortunes_uc3m()
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!img') then
text = msg.text:sub(6,-1)
url = getGoogleImage(text)
file_path = download_to_file(url)
print(file_path)
send_photo(receiver, file_path, ok_cb, false)
return
end
if string.starts(msg.text, '!rae') then
2014-09-25 23:28:04 +02:00
text = msg.text:sub(6,-1)
2014-11-03 11:54:03 +01:00
meaning = getDulcinea(text)
send_msg(receiver, meaning, ok_cb, false)
end
if string.starts(msg.text, '!9gag') then
url, title = get_9GAG()
2014-09-25 23:28:04 +02:00
file_path = download_to_file(url)
send_photo(receiver, file_path, ok_cb, false)
2014-11-03 11:54:03 +01:00
send_msg(receiver, title, ok_cb, false)
2014-09-25 15:24:52 +02:00
return
2014-11-03 11:54:03 +01:00
end
if string.starts(msg.text, '!fortune') then
text = run_bash('fortune')
send_msg(receiver, text, ok_cb, false)
return
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
end
if string.starts(msg.text, '!cpu') then
text = run_bash('uname -snr') .. ' ' .. run_bash('whoami')
text = text .. '\n' .. run_bash('top -b |head -2')
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!ping') then
print('receiver: '..receiver)
send_msg (receiver, 'pong', ok_cb, false)
return
end
if string.starts(msg.text, '!weather') then
if string.len(msg.text) <= 9 then
city = 'Madrid,ES'
else
city = msg.text:sub(10,-1)
end
text = get_weather(city)
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!echo') then
echo = msg.text:sub(7,-1)
send_msg(receiver, echo, ok_cb, false)
return
end
if string.starts(msg.text, '!eur') then
local eur = getEURUSD(msg.text)
send_msg(receiver, eur, ok_cb, false)
return
end
if string.starts(msg.text, '!set') then
local text = save_value(msg.text:sub(5,-1))
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!get') then
local text = get_value(msg.text:sub(6,-1))
send_msg(receiver, text, ok_cb, false)
return
end
if string.starts(msg.text, '!version') then
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
end
if string.starts(msg.text, '!help') then
text = [[!help : print this help
!ping : bot sends pong
!sh (text) : send commands to bash (only privileged users)
!echo (text) : echo the msg
!version : version info
!cpu : status (uname + top)
!fwd : forward msg
!forni : send text to group Fornicio
!fortune : print a random adage
!weather [city] : weather in that city (Madrid if not city)
!9gag : send random url image from 9gag
!rae (word): Spanish dictionary
!eur : EURUSD market value
!img (text) : search image with Google API and sends it
2014-11-03 22:36:15 +01:00
!uc3m : fortunes from Universidad Carlos III
!set [variable_name] [value] store for !get
!get (variable_name) retrieves variables saved with !set]]
2014-11-03 11:54:03 +01:00
send_msg(receiver, text, ok_cb, false)
return
end
2014-11-03 11:54:03 +01:00
end
2014-11-03 11:54:03 +01:00
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
2014-11-03 11:54:03 +01: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.sh_enabled then
print ("!sh command is enabled")
for v,user in pairs(config.sudo_users) do
print("Allowed user: " .. user)
end
end
-- print("Torrent path: " .. config.torrent_path)
f:close()
return config
end
2014-11-03 11:54:03 +01:00
function save_value( text )
2014-11-03 22:36:15 +01:00
var_name, var_value = string.match(text, "(%a+) (.+)")
if (a == nil or b == nil) then
2014-11-03 11:54:03 +01:00
return "Usage: !set var_name value"
end
2014-11-03 22:36:15 +01:00
config.values[var_name] = var_value
2014-11-03 11:54:03 +01:00
local json_text = json:encode_pretty(config)
file = io.open ("./bot/config.json", "w+")
file:write(json_text)
file:close()
2014-11-03 22:36:15 +01:00
return "Saved "..var_name.." = "..var_value
2014-11-03 11:54:03 +01:00
end
2014-11-03 11:54:03 +01:00
function get_value( value_name )
2014-11-03 14:25:51 +01:00
-- If there is not value name, return all the values.
if (value_name == "" ) then
local text = ""
for key,value in pairs(config.values) do
2014-11-03 22:36:15 +01:00
text = text..key.." = "..value.."\n"
2014-11-03 14:25:51 +01:00
end
return text
end
2014-11-03 11:54:03 +01:00
local value = config.values[value_name]
if ( value == nil) then
return "Cant find "..value_name
end
return value_name.." = "..value
end
2014-11-03 11:54:03 +01:00
function is_sudo(msg)
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-10-09 15:09:06 +02:00
2014-11-03 11:54:03 +01:00
function write_log_file(msg)
name = get_name(msg)
ret = name .. ' > ' .. msg.text
write_to_file(config.log_file, ret)
end
2014-11-03 11:32:26 +01:00
2014-11-03 11:54:03 +01:00
-- Saves a string to file
function write_to_file(filename, value)
if (value) then
local file = io.open(filename,"a")
file:write(value, "\n")
file:close()
end
end
2014-11-03 11:32:26 +01:00
2014-11-03 11:54:03 +01:00
function get_name(msg)
local name = msg.from.first_name
if name == nil then
name = msg.from.id
end
return name
end
2014-09-25 23:28:04 +02:00
2014-11-03 11:54:03 +01:00
function run_sh(msg)
name = get_name(msg)
text = ''
if config.sh_enabled == false then
text = '!sh command is disabled'
else
if is_sudo(msg) then
bash = msg.text:sub(4,-1)
text = run_bash(bash)
else
text = name .. ' you have no power here!'
end
end
return text
2014-07-08 20:55:17 +02:00
end
2014-11-03 11:54:03 +01:00
function run_bash(str)
local cmd = io.popen(str)
local result = cmd:read('*all')
cmd:close()
return result
end
function get_fortunes_uc3m()
math.randomseed(os.time())
local i = math.random(0,178) -- max 178
local web = "http://www.gul.es/fortunes/f"..i
b, c, h = http.request(web)
return b
end
function getDulcinea( text )
-- Powered by https://github.com/javierhonduco/dulcinea
local api = "http://dulcinea.herokuapp.com/api/?query="
2014-10-04 17:02:29 +02:00
b = http.request(api..text)
dulcinea = json:decode(b)
2014-11-03 11:54:03 +01:00
if dulcinea.status == "error" then
return "Error: " .. dulcinea.message
2014-10-04 17:36:33 +02:00
end
2014-11-03 11:54:03 +01:00
while dulcinea.type == "multiple" do
text = dulcinea.response[1].id
b = http.request(api..text)
dulcinea = json:decode(b)
2014-10-04 17:36:33 +02:00
end
2014-11-03 11:54:03 +01:00
vardump(dulcinea)
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
end
return text
2014-10-04 17:02:29 +02:00
end
2014-11-03 11:54:03 +01:00
function getGoogleImage(text)
text = URL.escape(text)
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
end
2014-10-04 17:02:29 +02:00
2014-11-03 11:54:03 +01:00
function get_9GAG()
b = http.request("http://api-9gag.herokuapp.com/")
local gag = json:decode(b)
math.randomseed(os.time())
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
end
2014-10-04 17:02:29 +02:00
2014-11-03 11:54:03 +01:00
function getEURUSD(text)
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]
local usd = string.match(text, "!eur (%d+[%d%.]*)")
text = symbol..'\n'..'Buy: '..buy..'\n'..'Sell: '..sell
if usd then
eur = tonumber(usd) / tonumber(buy)
text = text.."\n "..usd.."USD = "..eur.."EUR"
2014-10-04 17:02:29 +02:00
end
2014-11-03 11:54:03 +01:00
return text
2014-10-04 17:02:29 +02:00
end
2014-11-03 11:54:03 +01: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"
2014-09-25 23:28:04 +02:00
file_path = "/tmp/"..file_name
else
2014-11-03 11:54:03 +01:00
if htype == "image/gif" then
file_name = string.random(5)..".gif"
2014-09-25 23:28:04 +02:00
file_path = "/tmp/"..file_name
else
2014-11-03 11:54:03 +01:00
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
2014-09-25 23:28:04 +02:00
end
end
2014-11-03 11:54:03 +01:00
file = io.open(file_path, "w+")
file:write(req)
file:close()
return file_path
2014-09-25 23:28:04 +02:00
end
2014-11-03 11:54:03 +01: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)
local city = weather.name
local country = weather.sys.country
temp = 'The temperature in ' .. city .. ' (' .. country .. ')'
temp = temp .. ' is ' .. weather.main.temp .. '°C'
conditions = 'Current conditions are: ' .. weather.weather[1].description
if weather.weather[1].main == 'Clear' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Clouds' then
conditions = conditions .. ' ☁☁'
elseif weather.weather[1].main == 'Rain' then
conditions = conditions .. ''
elseif weather.weather[1].main == 'Thunderstorm' then
conditions = conditions .. ' ☔☔☔☔'
end
return temp .. '\n' .. conditions
end
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)
2014-06-30 12:42:28 +02:00
end
2014-11-03 11:54:03 +01:00
return words
2014-06-30 12:42:28 +02:00
end
2014-07-04 13:41:44 +02:00
2014-11-03 11:54:03 +01: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-11-03 11:54:03 +01: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-11-03 11:54:03 +01: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
2014-11-03 11:54:03 +01:00
function on_our_id (id)
our_id = id
end
2014-11-03 11:54:03 +01:00
function on_user_update (user, what)
--vardump (user)
end
2014-11-03 11:54:03 +01:00
function on_chat_update (chat, what)
--vardump (chat)
end
2014-11-03 11:54:03 +01:00
function on_secret_chat_update (schat, what)
--vardump (schat)
end
2014-11-03 11:54:03 +01:00
function on_get_difference_end ()
end
2014-11-03 11:54:03 +01:00
function on_binlog_replay_end ()
started = 1
end