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

56 lines
1.1 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 triggers = {
''
2015-07-05 04:51:12 +02:00
}
local action = function(msg)
2015-07-05 04:51:12 +02:00
if not msg.reply_to_message then
return true
elseif msg.reply_to_message and msg.reply_to_message.from.id ~= bot.id then
return true
end
2015-07-05 04:51:12 +02:00
sendChatAction(msg.chat.id, 'typing')
2015-07-05 04:51:12 +02:00
local url = 'http://www.simsimi.com/requestChat?lc=en&ft=1.0&req=' .. URL.escape(msg.text_lower)
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
sendMessage(msg.chat.id, config.errors.chatter_connection)
return
2015-07-05 04:51:12 +02:00
end
local jdat = JSON.decode(jstr)
local message = jdat.res.msg
2015-07-05 04:51:12 +02: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.
local filter = {
['%aimi?%aimi?'] = bot.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
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
sendMessage(msg.chat.id, message)
2015-07-05 04:51:12 +02:00
end
return {
action = action,
triggers = triggers
}