What is the best and safest way to implement my own functions? Should I include them in hook.php?
I want my functions to be able to use the current exiting classes (this plugin's) as well (like getMessage()).
I'm not much familiar with classes in programming.
Maybe this wiki article can help you:
https://github.com/php-telegram-bot/core/wiki/Snippets
I personally use composer autoloader with my own namespaces, putting all classes in one directory.
I'd personally go with @jacklul's suggestion too, to implement all your code in own classes.
I'm not much familiar with classes in programming.
This is why I thought of pointing you to the wiki ๐
@jacklul, I don't have access to terminal with my hosting plan. Can I still use composer for that purpose?
I believe there were wrappers to use composer from webspace, you can always run composer locally and then upload vendor directory.
@jacklul thank you very much for helping.
@noplanman, I'm now more familiar with and interested in OOP. Can you give me some examples on how to implement my own helper class using composer with my own namespaces?
@ttvd94 Great to hear that you're getting more into OOP :tada:
To use the composer autoloader, I suggest to get a bit familiar with it by reading here:
https://getcomposer.org/doc/04-schema.md#autoload
Depending on how you want to manage your namespaces, you need to choose the specific autoloading method.
Give it a try and let me know if you need more help!
@noplanman I read the article and also watched some youtubes, but couldn't get the autoloader to work. I keep getting this error:
PHP Fatal error: Uncaught Error: Class 'Longman\TelegramBot\Helpers' not found in /path_to_bot/Commands/StartCommand.php:59
Here is my composer.json file:
{
"name": "telegram-bot/project-name",
"type": "project",
"description": "Project description",
"license": "MIT",
"autoload": {
"psr-4": {
"Helpers\\": "Helpers/"
}
},
"require": {
"longman/telegram-bot": "*"
}
}
And here is my folder structure:
bot-root/
โโโ commands/
โโโ Helpers/
โ โโโ Helpers.php
โโโ vendor/
โโโ composer.json
โโโ composer.lock
โโโ hook.php
And StartCommand.php looks like:
<?php
namespace Longman\TelegramBot\Commands\SystemCommands;
use Longman\TelegramBot\Commands\SystemCommand;
use Longman\TelegramBot\Helpers;
use Longman\TelegramBot\Request;
class StartCommand extends SystemCommand
{
protected $name = 'start';
protected $description = 'Start command';
protected $usage = '/start';
protected $version = '1.1.0';
protected $private_only = true;
public function execute()
{
$message = $this->getMessage();
$helpers = new Helpers();
return Request::sendMessage(['chat_id'=> $chat_id, 'text'=> $helpers->hey()]);
}
}
And Helpers.php:
```
namespace LongmanTelegramBot;
class Helpers {
public function __construct() {}
public function hey() {
return 'heyy';
}
}
```
Ah of course, because you'll have to add the namespace you used in your Helper.php file, which should be namespace Helpers;.
So in your StartCommand.php you need to put use Helpers\Helpers; (instead of use Longman\TelegramBot\Helpers;)
@ttvd94 Can we close here then?
Sure. Thanks for your help @noplanman <3
Most helpful comment
Ah of course, because you'll have to add the namespace you used in your
Helper.phpfile, which should benamespace Helpers;.So in your
StartCommand.phpyou need to putuse Helpers\Helpers;(instead ofuse Longman\TelegramBot\Helpers;)