0

I am working with a PHP based open source chat bot and trying to save the message the end user typed in to the bot that triggered the "fallback" function. so that I can see what sorts of things people are asking the bot that I might not of built logic/answers to. However my issue is the mysql insert does not seem to work. if I take my actual query and past it into phpmyadmin the query works but in the php page its not working so trying to figure out what it might not be working.

Here is my full code

<?php
  require_once('../vendor/autoload.php');

  use BotMan\BotMan\BotMan;
  use BotMan\BotMan\BotManFactory;
  use BotMan\BotMan\Drivers\DriverManager;
  use BotMan\BotMan\Middleware\ApiAi;

  DriverManager::loadDriver(\BotMan\Drivers\Web\WebDriver::class);

  $config = [
     'web' => [
                 'matchingData' => [
                 'driver' => 'web',
                 ],
              ]
            ];

  $botman = BotManFactory::create($config);
  $dialogflow = ApiAi::create('123abc')->listenForAction();

   // Apply global "received" middleware
   $botman->middleware->received($dialogflow);

   //lets open connection to the mysql database
   $chatdbconn = mysqli_connect("localhost","root","root","chatdb");
   $chatdatetime = date('Y-m-d H:i:s');

   /////////////////////////////////////
   // DIALOG FLOW
   /////////////////////////////////////
   $botman->hears('hello|customerservice_contact|brand_feeling', function (BotMan $bot) {
    // The incoming message matched the "my_api_action" on Dialogflow
   // Retrieve Dialogflow information:
   $extras = $bot->getMessage()->getExtras();
   $apiReply = $extras['apiReply'];
   $apiAction = $extras['apiAction'];
   $apiIntent = $extras['apiIntent'];

   $bot->typesAndWaits(2);
   $bot->reply($apiReply);
  })->middleware($dialogflow);


  $botman->fallback(function($bot) {
    global $chatdbconn;
    $phrase = $bot->getMessage();
    $unknownphrase = mysqli_real_escape_string($chatdbconn, $phrase->getText());


    //$created_on = date('Y-m-d H:i:s');
    mysqli_query($chatdbconn, "INSERT into chatbot_unknown_phrases(the_phrase) VALUES('$unknownphrase')");
    $bot->typesAndWaits(2);
    $bot->reply('Sorry, I did not understand the command.');
    });
      // Start listening
      $botman->listen();
    ?> 
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Jayreis
  • 253
  • 1
  • 7
  • 28
  • 1
    Your code is vulnerable to SQL injection. You should use prepared statements. – Dharman May 28 '19 at 20:06
  • Enable exceptions: https://stackoverflow.com/q/22662488/1839439 – Dharman May 28 '19 at 20:08
  • Ahh I found the mysql user didn't have the correct permissions. thanks all – Jayreis May 28 '19 at 21:15
  • Did the linked question help you? – Dharman May 28 '19 at 21:28
  • The right tool to solve such problems is your debugger. You should step through your code line-by-line before asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should [edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – Rohan Khude May 29 '19 at 18:17

0 Answers0