php botman 1

php botman

 use BotMan\BotMan\BotMan;
use BotMan\BotMan\Messages\Incoming\Answer;
 
 public function handle()
    {
       $botman=app("botman");

       $botman->hears("{message}",function($botman,$message)
       {
         if($message=="hi")
         {
           $this->askName($botman);
         }
         else
         {
           $botman->reply("kindly write hi to start the talk");
         }
       });

       $botman->listen();

    }


    public function askName($botman)
    {
      $botman->ask("Hello what is your name",function(Answer $answer)
      {
        $name=$answer->getText();
        $this->say("Nice to meet you"." ".$name);

      });
    }

Here is what the above code is Doing:
1. We are creating a function called handle() which will be called when the bot is started.
2. We are creating a variable called $botman which will be used to create a new instance of BotMan.
3. We are using the hears() method to listen to the messages sent by the user.
4. We are using the ask() method to ask the user for his/her name.
5. We are using the say() method to reply to the user.

Now, we need to create a route for the above controller.

Route::post(‘/botman’, ‘BotManController@handle’);

Now, we need to create a view for the bot.




BotMan Studio



Similar Posts