简体   繁体   English

Botman 在对话课上陷入了一个循环

[英]Botman is stuck in a loop in a conversation class

Botman is great, but Im having a problem with part of a conversation. Botman 很棒,但我在部分对话中遇到了问题。 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.我正在查询一个 API,其中一些 API 结果以品牌名称作为前缀,我尝试在需要时添加品牌名称以在 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.所以我所做的是将所有品牌产品放入array()然后使用in_array($needle, $haystack)查找产品并在需要时添加品牌名称。

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:如果我查询产品列表中的某些内容,结果是品牌产品名称的源源不断(如果我们要求“Afgoo”,这就是我们将得到的结果:

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() .我想将品牌名称“Bhang”添加到$bhang array()列出的菌株的查询$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)如果您在使用 Telegram,您也可以亲眼看看,向“CocoTheWeedBot”发送一条消息,消息正文为:“strain”(触发压力对话),然后一个很好的例子是回复机器人问题: “afgoo”(或 $bhang 数组中的任何内容)

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

php artisan cache:clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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