rp -> fetch (chatbot)

This commit is contained in:
Flummi 2018-09-14 21:39:47 +02:00
parent f900ad6467
commit c302597c63

View File

@ -1,23 +1,23 @@
import rp from "request-promise-native"; import fetch from "../../fetch";
import { cfg } from "../../../inc/cfg"; import { cfg } from "../../../inc/cfg";
class cleverbot { class cleverbot {
constructor() { constructor() {
this.api = "https://cleverbot.io/1.0"; this.api = "https://cleverbot.io/1.0";
this.nick = ""; this.nick = "";
setTimeout(()=>this.init(), 5000); setTimeout(() => this.init(), 5000);
} }
init() { init() {
const options = { const options = {
url: `${this.api}/create`,
method: "POST", method: "POST",
body: Object.assign(cfg.main.chatbot.val, { body: {
nick: "uwibot" ...cfg.main.chatbot.val,
}), ...{ nick: "uwibot" }
json: true }
}; };
rp(options) fetch(`${this.api}/create`, options)
.then(res => res.json())
.then(res => this.nick = res.status === "success" ? res.nick: "uwibot") .then(res => this.nick = res.status === "success" ? res.nick: "uwibot")
.catch(err => console.log(err)); .catch(err => console.log(err));
} }
@ -25,15 +25,17 @@ class cleverbot {
ask(msg) { ask(msg) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const options = { const options = {
url: `${this.api}/ask`,
method: "POST", method: "POST",
body: Object.assign(cfg.main.chatbot.val, { body: {
nick: this.nick, ...cfg.main.chatbot.val,
text: msg ...{
}), nick: this.nick,
json: true text: msg
}
}
}; };
rp(options) fetch(`${this.api}/ask`, options)
.then(res => res.json())
.then(res => res.status === "success"?resolve(res):reject(res.status)) .then(res => res.status === "success"?resolve(res):reject(res.status))
.catch(err => reject(err)); .catch(err => reject(err));
}); });