简体   繁体   中英

Botman is stuck in a loop in a conversation class

Botman is great, but Im having a problem with part of a conversation. It seems to be stuck in an endless loop (the only way to stop it is to kill the bot process and clear its cache).

Heres what I want to do:

Im querying an API and some of the API results have a brand name as the prefix and Im trying to add the brand name when needed to find the relevant product in the API.

So what Ive done is put all the branded products into an array() and Im using in_array($needle, $haystack) to find the products and add the brand name if needed.

So here is the main conversation function:

Bot: What do you want to look up?

User: product name

public function lookupStrain() {


        
        // ASK: WHAT STRAIN ARE WE GOING TO QUERY THE API FOR
        $this->ask('What strain do you want to know more about?', function($answer) { 

            $this->strain = $answer->getText();


            // DUMB: these are the "branded" strains

            $bhang = array('Afgoo', 'Arjans Strawberry Haze', 'Blackberry Kush', 'Bubba Kush', 'Cali Orange', 'Casey Jones', 'Cheese', 'ChemDawg', 'Cherry Kola', 'Chocolope', 'Diablo OG', 'Durban Poison', 'Dutch Treat', 'Girl Scout Cookies', 'Granddaddy Purple', 'Grape Ape', 'Harlequin', 'Hawaiian Haze', 'Holy Grail', 'Island Sweet Skunk', 'J-1', 'Jack Herer', 'Juicy Fruit', 'Kali Mist', 'King Kush', 'Lavender Kush', 'Lemon Haze', 'Lemon Skunk', 'Mango OG', 'Master Kush', 'Maui Waui', 'Mr. Nice', 'Northern Lights', 'Ogre', 'Perfecto Hybrid', 'Perfecto Indica', 'Pineapple Express', 'Pineapple Kush', 'Purple Diesel', 'Purple Erkle', 'Skunk #1', 'Skywalker OG', 'Sour Diesel', 'Sour OG', 'Strawberry Cough', 'Strawberry Diesel', 'Super Lemon Haze', 'Tangie', 'Triple Berry Goo', 'White Diesel', 'White Widow', 'XJ-13', 'Yoda OG');

            // DUMBER: set brand name for strains that need it

            if (in_array(ucwords($this->strain), $bhang) == $this->strain) {
          
                // this is just a placeholder to show that we got a result, but when this event happens it just repeats endlessly

                $this->say("Bhang " . ucwords($this->strain));

            }           
            
            // NOT AS DUMB: regular strain names without the branding
            else {

                $strainQuery = ucwords($this->strain);

                            
            }

            // CURL API QUERY
            $ch =  curl_init('http://strainapi.evanbusse.com/phwOFe1/strains/search/name/' . $strainQuery);


            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 3);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
            
            $result   = curl_exec($ch);
            $j_result = json_decode($result, true);

            // loop through all api results
            foreach ($j_result as $key => $value) {
                
                if ($strainQuery == $value['name']) {
                
                    $this->say($value['desc']);
                }


            }

          

        });


    }



}

The result if I query for something thats in the product list is an endless stream of the branded products name (if we were to ask for "Afgoo" this is the result we would get:

Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
Bhang Afgoo 
and on and on..

HOWEVER..

it works properly and only displays the results once if the query is for a strain thats not on the branded list.

So how do I get the branded list to work like the non branded list? I want to add the brand name "Bhang" to the begining of the query for the strains listed in the $bhang array() .

Also you can see for yourself if you're on Telegram, send a message to "CocoTheWeedBot" with the message body: "strain" (to trigger the strain conversation" and then a good example would be to reply to the bots question with: "afgoo" (or anything in the $bhang array)

您可以通过以下命令清除botman的缓存来停止对话:

php artisan cache:clear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM