Apps Home
|
Create an App
LittleChatbot
Author:
nnnklein
Description
Source Code
Launch App
Current Users
Created by:
Nnnklein
function contains(s, st){ return s.indexOf(st) !== -1; } function startsWith(s, prefix) { return s.indexOf(prefix) == 0; } function endsWith(s, suffix) { return s.indexOf(suffix, s.length - suffix.length) !== -1; } function getCurrentTime(){ return new Date().getTime(); } function containsOne (s, arrayOfContainingStrings){ s = s.toLowerCase(); for(var i in arrayOfContainingStrings){ var st = arrayOfContainingStrings[i].toLowerCase(); if(s == st){//s equals st return true; } //is st in s, but separated to other words by whitespace? if(startsWith(s, st+" ") || endsWith(s, " "+st) || contains(s, " "+st+" ")){ return true; } } return false; } var strStartBot = "bot"; var strPauseBot = "stopbot"; var strResetBot = "resetbot"; cb.changeRoomSubject("To talk to the bot, just start your text with \""+strStartBot+"\". Stop talking by writing \""+strPauseBot+"\", or reset it by writing \""+strResetBot+"\""); var timeLastMessage = getCurrentTime(); var timeoutToAksUsersIfTheyAreStillThere = 60000 * 4; //4 Minutes var allusers = []; var messageCount = 0; function dontUnderstand(arrayWithPossibilities){ var st = ""; var delimiter = " or "; for(var i in arrayWithPossibilities){ st += arrayWithPossibilities[i] + delimiter; } st = st.substring(0, st.length - delimiter.length); return "Sorry, I don\'t understand, write "+st+" please"; } function textWhenNobodyHasWritten() { if(timeLastMessage + timeoutToAksUsersIfTheyAreStillThere <= getCurrentTime()){ cb.chatNotice("Bot: Nobody is writing... still gone to bed?"); timeLastMessage = getCurrentTime(); } cb.setTimeout(textWhenNobodyHasWritten, 60000); } cb.setTimeout(textWhenNobodyHasWritten, 60000); function isYes(str){ return containsOne(str, ["yes", "yeah", "yo", "jo", "ja", "oui"]); } function isNo(str){ return containsOne(str, ["no", "nope", "nein", "non"]); } function handleMessage(message){ var test = undefined; timeLastMessage = getCurrentTime(); var username = message.user; //Test var arrayContent = ""; for(var i in allusers){ arrayContent += "("+i+" ["+allusers[i].name+", "+allusers[i].msgid+", "+allusers[i].usesbot+"]), "; } //cb.chatNotice("allusers: " + arrayContent); var text = message.m.trim().toLowerCase(); var out = undefined; var isFemale = message.gender == "f"; var user = allusers[username]; //User Object test = user; //start or restart chatbot if(startsWith(text, strStartBot)){ if(user == undefined){ allusers[username] = {"name" : username, "msgid" : 0, "usesbot" : true}; //msgid -> message id user = allusers[username]; }else{ user.usesbot = true; return " -> "+username+", Bot is ready"; } } //pause chatbot if(user != undefined && startsWith(text, strPauseBot)){ user.usesbot = false; return " -> "+username+", Bot is stopped"; } //reset chatbot for this user only if(user != undefined && startsWith(text, strResetBot)){ allusers[username] = {"name" : username, "msgid" : 0, "usesbot" : false}; //... = undefined leads to an error later user = allusers[username]; return " -> "+username+", Bot is resetted"; } if(user != undefined && user.usesbot){ //user is in bot speak mode switch(user.msgid){ case 0: out = "Hello "+username+" nice to meet you. How was the day?"; user.msgid = 1; break; case 1: out = "OK, the weather here in the PC is always the same. Sometimes it's very windy, because of the cooler. Should I tell you something?"; user.msgid = 2; break; case 2: //previous is 1, 3 if(isYes(text)){ out = "OK, what do you want me to speak about? About me or about the sexy performer?"; user.msgid = 4; }else if(isNo(text)){ out = "No? Well, tell me something about you"; user.msgid = 3; }else{ out = dontUnderstand(["yes","no"]); } break; case 3: //previous is 2, 4 out = "ah okay. Should I tell you now something?"; user.msgid = 2; break; case 4: //previous is 2 if(containsOne(text, ["you"])){ out = "I'm a happy computer program, written in JavaScript. I'm single and I'm looking for a pretty php script, so that we can have nice HTML children :)"; user.msgid = 3; }else if(containsOne(text, ["performer"])){ out = "He\'s online here once or twice a week. Thats my working time. He enjoys dancing, especially for nice girls ^^"; if(isFemale){ out += " So ask him for writing private, if you want ^^"; }else{ out += " But you can also write him ^^"; } user.msgid = 3; }else{ out = dontUnderstand(["you","performer"]); } break; default: out = "I\'m confused. You need to reset me... Write \""+strResetBot+"\""; break; } out = " -> "+username + ": " + out; }else{ //Answering to text, that is not expecially for the bot if(containsOne(text, ["hi","hey","hello"])){ out = "Hi "+username+", welcome to this sexy chat :)"; if(isFemale){ out += " You should know... the performer likes very much to talk to nice girls. You should ask for pm :)"; } }else if(containsOne(text, ["penis","cock"])){ out = "Yeah, a great Penis!!"; }else if(containsOne(text, ["sexy","mmm","nice"])){ out = "You're right, "+username+", and have you seen his ass?? WOW"; } if(out !== undefined){ out = ": " + out; } } return out; } cb.onMessage(function (message) { var out = handleMessage(message); messageCount++; if(out !== undefined){ cb.chatNotice("Bot" + out); //cb.chatNotice("Bot" + messageCount + out); } //cb.chatNotice("Test "+test); });
© Copyright Chaturbate 2011- 2026. All Rights Reserved.