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-2/bot.lua

95 lines
1.9 KiB
Lua
Raw Normal View History

2015-07-03 00:15:52 +02:00
-- bot.lua
-- Run this!
HTTP = require('socket.http')
HTTPS = require('ssl.https')
JSON = require('dkjson')
2015-07-03 00:15:52 +02:00
URL = require('socket.url')
2015-07-08 10:03:27 +02:00
VERSION = 2.2
2015-07-03 00:15:52 +02:00
function on_msg_receive(msg)
2015-07-04 16:55:18 +02:00
if msg.date < os.time() - 5 then return end -- don't react to old messages
2015-07-03 00:15:52 +02:00
if not msg.text then return end -- don't react to media messages
if msg.forward_from then return end -- don't react to forwarded messages
local lower = string.lower(msg.text)
for i,v in pairs(plugins) do
for j,w in pairs(v.triggers) do
if string.match(lower, w) then
send_chat_action(msg.chat.id, 'typing')
v.action(msg)
end
end
end
end
function bot_init()
print('\nLoading configuration...')
local jstr = io.open('config.json')
local jstr = jstr:read('*all')
config = JSON.decode(jstr)
print(#config.plugins .. ' plugins enabled.')
require('bindings')
require('utilities')
print('\nFetching bot information...')
bot = get_me().result
for k,v in pairs(bot) do
print('',k,v)
end
print('Bot information retrieved!\n')
print('Loading plugins...')
plugins = {}
for i,v in ipairs(config.plugins) do
print('',v)
local p = loadfile('plugins/'..v)()
table.insert(plugins, p)
end
print('Done! Plugins loaded: ' .. #plugins .. '\n')
print('Generating help message...')
2015-07-04 16:54:41 +02:00
help_message = ''
2015-07-03 00:15:52 +02:00
for i,v in ipairs(plugins) do
if v.doc then
local a = string.sub(v.doc, 1, string.find(v.doc, '\n')-1)
print(a)
help_message = help_message .. ' - ' .. a .. '\n'
end
end
print('Help message generated!\n')
is_started = true
end
bot_init()
reminders = {}
last_update = 0
while is_started == true do
for i,v in ipairs(get_updates(last_update).result) do
if v.update_id > last_update then
last_update = v.update_id
on_msg_receive(v.message)
end
end
2015-07-03 00:56:49 +02:00
for i,v in pairs(reminders) do
2015-07-03 00:15:52 +02:00
if os.time() > v.alarm then
send_message(v.chat_id, 'Reminder: '..v.text)
table.remove(reminders, i)
end
end
end