php-telegram-bot / core

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

Cant include classes correctly into user command #296

Closed savely-krasovsky closed 8 years ago

savely-krasovsky commented 8 years ago

Required Information

What SHOULD be happening?

I include tho classes and successfully use their methods.

Actual behaviour

What IS happening?

I can't include two classes. So methods from their give an errors.

Steps to reproduce

Explain how to reproduce the issue

Just add some classes two user command.

Extra details

Please post any extra details that might help solve the issue (e.g. logs)

Can I include classes into user command? I have code, like this:

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

require_once __DIR__ . "/../class/Database.class.php";
require_once __DIR__ . "/../class/System.class.php";

use Longman\TelegramBot\Request;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ForceReply;
use Longman\TelegramBot\Entities\ReplyKeyboardHide;
use Longman\TelegramBot\Entities\ReplyKeyboardMarkup;

class SomeUserCommand extends UserCommand
{
// Some methods from System and Database
}
noplanman commented 8 years ago

Hi @Lord-Protector

You can use it like that if you like, I don't understand why this shouldn't work.

What error message do you get?

jacklul commented 8 years ago

It should work, there must be different issue there, maybe the classes name conflict?

I've used it before: https://github.com/jacklul/Tic-Tac-Toe-Bot/blob/master/Commands/CallbackqueryCommand.php#L11

savely-krasovsky commented 8 years ago

Thanks for answers. @jacklul my class has "Database" name. I didn't find class with this name in API. So I don't think that it's a conflict. I use Database and System methods in /hook.php and they work perfect! I just copy and paste those code strings from hook.php to user command code.

Some fragment:

<?php
require_once __DIR__ . "/class/Database.class.php";
require_once __DIR__ . "/class/System.class.php";
require __DIR__ . '/vendor/autoload.php';

$tg = Database::getService('sendUpdateService');
$pieces = explode(';', $tg['address']);
$API_KEY = $pieces[0];
$USER_ID = $pieces[1];

// Some code, handler, etc.

@noplanman And... I am novice at php and I don't know how to correctly debug php-code. On good it is necessary to setup Linux, Nginx, MySQL, PHP, Xdebug and ALL that I use on my VPS with real bot. Is there other way? For example remote debugging directly on VPS? Or can I just use var_dump in my UserCommands?

Sorry for my bad English...

noplanman commented 8 years ago

About the files and classes you're requireing. If you add it in your hook.php file, you can use the classes and functions everywhere in the whole bot. If you only require them in the command itself, it will only work after that command has been loaded. So in your case, just have it in the hook.php and it should work perfectly!

Depending on the tools you're using, the easiest way to find the problem quickly, is to use var_dump and $telegram->setCustomInput(). First you must make sure that your PHP errors are being output. Then activate the logging and get one of the update strings and pass it to the setCustomInput method. You can then enter the URL of your hook.php directly in your browser to see the var_dump output directly, or any error that may be happening.

Also, I strongly recommend you to set up a local development environment, so that you don't need to do everything on your "live" VPS installation. If you have questions about that, just ask.

Here an example for the debugging:

<?php

require_once __DIR__ . '/class/Database.class.php';
require_once __DIR__ . '/class/System.class.php';
require_once __DIR__ . '/vendor/autoload.php';

$tg = Database::getService('sendUpdateService');
$pieces = explode(';', $tg['address']);
$API_KEY = $pieces[0];
$USER_ID = $pieces[1];
$BOTNAME = 'my_bot';

$telegram = new Telegram($API_KEY, $BOT_NAME);

// Log updates.
TelegramLog::initUpdateLog($BOT_NAME . '_update.log');

// Set custom input.
$telegram->setCustomInput('your-json-update-string');

// etc. etc.

Then in your command, just use var_dump.

savely-krasovsky commented 8 years ago

@noplanman thanks for so detailed answer. $telegram->setCustomInput() is good, but yes, it is really hard to catch some bugs. I can't really understand why I cant use class methods. In the hook.php code they work perfect. But in command -- no! It's enouth to comment them and all command works good. But I really need to get from my personal DB some strings... :(

noplanman commented 8 years ago

So you haven't found the problem withvar_dump yet? It must work, as I've been doing it like that too.

I can't understand why you can't access them from your command, when you require them in hook.php, so strange.

noplanman commented 8 years ago

Also, maybe you forgot to put the \ before the class name when accessing it?

Because hook.php isn't in a specific namespace, just using Database:: works fine. Inside a command, you will need to use \Database::, as it will otherwise search for that class within the namespace, which obviously doesn't exist.

savely-krasovsky commented 8 years ago

@noplanman THANKS! This is little backslash solve my problem! Very grateful for your help!