1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help with php chatbot response time.

Discussion in 'PHP & Perl' started by juggerburn, Jan 1, 2010.

  1. juggerburn

    juggerburn Junior Member

    Joined:
    May 21, 2009
    Messages:
    113
    Likes Received:
    11
    so i signed up with imified which bascially lets you run a chat bot for popular instant messaging programs from your website.

    here is the example they use slightly modified by me:

    --------------------------------------------------------------
    <?php
    switch ($_REQUEST['step']) {
    case 1:
    echo "Hi, what's your name?";
    break;
    case 2:
    echo "Hi " . $_REQUEST['value1'] . ", where do you live?";
    break;
    case 3:
    echo "Well, welcome to this hello world bot.";
    break;
    }
    ?>

    --------------------------------------------------------------


    the problem with this is the responses come out right after i send a message, which makes it unrealistic. is there anyone here who knows how to code this so my bot delays it's response by say 10 seconds or so?

    help would be greatly appreciated.
     
  2. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
  3. juggerburn

    juggerburn Junior Member

    Joined:
    May 21, 2009
    Messages:
    113
    Likes Received:
    11
    thanks, though i did figure it out after prying yesterday lol, your help is definitely much appreciated by me.
    seem to be having another problem though. i'm using the sleep function now and the responses are delayed now. i guess there was no way to see the problem before because of the quick responses, but if someone sends me two separate consecutive messages, then my bot will send the first response 10 seconds later, and send my next response 10 seconds after that without the person having sent another message.

    i want it to where no matter how many messages a person sends me before my bot can respond, it only sends out one response. say if someone goes crazy and sends my bot like 5 messages before it can respond, it don't want my bot to prep itself for 5 messages, just one. i want it to where if someone can hypothetically send like 7 quick messages before my bots response, but my bot treats it like it has only recieved one message. this might sound more unnatural to some of you, but i think it's better.

    here is what it looks like now:

    <?php
    // sleep for 10 seconds
    sleep(10);
    switch ($_REQUEST['step']) {
    case 1:
    echo "Hi, what's your name?";
    break;
    case 2:
    echo "Hi " . $_REQUEST['value1'] . ", where do you live?";
    break;
    case 3:
    echo "Well, welcome to this hello world bot, " . $_REQUEST['value1'] . "<br>from " . $_REQUEST['value2'] . ".<reset>";
    break;
    }
    ?>
     
    Last edited: Jan 1, 2010
  4. juggerburn

    juggerburn Junior Member

    Joined:
    May 21, 2009
    Messages:
    113
    Likes Received:
    11
    hmmm, looks like the rand function is what i'm looking for, let me see.
     
  5. juggerburn

    juggerburn Junior Member

    Joined:
    May 21, 2009
    Messages:
    113
    Likes Received:
    11
    another way to visualize the description of this is issue:

    this is how my bot currently operates:

    someone sends my bot 5 messages before my bot has a chance to respond. After this, for testing purposes, they send no more messages and wait to see what the bot does. my bot responds after 10 seconds and keeps responding in ten second intervals until it has sent the user 5 responses.

    this is how i want it to be:

    user sends bot 5 messages before the bot can respond, and for testing purposes decides to send no more. the bot wakes up in 10 seconds after initial contact from tester and sends response. tester waits and waits, and does not get another response from the bot until the tester decides to send another message, whether it be 1 or 20 (however many you can fit into a 10 second time frame) only to be met with just one response message from the bot. until he decides to send another message and so on.
     
    Last edited: Jan 1, 2010
  6. cyklotrial

    cyklotrial Regular Member

    Joined:
    Oct 13, 2008
    Messages:
    248
    Likes Received:
    82
    Location:
    Wonderland
    It's a tipical critical section problem.
    Ok here is what u have to do.

    1)Create new session when the conversation is beginning.
    2)Create temporary txt file which name will be session ID
    3)when script will get first msg - lock this txt file
    4)use your response function
    5)unlock txt file
    6)when the conversation is ending - delet txt file

    no i'll show u how to make step's 3-5 cos making steps 1,2 and 6 are very easy.

    PHP:
    <?php
    $file
    ="session_id.txt";

    $fp fopen($file"r+");

    if (
    flock($fpLOCK_EX)) { //lock file
        /*
        ** 
        **    here put your response function
        **
        */    
        
    flock($fpLOCK_UN); // release the lock


    fclose($fp);
    ?>

    ALGORITHM:
    -Get question
    -Set delay
    -Send response
    -Wait for next question
    -Jump to first step

    This way script will allways send response only for first question and this which was sent after script response.

    I hope you will understand :)
    Sorry for my english.

    regards
    Cyklotrial
     
  7. juggerburn

    juggerburn Junior Member

    Joined:
    May 21, 2009
    Messages:
    113
    Likes Received:
    11
    thanks for the help man,