Added single-threaded and multi-threaded example
This commit is contained in:
parent
da6abceed2
commit
040426a63c
9 changed files with 110 additions and 15 deletions
1
.idea/php.xml
generated
1
.idea/php.xml
generated
|
@ -14,6 +14,7 @@
|
||||||
<path value="/usr/share/php" />
|
<path value="/usr/share/php" />
|
||||||
<path value="/etc/ncc" />
|
<path value="/etc/ncc" />
|
||||||
<path value="/var/ncc/packages/net.nosial.tamerlib=1.0.0" />
|
<path value="/var/ncc/packages/net.nosial.tamerlib=1.0.0" />
|
||||||
|
<path value="/var/ncc/packages/net.nosial.loglib=1.0.0" />
|
||||||
</include_path>
|
</include_path>
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
|
<component name="PhpProjectSharedConfiguration" php_language_level="8.2" />
|
||||||
|
|
|
@ -16,6 +16,14 @@
|
||||||
"build": {
|
"build": {
|
||||||
"source_path": "src",
|
"source_path": "src",
|
||||||
"default_configuration": "release",
|
"default_configuration": "release",
|
||||||
|
"dependencies": [
|
||||||
|
{
|
||||||
|
"name": "net.nosial.loglib",
|
||||||
|
"version": "latest",
|
||||||
|
"source_type": "remote",
|
||||||
|
"source": "nosial/libs.log=latest@n64"
|
||||||
|
}
|
||||||
|
],
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "release",
|
"name": "release",
|
||||||
|
|
|
@ -21,6 +21,23 @@
|
||||||
const ChatPhotoChanged = 'chat_photo_changed';
|
const ChatPhotoChanged = 'chat_photo_changed';
|
||||||
Const CallbackQuery = 'callback_query';
|
Const CallbackQuery = 'callback_query';
|
||||||
|
|
||||||
|
const All = [
|
||||||
|
self::GenericUpdate,
|
||||||
|
self::Message,
|
||||||
|
self::EditedMessage,
|
||||||
|
self::GenericCommandMessage,
|
||||||
|
self::ChatMemberJoined,
|
||||||
|
self::ChatMemberLeft,
|
||||||
|
self::ChatMemberKicked,
|
||||||
|
self::ChatMemberBanned,
|
||||||
|
self::ChatMemberUnbanned,
|
||||||
|
self::ChatMemberPromoted,
|
||||||
|
self::ChatMemberDemoted,
|
||||||
|
self::ChatMemberRestricted,
|
||||||
|
self::ChatMemberUnrestricted,
|
||||||
|
self::ChatTitleChanged,
|
||||||
|
self::ChatPhotoChanged,
|
||||||
|
self::CallbackQuery,
|
||||||
|
];
|
||||||
|
|
||||||
}
|
}
|
10
tests/autoload.php
Normal file
10
tests/autoload.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Import ncc
|
||||||
|
require 'ncc';
|
||||||
|
|
||||||
|
import('net.nosial.tgbotlib');
|
||||||
|
|
||||||
|
// Require commands & event handlers
|
||||||
|
require 'commands' . DIRECTORY_SEPARATOR . 'StartCommand.php';
|
||||||
|
require 'commands' . DIRECTORY_SEPARATOR . 'HashCommand.php';
|
26
tests/commands/HashCommand.php
Normal file
26
tests/commands/HashCommand.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace commands;
|
||||||
|
|
||||||
|
use TgBotLib\Bot;
|
||||||
|
use TgBotLib\Exceptions\TelegramException;
|
||||||
|
use TgBotLib\Interfaces\CommandInterface;
|
||||||
|
use TgBotLib\Objects\Telegram\Update;
|
||||||
|
|
||||||
|
class HashCommand implements CommandInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param Bot $bot
|
||||||
|
* @param Update $update
|
||||||
|
* @return void
|
||||||
|
* @throws TelegramException
|
||||||
|
*/
|
||||||
|
public function handle(Bot $bot, Update $update): void
|
||||||
|
{
|
||||||
|
// Usage: /hash <text>
|
||||||
|
$data = str_replace('/hash ', '', $update->getMessage()->getText());
|
||||||
|
$bot->sendMessage(
|
||||||
|
$update->getMessage()->getChat()->getId(), md5($data)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
require 'ncc';
|
|
||||||
import('net.nosial.tgbotlib');
|
|
||||||
|
|
||||||
require 'commands/StartCommand.php';
|
|
||||||
|
|
||||||
$bot = new TgBotLib\Bot('865804194:AAHTo9aIFP5X47dMYLJ6eoldHJnM6sc3LBc');
|
|
||||||
$bot->setCommandHandler('start', new \commands\StartCommand());
|
|
||||||
|
|
||||||
print_r(json_encode($bot->getMe()->toArray()) . PHP_EOL);
|
|
||||||
$bot->sendMessage(570787098, 'Hello, world!');
|
|
||||||
$bot->handleGetUpdates();
|
|
||||||
unset($bot);
|
|
19
tests/multi_threaded.php
Normal file
19
tests/multi_threaded.php
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
import('net.nosial.tamerlib');
|
||||||
|
|
||||||
|
$bot = new TgBotLib\Bot(getenv('BOT_TOKEN'));
|
||||||
|
TamerLib\Tamer::init(\TamerLib\Abstracts\ProtocolType::Gearman, ['127.0.0.1:4730']);
|
||||||
|
TamerLib\Tamer::addWorker(__DIR__ . DIRECTORY_SEPARATOR . 'worker.php', 10);
|
||||||
|
|
||||||
|
var_dump('Starting workers');
|
||||||
|
TamerLib\Tamer::startWorkers();
|
||||||
|
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
foreach($bot->getUpdates() as $update)
|
||||||
|
{
|
||||||
|
TamerLib\Tamer::do(TamerLib\Objects\Task::create('handle_update', json_encode($update->toArray())));
|
||||||
|
}
|
||||||
|
}
|
10
tests/single_threaded.php
Normal file
10
tests/single_threaded.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
|
||||||
|
$bot = new TgBotLib\Bot(getenv('BOT_TOKEN'));
|
||||||
|
|
||||||
|
$bot->setCommandHandler('start', new \commands\StartCommand());
|
||||||
|
$bot->setCommandHandler('hash', new \commands\HashCommand());
|
||||||
|
|
||||||
|
$bot->handleGetUpdates(true);
|
18
tests/worker.php
Normal file
18
tests/worker.php
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
import('net.nosial.tamerlib');
|
||||||
|
|
||||||
|
$bot = new TgBotLib\Bot('865804194:AAHTo9aIFP5X47dMYLJ6eoldHJnM6sc3LBc');
|
||||||
|
|
||||||
|
$bot->setCommandHandler('start', new \commands\StartCommand());
|
||||||
|
$bot->setCommandHandler('hash', new \commands\HashCommand());
|
||||||
|
|
||||||
|
TamerLib\Tamer::initWorker();
|
||||||
|
|
||||||
|
TamerLib\Tamer::addFunction('handle_update', function (\TamerLib\Objects\Job $job) use ($bot)
|
||||||
|
{
|
||||||
|
$bot->handleUpdate(\TgBotLib\Objects\Telegram\Update::fromArray(json_decode($job->getData(), true)));
|
||||||
|
});
|
||||||
|
|
||||||
|
TamerLib\Tamer::work();
|
Loading…
Add table
Add a link
Reference in a new issue