aktueller Stand lol
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import fetch from "flumm-fetch-cookies";
|
||||
import crypto from "crypto";
|
||||
//import config from "../../../../cfg/config.json";
|
||||
|
||||
export default class cleverbot {
|
||||
constructor() {
|
||||
@@ -18,51 +17,12 @@ export default class cleverbot {
|
||||
let body = `stimulus=${escape(stimulus)}&cb_settings_scripting=no&islearning=1&icognoid=ws&icognocheck=`;
|
||||
body += crypto.createHash("md5").update(body.substring(7, 33)).digest("hex");
|
||||
|
||||
return decodeURIComponent((await fetch("https://www.cleverbot.com/webservicemin?uc=UseOfficialCleverbotAPI", {
|
||||
return decodeURIComponent((await (await fetch("https://www.cleverbot.com/webservicemin?uc=UseOfficialCleverbotAPI", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36"
|
||||
},
|
||||
body: body
|
||||
})).headers["cboutput"]);
|
||||
})).text()).split("\r")[0]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*export default new class cleverbot {
|
||||
constructor() {
|
||||
this.api = "https://cleverbot.io/1.0";
|
||||
this.nick = "";
|
||||
this.init();
|
||||
}
|
||||
|
||||
init() {
|
||||
const options = {
|
||||
method: "POST",
|
||||
body: {
|
||||
...config.apis.cleverbot,
|
||||
...{ nick: "uwibot" }
|
||||
}
|
||||
};
|
||||
fetch(`${this.api}/create`, options)
|
||||
.then(res => res.json())
|
||||
.then(res => this.nick = res.status === "success" ? res.nick: "uwibot")
|
||||
.catch(err => console.log("cleverbot is offline or whatever lol"));
|
||||
}
|
||||
|
||||
ask(msg) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
method: "POST",
|
||||
body: {
|
||||
...config.apis.cleverbot,
|
||||
...{
|
||||
nick: this.nick,
|
||||
text: msg
|
||||
}
|
||||
}
|
||||
};
|
||||
fetch(`${this.api}/ask`, options)
|
||||
.then(res => res.json())
|
||||
.then(res => res.status === "success"?resolve(res):reject(res.status))
|
||||
.catch(err => reject("cleverbot is offline or whatever lol"));
|
||||
});
|
||||
}
|
||||
};*/
|
||||
|
50
src/inc/trigger/lib/fetch.mjs
Normal file
50
src/inc/trigger/lib/fetch.mjs
Normal file
@@ -0,0 +1,50 @@
|
||||
import http from "http";
|
||||
import https from "https";
|
||||
import url from "url";
|
||||
import querystring from "querystring";
|
||||
|
||||
const redirectStatus = new Set([ 301, 302, 303, 307, 308 ]);
|
||||
|
||||
const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res
|
||||
.setEncoding("utf8")
|
||||
.on("data", chunk => data += chunk)
|
||||
.on("end", () => {
|
||||
switch(mode) {
|
||||
case "text": resolve(data); break;
|
||||
case "json": try { resolve(JSON.parse(data)); } catch(err) { reject(data); } break;
|
||||
case "buffer": resolve(new Buffer.from(data)); break;
|
||||
default: reject("lol no D:"); break;
|
||||
}
|
||||
}));
|
||||
const genResolve = res => ({
|
||||
body: res,
|
||||
headers: res.headers,
|
||||
text: () => readdata(res, "text"),
|
||||
json: () => readdata(res, "json"),
|
||||
buffer: () => readdata(res, "buffer")
|
||||
});
|
||||
const request = (a, options, resolve, reject, link = url.parse(a), body = "") => {
|
||||
options = {...{ hostname: link.hostname, path: link.path, method: "GET", redirect: "follow" }, ...options};
|
||||
if(options.method === "POST") {
|
||||
body = typeof options.body === "object" ? querystring.stringify(options.body) : options.body;
|
||||
delete options.body;
|
||||
options.headers = {...options.headers, ...{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body)
|
||||
}};
|
||||
}
|
||||
(link.protocol === "https:"?https:http).request(options, res => {
|
||||
if(redirectStatus.has(res.statusCode) || options.redirect === "manual") {
|
||||
switch(options.redirect) {
|
||||
case "follow":
|
||||
delete options.hostname;
|
||||
delete options.path;
|
||||
request(res.headers.location.startsWith("http") ? res.headers.location : `${link.protocol}//${link.host}${res.headers.location}`, options, resolve, reject);
|
||||
break;
|
||||
case "error": return reject("error lol");
|
||||
}
|
||||
}
|
||||
else return resolve(genResolve(res));
|
||||
}).on("error", err => err).end(body);
|
||||
};
|
||||
export default (_url, options = {}) => new Promise((resolve, reject) => request(_url, options, resolve, reject));
|
Reference in New Issue
Block a user