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/plugins/chatter.lua

94 lines
2.3 KiB
Lua
Raw Normal View History

-- Put this absolutely at the end, even after greetings.lua.
2015-07-05 18:14:41 +02:00
local chatter = {}
local HTTP = require('socket.http')
local URL = require('socket.url')
local JSON = require('dkjson')
local bindings = require('bindings')
local utilities = require('utilities')
function chatter:init()
if not self.config.simsimi_key then
print('Missing config value: simsimi_key.')
print('chatter.lua will not be enabled.')
return
end
2016-01-21 20:57:06 +01:00
chatter.triggers = {
'',
'^' .. self.info.first_name .. ',',
'^@' .. self.info.username .. ','
}
end
2015-07-05 04:51:12 +02:00
function chatter:action(msg)
2015-07-05 04:51:12 +02:00
if msg.text == '' then return end
-- This is awkward, but if you have a better way, please share.
if msg.text_lower:match('^' .. self.info.first_name .. ',')
or msg.text_lower:match('^@' .. self.info.username .. ',') then
elseif msg.text:match('^/') then
return true
2015-11-27 04:25:03 +01:00
-- Uncomment the following line for Al Gore-like reply chatter.
-- elseif msg.reply_to_message and msg.reply_to_message.from.id == bot.id then
elseif msg.from.id == msg.chat.id then
else
return true
end
2015-07-05 04:51:12 +02:00
bindings.sendChatAction(self, { action = 'typing' } )
2015-07-05 04:51:12 +02:00
local input = msg.text_lower
input = input:gsub(self.info.first_name, 'simsimi')
input = input:gsub('@'..self.info.username, 'simsimi')
local sandbox
if self.config.simsimi_trial then
2016-01-21 20:57:06 +01:00
sandbox = 'sandbox.'
else
sandbox = '' -- NO Sandbox
end
local url = 'http://' ..sandbox.. 'api.simsimi.com/request.p?key=' ..self.config.simsimi_key.. '&lc=' ..self.config.lang.. '&ft=1.0&text=' .. URL.escape(input)
2015-07-05 04:51:12 +02:00
local jstr, res = HTTP.request(url)
2015-07-05 04:51:12 +02:00
if res ~= 200 then
utilities.send_message(self, msg.chat.id, self.config.errors.chatter_connection)
return
2015-07-05 04:51:12 +02:00
end
local jdat = JSON.decode(jstr)
2016-01-21 20:57:06 +01:00
if not jdat.response then
utilities.send_message(self, msg.chat.id, self.config.errors.chatter_response)
2016-01-21 20:57:06 +01:00
return
end
2016-04-27 01:37:55 +02:00
local output = jdat.response
2015-07-05 04:51:12 +02:00
2016-04-27 01:37:55 +02:00
if output:match('^I HAVE NO RESPONSE.') then
2016-04-29 06:12:04 +02:00
output = self.config.errors.chatter_response
2015-07-05 04:51:12 +02:00
end
2015-07-05 18:14:41 +02:00
-- Let's clean up the response a little. Capitalization & punctuation.
local filter = {
['%aimi?%aimi?'] = self.info.first_name,
2015-07-08 04:24:12 +02:00
['^%s*(.-)%s*$'] = '%1',
['^%l'] = string.upper,
['USER'] = msg.from.first_name
2015-07-08 04:24:12 +02:00
}
for k,v in pairs(filter) do
2016-04-27 01:37:55 +02:00
output = string.gsub(output, k, v)
2015-07-08 04:24:12 +02:00
end
2016-04-27 01:37:55 +02:00
if not string.match(output, '%p$') then
output = output .. '.'
2015-07-05 18:14:41 +02:00
end
utilities.send_message(self, msg.chat.id, output)
2015-07-05 04:51:12 +02:00
end
return chatter