2015-11-24 21:22:04 -05:00
|
|
|
-- Put this absolutely at the end, even after greetings.lua.
|
2015-07-05 12:14:41 -04:00
|
|
|
|
2016-04-10 21:04:47 -07:00
|
|
|
local chatter = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 19:07:23 +00:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 00:31:34 -04:00
|
|
|
local bindings = require('otouto.bindings')
|
2016-06-07 03:04:27 -04:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-10 21:04:47 -07:00
|
|
|
|
2016-05-26 17:26:30 -07:00
|
|
|
function chatter:init(config)
|
|
|
|
if not config.simsimi_key then
|
2016-04-10 21:04:47 -07:00
|
|
|
print('Missing config value: simsimi_key.')
|
|
|
|
print('chatter.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
2016-01-21 16:57:06 -03:00
|
|
|
|
2016-04-10 21:04:47 -07:00
|
|
|
chatter.triggers = {
|
2016-05-30 19:10:58 -04:00
|
|
|
''
|
2016-04-10 21:04:47 -07:00
|
|
|
}
|
|
|
|
end
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2016-05-30 19:10:58 -04:00
|
|
|
chatter.base_url = 'http://%sapi.simsimi.com/request.p?key=%s&lc=%s&ft=1.0&text=%s'
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2016-05-26 17:26:30 -07:00
|
|
|
function chatter:action(msg, config)
|
2015-11-29 06:41:53 -05:00
|
|
|
|
2016-05-30 19:10:58 -04:00
|
|
|
if msg.text == '' then return true end
|
|
|
|
|
|
|
|
if (
|
|
|
|
not (
|
|
|
|
msg.text_lower:match('^'..self.info.first_name:lower()..',')
|
|
|
|
or msg.text_lower:match('^@'..self.info.username:lower()..',')
|
|
|
|
or msg.from.id == msg.chat.id
|
|
|
|
--Uncomment the following line for Al Gore-like conversation.
|
|
|
|
--or (msg.reply_to_message and msg.reply_to_message.from.id == self.info.id)
|
|
|
|
)
|
2016-06-06 23:45:11 -04:00
|
|
|
or msg.text:match('^'..config.cmd_pat)
|
2016-05-30 19:10:58 -04:00
|
|
|
or msg.text == ''
|
|
|
|
) then
|
2015-11-24 21:22:04 -05:00
|
|
|
return true
|
|
|
|
end
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2016-05-29 13:08:39 -04:00
|
|
|
bindings.sendChatAction(self, { action = 'typing' } )
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2016-05-30 19:10:58 -04:00
|
|
|
local input = msg.text_lower:gsub(self.info.first_name, 'simsimi')
|
2016-04-10 21:04:47 -07:00
|
|
|
input = input:gsub('@'..self.info.username, 'simsimi')
|
|
|
|
|
2016-06-07 03:02:05 -04:00
|
|
|
local sandbox = config.simsimi_trial and 'sandbox.' or ''
|
2015-11-26 05:34:16 -05:00
|
|
|
|
2016-06-07 03:02:05 -04:00
|
|
|
local url = chatter.base_url:format(sandbox, config.simsimi_key, config.lang, URL.escape(input))
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2015-11-24 21:22:04 -05:00
|
|
|
local jstr, res = HTTP.request(url)
|
2015-07-04 22:51:12 -04:00
|
|
|
if res ~= 200 then
|
2016-05-26 17:26:30 -07:00
|
|
|
utilities.send_message(self, msg.chat.id, config.errors.chatter_connection)
|
2015-11-24 21:22:04 -05:00
|
|
|
return
|
2015-07-04 22:51:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
2016-05-30 19:10:58 -04:00
|
|
|
if not jdat.response or jdat.response:match('^I HAVE NO RESPONSE.') then
|
2016-05-26 17:26:30 -07:00
|
|
|
utilities.send_message(self, msg.chat.id, config.errors.chatter_response)
|
2016-01-21 16:57:06 -03:00
|
|
|
return
|
|
|
|
end
|
2016-04-26 23:37:55 +00:00
|
|
|
local output = jdat.response
|
2015-07-04 22:51:12 -04:00
|
|
|
|
2016-05-30 19:10:58 -04:00
|
|
|
-- Clean up the response here.
|
|
|
|
output = utilities.trim(output)
|
|
|
|
-- Simsimi will often refer to itself. Replace "simsimi" with the bot name.
|
|
|
|
output = output:gsub('%aimi?%aimi?', self.info.first_name)
|
|
|
|
-- Self-explanatory.
|
|
|
|
output = output:gsub('USER', msg.from.first_name)
|
|
|
|
-- Capitalize the first letter.
|
|
|
|
output = output:gsub('^%l', string.upper)
|
|
|
|
-- Add a period if there is no punctuation.
|
|
|
|
output = output:gsub('%P$', '%1.')
|
2015-07-05 12:14:41 -04:00
|
|
|
|
2016-05-29 13:08:39 -04:00
|
|
|
utilities.send_message(self, msg.chat.id, output)
|
2015-07-04 22:51:12 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-10 21:04:47 -07:00
|
|
|
return chatter
|