2015-11-25 03:22:04 +01:00
|
|
|
-- Put this absolutely at the end, even after greetings.lua.
|
2015-07-05 18:14:41 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local triggers = {
|
2015-11-26 11:34:16 +01:00
|
|
|
'',
|
2015-11-27 00:55:57 +01:00
|
|
|
'^' .. bot.first_name .. ',',
|
|
|
|
'^@' .. bot.username .. ','
|
2015-07-05 04:51:12 +02:00
|
|
|
}
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local action = function(msg)
|
2015-07-05 04:51:12 +02:00
|
|
|
|
2015-11-29 12:41:53 +01:00
|
|
|
if msg.text == '' then return end
|
|
|
|
|
2015-11-26 11:34:16 +01:00
|
|
|
-- This is awkward, but if you have a better way, please share.
|
|
|
|
if msg.text_lower:match('^' .. bot.first_name .. ',') then
|
2015-11-27 00:55:57 +01:00
|
|
|
elseif msg.text_lower:match('^@' .. bot.username .. ',') then
|
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
|
2015-11-26 11:34:16 +01:00
|
|
|
elseif msg.from.id == msg.chat.id then
|
|
|
|
else
|
2015-11-25 03:22:04 +01:00
|
|
|
return true
|
|
|
|
end
|
2015-07-05 04:51:12 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendChatAction(msg.chat.id, 'typing')
|
2015-07-05 04:51:12 +02:00
|
|
|
|
2015-11-27 00:55:57 +01:00
|
|
|
local input = msg.text_lower
|
|
|
|
input = input:gsub(bot.first_name, 'simsimi')
|
|
|
|
input = input:gsub('@'..bot.username, 'simsimi')
|
2015-11-26 11:34:16 +01:00
|
|
|
|
|
|
|
local url = 'http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=' .. URL.escape(input)
|
2015-07-05 04:51:12 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jstr, res = HTTP.request(url)
|
2015-07-05 04:51:12 +02:00
|
|
|
if res ~= 200 then
|
2015-11-25 03:22:04 +01:00
|
|
|
sendMessage(msg.chat.id, config.errors.chatter_connection)
|
|
|
|
return
|
2015-07-05 04:51:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
2015-11-25 03:22:04 +01:00
|
|
|
local message = jdat.res.msg
|
2015-07-05 04:51:12 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if message:match('^I HAVE NO RESPONSE.') then
|
|
|
|
message = 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.
|
2015-11-25 03:22:04 +01:00
|
|
|
local filter = {
|
2015-08-23 08:46:34 +02:00
|
|
|
['%aimi?%aimi?'] = bot.first_name,
|
2015-07-08 04:24:12 +02:00
|
|
|
['^%s*(.-)%s*$'] = '%1',
|
2015-08-23 08:46:34 +02:00
|
|
|
['^%l'] = string.upper,
|
|
|
|
['USER'] = msg.from.first_name
|
2015-07-08 04:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for k,v in pairs(filter) do
|
|
|
|
message = string.gsub(message, k, v)
|
|
|
|
end
|
|
|
|
|
2015-07-05 18:14:41 +02:00
|
|
|
if not string.match(message, '%p$') then
|
|
|
|
message = message .. '.'
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendMessage(msg.chat.id, message)
|
2015-07-05 04:51:12 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return {
|
|
|
|
action = action,
|
|
|
|
triggers = triggers
|
|
|
|
}
|