2015-07-03 00:15:52 +02:00
|
|
|
-- bot.lua
|
|
|
|
-- Run this!
|
|
|
|
|
|
|
|
HTTP = require('socket.http')
|
|
|
|
HTTPS = require('ssl.https')
|
|
|
|
URL = require('socket.url')
|
2015-07-10 09:52:22 +02:00
|
|
|
JSON = require('dkjson')
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-07-29 00:15:24 +02:00
|
|
|
VERSION = 2.5
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
function on_msg_receive(msg)
|
|
|
|
|
2015-07-10 09:52:22 +02:00
|
|
|
msg = process_msg(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
|
2015-07-29 00:13:46 +02:00
|
|
|
--[[
|
|
|
|
if msg.from.id == 77029297 then
|
|
|
|
send_message(msg.chat.id, '/END@MINDSTORMER619')
|
|
|
|
end
|
|
|
|
]]--
|
2015-07-03 00:15:52 +02:00
|
|
|
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
|
2015-07-19 22:46:06 +02:00
|
|
|
if not v.no_typing then
|
|
|
|
send_chat_action(msg.chat.id, 'typing')
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
v.action(msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function bot_init()
|
|
|
|
|
|
|
|
print('\nLoading configuration...')
|
|
|
|
|
2015-07-15 08:15:23 +02:00
|
|
|
config = dofile('config.lua')
|
2015-07-11 10:31:29 +02:00
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
print(#config.plugins .. ' plugins enabled.')
|
|
|
|
|
|
|
|
require('bindings')
|
|
|
|
require('utilities')
|
|
|
|
|
|
|
|
print('\nFetching bot information...')
|
|
|
|
|
|
|
|
bot = get_me().result
|
2015-07-19 22:46:06 +02:00
|
|
|
if not bot then
|
|
|
|
error('Failure fetching bot information.')
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
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)
|
2015-07-15 08:15:23 +02:00
|
|
|
local p = dofile('plugins/'..v)
|
2015-07-03 00:15:52 +02:00
|
|
|
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')
|
|
|
|
|
2015-07-11 05:43:30 +02:00
|
|
|
print('username: @'..bot.username)
|
|
|
|
print('name: '..bot.first_name)
|
|
|
|
print('ID: '..bot.id)
|
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
is_started = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-07-10 09:52:22 +02:00
|
|
|
function process_msg(msg)
|
|
|
|
|
|
|
|
if msg.new_chat_participant and msg.new_chat_participant.id ~= bot.id then
|
|
|
|
msg.text = 'hi '..bot.first_name
|
|
|
|
msg.from = msg.new_chat_participant
|
|
|
|
end
|
|
|
|
|
|
|
|
if msg.left_chat_participant and msg.left_chat_participant.id ~= bot.id then
|
|
|
|
msg.text = 'bye '..bot.first_name
|
|
|
|
msg.from = msg.left_chat_participant
|
|
|
|
end
|
|
|
|
|
2015-07-15 08:15:23 +02:00
|
|
|
if msg.new_chat_participant and msg.new_chat_participant.id == bot.id then
|
|
|
|
msg.text = '/about'
|
|
|
|
end
|
|
|
|
|
2015-07-10 09:52:22 +02:00
|
|
|
return msg
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-07-03 00:15:52 +02:00
|
|
|
bot_init()
|
|
|
|
reminders = {}
|
|
|
|
last_update = 0
|
|
|
|
while is_started == true do
|
|
|
|
|
2015-07-19 22:46:06 +02:00
|
|
|
local res = get_updates(last_update)
|
|
|
|
if not res then
|
2015-07-15 08:15:23 +02:00
|
|
|
print('Error getting updates.')
|
|
|
|
else
|
2015-07-19 22:46:06 +02:00
|
|
|
for i,v in ipairs(res.result) do
|
2015-07-15 08:15:23 +02:00
|
|
|
if v.update_id > last_update then
|
|
|
|
last_update = v.update_id
|
|
|
|
on_msg_receive(v.message)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
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
|
2015-07-15 08:15:23 +02:00
|
|
|
local a = send_message(v.chat_id, 'Reminder: '..v.text)
|
|
|
|
if a then
|
|
|
|
table.remove(reminders, i)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2015-07-19 22:46:06 +02:00
|
|
|
print('Halted.')
|