Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate commands from MySQL Table #97

Open
BetaStacks opened this issue Feb 19, 2019 · 1 comment
Open

Generate commands from MySQL Table #97

BetaStacks opened this issue Feb 19, 2019 · 1 comment

Comments

@BetaStacks
Copy link

BetaStacks commented Feb 19, 2019

Hi!

First off, thank you for this framework! I have been building a chatbot with some JS libraries and really wanted a way to protect some of the clever coding I have done. This is a fantastic solution.

One of my current goals to use my nice GUI CRUD editor to build out my conversations.
The script below connects to a Database and creates a command for each row in the table.

However, every response is equal to the last row's response value. Can anyone provide me with a hint on what I should be googling to find a solution? Right now I am focused on Variable Scopes.

Cheers!

Brandon

<?php
use App\Http\Controllers\BotManController;


$servername = "localhost";
$username = "USR";
$password = "PASS";
$dbname = "DB";



$botman = resolve('botman');



// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, command, responce, function FROM commands_general";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
     //   echo "<br> id: ". $row["id"]. " - Command: ". $row["command"]. " " . $row["responce"] . "<br>";
     $command = $row["command"];
     $GLOBALS['responce'. $row["id"]] = $row["responce"];
     $GLOBALS['id'] = $row["id"];
       
    $botman->hears($command, function ($bot) {
      global $id;

     // global $responce.$x;
        $bot->reply('heard you '.$id);  
        $id ++;  
});
    }
} else {
    echo "0 results";
}

$conn->close();

@Edd-G
Copy link

Edd-G commented Jul 14, 2019

Hi!
Have you figured out this question?
I have the same task.
Maybe paste command from external files?
Like this:

if ($result->num_rows > 0) {
   $tempFile = tmpfile();
   $dataTmp = stream_get_meta_data($tempFile);
   $commandFilePath = $dataTmp['uri'];
   $command = '';
    while($row = $result->fetch_assoc()) {
	    $command .= '
			$botman->hears("'.$row["command"].'", function ($bot) {
			    $bot->reply("'.$row["responce"].'");
			});
		';
	}
	fwrite($tempFile,"<?php\r\n".$command);
	include_once $commandFilePath;
	fclose($tempFile);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants