博特曼陷入对话课程的循环之中

时间:2019-03-13 00:24:51

标签: php

Botman很棒,但是我在部分对话中遇到了问题。它似乎陷入了无休止的循环(停止它的唯一方法是杀死bot进程并清除其缓存)。

这里是我想做什么:

我正在查询API,并且某些API结果以商标名称作为前缀,而Im会在需要时在该API中查找相关产品时尝试添加商标名称。

因此,我所做的就是将所有品牌产品放入array(),Im使用in_array($needle, $haystack)查找产品并在需要时添加品牌名称。

这是主要的对话功能:

  

Bot:您要查找什么?

     

用户:产品名称

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']);
                }


            }



        });


    }



}

如果我查询产品列表中的某些内容,结果就是品牌产品名称的源源不断(如果我们要输入“ Afgoo”,我们将得到以下结果:

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

但是..

它正常工作,并且如果查询所针对的菌株不在品牌列表中,则仅显示一次结果。

那么,如何使品牌列表像非品牌列表一样工作?我想在$bhang array()中列出的菌株的查询开头添加品牌名称“ Bhang”。

如果您正在使用Telegram,也可以自己查看,将消息正文发送到“ CocoTheWeedBot”,消息正文为:“ strain”(触发压力对话),然后一个很好的例子是回复机器人问题:“ afgoo”(或$ bhang数组中的任何内容)

1 个答案:

答案 0 :(得分:0)

您可以通过以下命令清除僵尸程序的缓存来停止对话:

php artisan cache:clear