Initial commit
commit
edf1ee1b8c
|
@ -0,0 +1,9 @@
|
|||
# AmogusV3
|
||||
Funny clamity chatbot powered by cleverbot_free API.
|
||||
|
||||
## Dependencies
|
||||
- Needs http access and optionally insecure env (for hot reloading)
|
||||
|
||||
Depends on the [libclamity](https://github.com/EliasFleckenstein03/libclamity) CSM.
|
||||
|
||||
You need to run the backend.py before running any instances of the bot, which depends on https://github.com/plasticuproject/cleverbotfree
|
|
@ -0,0 +1,34 @@
|
|||
import cleverbotfree
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
|
||||
cleverbot = None
|
||||
|
||||
class AmogusServer(BaseHTTPRequestHandler):
|
||||
def _set_headers(self):
|
||||
self.send_response(200)
|
||||
self.send_header("Content-type", "text/plain")
|
||||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
self._set_headers()
|
||||
query_components = parse_qs(urlparse(self.path).query)
|
||||
self.wfile.write(cleverbot.single_exchange(query_components["message"][0]).encode("utf8"))
|
||||
|
||||
def main():
|
||||
global cleverbot
|
||||
|
||||
with cleverbotfree.sync_playwright() as cleverbot_pw:
|
||||
cleverbot = cleverbotfree.Cleverbot(cleverbot_pw)
|
||||
|
||||
httpd = HTTPServer(("localhost", 6969), AmogusServer)
|
||||
|
||||
try:
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
finally:
|
||||
cleverbot.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
|
@ -0,0 +1,40 @@
|
|||
local http, env, storage
|
||||
|
||||
function amogus.urlencode(url)
|
||||
url = url:gsub("\n", "\r\n")
|
||||
url = url:gsub("([^%w ])", function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end)
|
||||
url = url:gsub(" ", "+")
|
||||
return url
|
||||
end
|
||||
|
||||
function amogus.parse_message(player, message, discord)
|
||||
if player ~= minetest.localplayer:get_name() then
|
||||
http.fetch({url = "http://localhost:6969?message=" .. amogus.urlencode(message), timeout = 100}, function(res)
|
||||
if res.succeeded then
|
||||
minetest.send_chat_message(res.data)
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
function amogus.reload()
|
||||
local func, err = env.loadfile("clientmods/amogus/bot.lua")
|
||||
if func then
|
||||
local old_amogus = table.copy(amogus)
|
||||
local status, init = pcall(func)
|
||||
if status then
|
||||
init(http, env, storage)
|
||||
else
|
||||
amogus = old_amogus
|
||||
return false, "Error: " .. init
|
||||
end
|
||||
else
|
||||
return false, "Syntax error: " .. err
|
||||
end
|
||||
end
|
||||
|
||||
return function(_http, _env, _storage)
|
||||
http, env, storage = _http, _env, _storage
|
||||
end
|
|
@ -0,0 +1,18 @@
|
|||
amogus = {}
|
||||
|
||||
local http = minetest.request_http_api()
|
||||
local env = minetest.request_insecure_environment()
|
||||
local storage = minetest.get_mod_storage()
|
||||
|
||||
libclamity.register_on_chat_message(function(...)
|
||||
amogus.parse_message(...)
|
||||
end)
|
||||
|
||||
minetest.register_chatcommand("amogus_reload", {
|
||||
func = function()
|
||||
return amogus.reload(http, env, storage)
|
||||
end
|
||||
})
|
||||
|
||||
loadfile(minetest.get_modpath("amogus") .. "/bot.lua")()(http, env, storage)
|
||||
|
Loading…
Reference in New Issue