Chatbot overhaul

This commit is contained in:
Flummi
2017-12-05 17:57:46 +01:00
parent 125badd13f
commit a0b991de89
2 changed files with 56 additions and 44 deletions

View File

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