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

212 lines
4.7 KiB
Lua
Raw Normal View History

http = require("socket.http")
https = require("ssl.https")
URL = require("socket.url")
json = (loadfile "./libs/JSON.lua")()
serpent = (loadfile "./libs/serpent.lua")()
require("./bot/utils")
2015-01-06 21:04:45 +01:00
VERSION = '0.8.2'
function on_msg_receive (msg)
vardump(msg)
if msg_valid(msg) == false then
return
end
do_action(msg)
mark_read(get_receiver(msg), ok_cb, false)
end
function ok_cb(extra, success, result)
end
function on_binlog_replay_end ()
started = 1
-- Uncomment the line to enable cron plugins.
-- postpone (cron_plugins, false, 5.0)
-- See plugins/ping.lua as an example for cron
_config = load_config()
-- load plugins
plugins = {}
load_plugins()
end
function msg_valid(msg)
-- Dont process outgoing messages
if msg.out then
return false
end
if msg.date < now then
return false
end
if msg.unread == 0 then
return false
end
end
2014-12-26 12:13:27 +01:00
function do_lex(msg, text)
for name, desc in pairs(plugins) do
if (desc.lex ~= nil) then
result = desc.lex(msg, text)
if (result ~= nil) then
print ("Mutating to " .. result)
text = result
2014-12-26 12:13:27 +01:00
end
end
end
-- print("Text mutated to " .. text)
return text
end
-- Where magic happens
function do_action(msg)
local receiver = get_receiver(msg)
local text = msg.text
if msg.text == nil then
-- Not a text message, make text the same as what tg shows so
-- we can match on it. The plugin is resposible for handling
text = '['..msg.media.type..']'
end
-- print("Received msg", text)
2014-12-26 12:13:27 +01:00
msg.text = do_lex(msg, text)
2014-12-26 12:13:27 +01:00
for name, desc in pairs(plugins) do
-- print("Trying module", name)
for k, pattern in pairs(desc.patterns) do
-- print("Trying", text, "against", pattern)
matches = { string.match(text, pattern) }
if matches[1] then
print(" matches", pattern)
2014-12-16 21:50:19 +01:00
if desc.run ~= nil then
-- If plugin is for privileged user
if desc.privileged and not is_sudo(msg) then
local text = 'This plugin requires privileged user'
send_msg(receiver, text, ok_cb, false)
else
result = desc.run(msg, matches)
-- print(" sending", result)
if (result) then
result = do_lex(msg, result)
_send_msg(receiver, result)
2014-12-26 12:13:27 +01:00
end
2014-12-16 21:50:19 +01:00
end
2014-11-04 16:09:08 +01:00
end
2014-11-04 22:06:59 +01:00
end
2014-11-03 11:54:03 +01:00
end
end
end
-- If text is longer than 4096 chars, send multiple msg.
-- https://core.telegram.org/method/messages.sendMessage
function _send_msg( destination, text)
local msg_text_max = 4096
local len = string.len(text)
local iterations = math.ceil(len / msg_text_max)
for i = 1, iterations, 1 do
local inital_c = i * msg_text_max - msg_text_max
local final_c = i * msg_text_max
-- dont worry about if text length < msg_text_max
local text_msg = string.sub(text,inital_c,final_c)
send_msg(destination, text_msg, ok_cb, false)
2014-11-03 11:54:03 +01:00
end
end
-- Save the content of _config to config.lua
function save_config( )
2015-01-06 21:08:18 +01:00
serialize_to_file(_config, './data/config.lua')
print ('saved config into ./data/config.lua')
end
2014-10-09 15:09:06 +02:00
function load_config( )
2015-01-06 21:08:18 +01:00
local f = io.open('./data/config.lua', "r")
-- If config.lua doesnt exists
if not f then
2015-01-06 21:08:18 +01:00
print ("Created new config file: data/config.lua")
create_config()
2015-01-06 21:08:18 +01:00
else
f:close()
end
2015-01-06 21:08:18 +01:00
local config = loadfile ("./data/config.lua")()
for v,user in pairs(config.sudo_users) do
print("Allowed user: " .. user)
end
return config
end
-- Create a basic config.json file and saves it.
function create_config( )
-- A simple config with basic plugins and ourserves as priviled user
config = {
enabled_plugins = {
"9gag",
"echo",
"get",
"set",
"images",
"img_google",
"location",
"media",
"plugins",
"stats",
"time",
"version",
"youtube" },
sudo_users = {our_id}
}
2015-01-06 21:08:18 +01:00
serialize_to_file(config, './data/config.lua')
print ('saved config into ./data/config.lua')
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
2014-12-24 01:38:41 +01:00
-- Enable plugins in config.json
2014-12-16 15:05:42 +01:00
function load_plugins()
for k, v in pairs(_config.enabled_plugins) do
2014-12-24 01:38:41 +01:00
print("Loading plugin", v)
t = loadfile("plugins/"..v..'.lua')()
2014-12-24 01:38:41 +01:00
table.insert(plugins, t)
2014-12-16 15:05:42 +01:00
end
end
2014-12-16 21:50:19 +01:00
-- Cron all the enabled plugins
function cron_plugins()
2014-12-16 15:05:42 +01:00
2014-12-16 21:50:19 +01:00
for name, desc in pairs(plugins) do
if desc.cron ~= nil then
2014-12-22 22:12:28 +01:00
print(desc.description)
2014-12-16 21:50:19 +01:00
desc.cron()
end
end
-- Called again in 5 mins
postpone (cron_plugins, false, 5*60.0)
2014-12-22 22:05:46 +01:00
end
-- Start and load values
our_id = 0
now = os.time()