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

@ -1,8 +1,4 @@
import { cfg } from "../cfg"; import cleverbot from "./lib/cleverbot";
import rp from "request-promise";
import jsdom from "jsdom";
let sessionid = "";
let api = "sprince/mrsprince"; //"fjosnir/hediko";
export default bot => { export default bot => {
bot._trigger.set("chatbot", new bot.trigger({ bot._trigger.set("chatbot", new bot.trigger({
@ -14,48 +10,13 @@ export default bot => {
.replace(/uwe/gi, "") .replace(/uwe/gi, "")
.split("?") .split("?")
.join(""); .join("");
mitsuku(chat) cleverbot.ask(chat)
.then(res => { .then(res => {
e.reply(res.responses[0].replace(/mitsuku/gi, "Uwe")); e.reply(res.response);
}) })
.catch(err => { .catch(err => {
e.reply("Darauf habe ich keine Antwort ;;"); console.log(err);
console.log(err.message);
}); });
} }
})); }));
}; };
const mitsuku = msg => new Promise((resolve, reject) => {
const options = {
uri: `https://playground.pandorabots.com/talk/${api}?user_key=${cfg.main.chatbotkey.val}&sessionid=${sessionid}&input=${msg}&client_name=uwe`,
method: "POST",
body: {
recent: true
},
json: true
};
rp(options)
.then(res => {
console.log(res);
resolve(res);
})
.catch(err => reject(err));
});
const init = () => {
const options = {
uri: `https://playground.pandorabots.com/talk/${api}?user_key=${cfg.main.chatbotkey.val}&input=Hallo&client_name=uwe`,
method: "POST",
body: {
recent: true
},
json: true
};
rp(options)
.then(res => {
sessionid = res.sessionid;
});
};
setTimeout(()=>init(), 1000);

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();