/    Sign up×
Community /Pin to ProfileBookmark

PHP chatbot script into javascript chatbot script.

On my website I have a system that allows people to create and view chatbots in php. Now I want to offer them as downloads in javascript. I have almost have no knowledge of javascript and when I do use it I hate it. Can someone convert or give me guidance on how to convert my chatbot script please.

[code=php]
//Javascript versions will not need to connect to the database. They will only need to define variables here. I will echo the data onto the javascript bots.
$start_text = $row[‘start’];
$default_response1 = $row[‘default1’];
$default_response2 = $row[‘default2’];
$default_response3 = $row[‘default3’];
$default_response4 = $row[‘default4’];
$default_response5 = $row[‘default5’];
$input_keys = explode(“}{“,$row[‘inputkeys’]);
$responses1 = explode(“}{“,$row[‘responses1’]);
$responses2 = explode(“}{“,$row[‘responses2’]);
$bot_name = $row[‘bot_name’];

mysql_close();

$pre = $_POST[‘pre’];
$words = array(“i’m”, “you’re”, “we’re”, “hi”, “cheerio”, “tally ho”, “thanks”, “i’d”, “i’ve”, “great”, “excellent”, “well”, “fine”, “superb”, “fantastic”, “you’ve”, “don’t”, “mother”, “father”, “?”, “lol”, “lulz”, “lmao”, “never”, “allways”, “correct”, “wrong”, “incorrect”);
$word_replacement = array(“i am”, “you are”, “we are”, “hello”, “bye”, “bye”, “thankyou”, “i would” , “i have”, “good”, “good”, “good”, “good”, “good”, “good”, “you have”, “do not”, “mum”, “dad”, “\?”, “funny”, “funny”, “funny”, “no”, “yes”, “yes”, “no”, “no”);
//Replace similar words with only one of them.
$new_test2 = str_replace($words, $word_replacement,str_replace(“‘”,”‘”,strtolower($_POST[‘new’])));
$new_test = $new_test2 . “.”;
$new = $_POST[‘new’];

if($pre == null){
//No previous conversation
echo ‘<textarea rows=”10″ cols=”59″>’ . $bot_name . ‘: ‘ . $start_text . ‘</textarea><br><br>Input: <form action=”chatbot.php?id=’ . $id . ‘” method=”post” name=”form”><input type=”text” name = “new”><input type=”hidden” name = “pre” value=”‘ . $bot_name . ‘: ‘ . $start_text . ‘”></form>’;

}else{
//History found
$last_response == $_POST[‘last_response’];
$de_response == $_POST[‘de_response’];
$y = count($input_keys);
$x = 0;
//loop through input keys until match is found with input.
while(($x < $y) && ($input_keys[$x] != null)){
$key = str_replace($words, $word_replacement, strtolower($input_keys[$x]));
$key1 = strstr($new_test, $key . ” “);
$key2 = strstr($new_test, $key . “.”);
$key3 = strstr($new_test, ” ” . $key);
if(($key1 != false) || ($key2 != false) || ($key3 != false)){
$ran = rand(1,2);
if($ran == “1”){
$response = $responses1[$x];
}else{
$response = $responses2[$x];
}
$key_found = 1;
break;
}
$x++;
}

if($key_found != 1){
if($de_response == “1”){
$response = $default_response1;
$de_response = “2”;
}elseif($de_response == “2”){
$response = $default_response2;
$de_response = “3”;
}elseif($de_response == “3”){
$response = $default_response3;
$de_response = “4”;
}elseif($de_response == “4”){
$response = $default_response4;
$de_response = “5”;
}else{
$response = $default_response5;
$de_response = “1”;
}
}

//Special intelligence
$full_stop = strpos($new_test,”.”);
$first_sentence = strtolower(substr($new_test,0,$full_stop));

$input_words = array(“yourself”, “myself”, “are you”, “you are”,”you will”, “you”,”i am”,”am i”, “I”, “were”, “was”, “my”, “mine”);
$bot_view = array(“myself”,”yourself”, “am I”, “I am”, “I will”,”me”,”you are”,”are you”, “you”, “was”, “were”, “your”, “your’s”);

$converted_first_sentence = $first_sentence;
$string_complete = 0;
for($x = 0; $x <= 12; $x++){
$pos = strpos($first_sentence,$input_words[$x]);
if(($pos !== false) && ($pos >= $string_complete)){
$input_word_len = strlen($input_words[$x]);
$converted_first_sentence = substr_replace($converted_first_sentence,$bot_view[$x],$pos,$input_word_len);
$bot_view_len = strlen($bot_view[$x]);
$string_complete = $pos + $bot_view_len – 1;
}
}

$response2 = str_replace(“<previous>”, $new, $response);
$response = str_replace(“<sentence>”, $first_sentence, $response2);
$response2 = str_replace(” i “,” I “,ucfirst(str_replace(“<viewpoint>”, $converted_first_sentence, $response)));

//Finalize and echo data.
$content2 = $pre . “nYou: ” . $new . “n” . $bot_name . “: ” . $response2;
$content = str_replace(‘\’,”,$content2);
echo ‘<textarea id=”textfield” rows=”10″ cols=”59″>’ . $content . ‘</textarea><br><br>Input: <form action=”chatbot.php?id=’ . $id . ‘” method=”post” name=”form”><input type=”text” name = “new”><input type=”hidden” name = “pre” value=”‘ . $content . ‘”><input type=”hidden” name = “last_response” value=”‘ . $last_response . ‘”><input type=”hidden” name = “de_response” value=”‘ . $de_response . ‘”></form>’;

}[/code]

Sorry I’m not too good with php either ?

Thanks if anyone can help me with this.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @god0fgod spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...