Portiere Cats- und Cleverbot-Plugins

This commit is contained in:
Andreas Bielawski 2016-06-17 16:42:54 +02:00
parent 515f994ae1
commit bbeb1883c3
2 changed files with 55 additions and 21 deletions

View File

@ -4,35 +4,34 @@ local HTTP = require('socket.http')
local utilities = require('otouto.utilities')
function cats:init(config)
if not config.thecatapi_key then
print('Missing config value: thecatapi_key.')
if not cred_data.cat_apikey then
print('Missing config value: cat_apikey.')
print('cats.lua will be enabled, but there are more features with a key.')
end
cats.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('cat').table
cats.triggers = {
"^/cat$",
"^/cat (gif)$"
}
cats.doc = [[*
]]..config.cmd_pat..[[cat*: Postet eine zufällige Katze
*]]..config.cmd_pat..[[cat* _gif_: Postet eine zufällige, animierte Katze]]
end
cats.command = 'cat'
cats.doc = '`Returns a cat!`'
local apikey = cred_data.cat_apikey or "" -- apply for one here: http://thecatapi.com/api-key-registration.html
function cats:action(msg, config)
local url = 'http://thecatapi.com/api/images/get?format=html&type=jpg'
if config.thecatapi_key then
url = url .. '&api_key=' .. config.thecatapi_key
if matches[1] == 'gif' then
local url = 'http://thecatapi.com/api/images/get?type=gif&apikey='..apikey
local file = download_to_file(url)
utilities.send_document(self, msg.chat.id, file, nil, msg.message_id)
else
local url = 'http://thecatapi.com/api/images/get?type=jpg,png&apikey='..apikey
local file = download_to_file(url)
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
local str, res = HTTP.request(url)
if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end
str = str:match('<img src="(.-)">')
local output = '[Cat!]('..str..')'
utilities.send_message(self, msg.chat.id, output, false, nil, true)
end
return cats

View File

@ -0,0 +1,35 @@
local cleverbot = {}
local https = require('ssl.https')
local URL = require('socket.url')
local utilities = require('otouto.utilities')
local json = require('dkjson')
function cleverbot:init(config)
cleverbot.triggers = {
"^/cbot (.*)$"
}
cleverbot.doc = [[*
]]..config.cmd_pat..[[cbot* _<Text>_*: Befragt den Cleverbot]]
end
cleverbot.command = 'cbot <Text>'
function cleverbot:action(msg, config)
local text = msg.text
local url = "https://brawlbot.tk/apis/chatter-bot-api/cleverbot.php?text="..URL.escape(text)
local query = https.request(url)
if query == nil then utilities.send_reply(self, msg, 'Ein Fehler ist aufgetreten :(') return end
local decode = json.decode(query)
local answer = string.gsub(decode.clever, "&Auml;", "Ä")
local answer = string.gsub(answer, "&auml;", "ä")
local answer = string.gsub(answer, "&Ouml;", "Ö")
local answer = string.gsub(answer, "&ouml;", "ö")
local answer = string.gsub(answer, "&Uuml;", "Ü")
local answer = string.gsub(answer, "&uuml;", "ü")
local answer = string.gsub(answer, "&szlig;", "ß")
utilities.send_reply(self, msg, answer)
end
return cleverbot