php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.87k stars 952 forks source link

About Connection With Database (not bot database) #926

Closed bryanfranklyn closed 4 years ago

bryanfranklyn commented 5 years ago

I want to use my database with medoo framework (database framework) it is posible ?

i use genericmessagecomand with regex to find words that would be the keyword to query on my database(i use database framework) with function "caridata" and return $text this text will send to user of my bot.

but when i place require'database.php' on genericmessagecomend to call my function 'caridata' bot not working(no respon from bot for any comand i try) happy if anyone helps me

Here my genericmessagecommand.php

<?php
namespace Longman\TelegramBot\Commands\SystemCommands;

use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Request;

class GenericmessageCommand extends SystemCommand
{

    protected $name = 'genericmessage';

    protected $description = 'Handle generic message';

    protected $version = '1.1.0';

    protected $need_mysql = true;

    public function executeNoDb()
    {
        // Do nothing
        return Request::emptyResponse();
    }
    public function execute()
    {

        $message = $this->getMessage();
        $chat_id = $message->getChat()->getId();
        $user_id = $message->getFrom()->getId();
        $pesan = trim($message->getText(true));

        if (preg_match("/\D{3}\/\d+/i", $pesan)){
            $text = caridata($pesan);
                    $data = [
            'chat_id' => $chat_id,
            'text'    => $text,
            'parse_mode' => 'markdown',
        ];

        return Request::sendMessage($data);
        }

        //If a conversation is busy, execute the conversation command after handling the message
            $conversation = new Conversation(
            $this->getMessage()->getFrom()->getId(),
            $this->getMessage()->getChat()->getId()
        );

        //Fetch conversation command if it exists and execute it
        if ($conversation->exists() && ($command = $conversation->getCommand())) {
            return $this->telegram->executeCommand($command);
        }

        return Request::emptyResponse();
    }
}

and here my database.php that contain the search function (caridata) and this database.php file require medoo framework (database framework)

<?php

// masukkan database framework nya
require_once 'medoo.php';

// Using Medoo namespace
use Medoo\Medoo;
try {
   $database = new Medoo([
        'database_type' => 'mysql',
        'database_name' => '*****',
        'server' => 'localhost',
        'username' => '******',
        'password' => '*****',
        'charset' => 'utf8'
    ]);
}
catch (Exception $e) {
    echo $e->getMessage();
}

function caridata($pesan)
{
    global $database;
    $hasil = '😢 Maaf site_id tidak ditemukan..';
    $datas = $database->select('data_mancore','*',['nama_odp' => $pesan]);
    $jml = count($datas);
    if ($jml > 0) {
        $hasil = "✍🏽 *$jml ODP DITEMUKAN*\n";
        $n = 0;
        foreach ($datas as $data) {
            $n++;
            $hasil .= "\n$n. ".substr($data['nama_odp'], 0, 7)."\n \n";
            $hasil .= "*STO         :* `$data[sto]`\n";
            $hasil .= "*IP GPON     :* `$data[ip_gpon]`\n";
            $hasil .= "*PORT OLT    :* `$data[port_olt]`\n";
            $hasil .= "*SPLITER ODP :* `$data[spliter_odp]`\n";
            $hasil .= "\n👀 /lihat $data[id_ftm]\n";
        }
    }

    return $hasil;
}
noplanman commented 5 years ago

From your code above, I can't see any import of database.php in your GenericmessageCommand.php file.

You should be able to put it at the beginning of your execute() method and it should work. Make sure that the file paths are correct. For best practice, use the absolute path when requiring/including files (e.g. require __DIR__ . '/../path/to/database.php';)

noplanman commented 5 years ago

@bryanfranklyn Did you manage to get this working? Can we close here?