Skip to content

[WIP] Update examples to global loop accessor #143

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ In order to use a proxy, you can use the https://github.com/clue/php-socks-react

use Clue\React\Socks\Client;

$loop = Factory::create();
// Replace PROXY_ADDRESS and PROXY_PORT with the correct configuration
$proxy = new Client('socks5://' . PROXY_ADDRESS . ':' . PROXY_PORT, new Connector($loop));
$handler = new HttpClientRequestHandler($loop, [
$handler = new HttpClientRequestHandler(Loop::get(), [
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down Expand Up @@ -105,16 +104,14 @@ use \unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Telegram\Methods\SendMessage;

$loop = \React\EventLoop\Factory::create();
$handler = new HttpClientRequestHandler($loop);
$handler = new HttpClientRequestHandler(Loop::get());
$tgLog = new TgLog(BOT_TOKEN, $handler);

$sendMessage = new SendMessage();
$sendMessage->chat_id = A_USER_CHAT_ID;
$sendMessage->text = 'Hello world!';

$tgLog->performApiRequest($sendMessage);
$loop->run();
```
$tgLog->performApiRequest($sendMessage);```
(Side note: In case `React\EventLoop\Factory` cannot be resolved in the above code, add `include('vendor/autoload.php')` to your PHP file).

With the `SendMessage()` object, you can create a message to be sent through the TgLog object.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"clue/block-react": "^1.3",
"yoshi2889/multipart-builder": "^0.1.1",
"amphp/artax": "^3",
"react/http": "^1.1"
"react/http": "^1.1",
"react/event-loop": "^1.3"
},
"require-dev": {
"monolog/monolog": "^1.17",
Expand Down
100 changes: 44 additions & 56 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions examples/00.send-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
include __DIR__.'/basics.php';

use \React\EventLoop\Factory;
use React\EventLoop\Loop;
use \unreal4u\TelegramAPI\HttpClientRequestHandler;
use \unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Telegram\Methods\SendMessage;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$sendMessage = new SendMessage();
$sendMessage->chat_id = A_USER_CHAT_ID;
$sendMessage->text = 'Hello world!';

$tgLog->performApiRequest($sendMessage);
$loop->run();
6 changes: 3 additions & 3 deletions examples/01.blocking-get-me.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use React\EventLoop\Loop;
use \unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\GetMe;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$response = Clue\React\Block\await($tgLog->performApiRequest(new GetMe()), $loop);
$response = Clue\React\Block\await($tgLog->performApiRequest(new GetMe()), Loop::get());
var_dump($response);
5 changes: 2 additions & 3 deletions examples/02.send-message-with-custom-dns.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
include __DIR__.'/basics.php';

use \React\EventLoop\Factory;
use React\EventLoop\Loop;
use \unreal4u\TelegramAPI\HttpClientRequestHandler;
use \unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Telegram\Methods\SendMessage;

$loop = Factory::create();
// Passing along an array with options will allow you to choose a custom DNS that isn't 8.8.8.8
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop, ['dns' => '8.8.4.4']));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get(), ['dns' => '8.8.4.4']));

$sendMessage = new SendMessage();
$sendMessage->chat_id = A_USER_CHAT_ID;
$sendMessage->text = 'Hello world!';

$tgLog->performApiRequest($sendMessage);
$loop->run();
5 changes: 1 addition & 4 deletions examples/get-chat-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use unreal4u\TelegramAPI\Telegram\Methods\GetChatMember;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$getChatMember = new GetChatMember();
$getChatMember->chat_id = A_GROUP_CHAT_ID;
Expand All @@ -28,5 +27,3 @@ function (\Exception $exception) {
echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage().PHP_EOL;
}
);

$loop->run();
6 changes: 2 additions & 4 deletions examples/get-chat-members-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use React\EventLoop\Loop;
use unreal4u\TelegramAPI\Exceptions\ClientException;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\GetChatMembersCount;
use unreal4u\TelegramAPI\TgLog;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultInt;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$getCMC = new GetChatMembersCount();
$getCMC->chat_id = A_GROUP_CHAT_ID;
Expand All @@ -29,5 +29,3 @@ function (ClientException $e) {
var_dump('Captured ClientException', $e->getError());
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
use unreal4u\TelegramAPI\Telegram\Types\File;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$getFile = new GetFile();
// You can get a file id from an update, webhook or any other message object
Expand All @@ -31,5 +30,3 @@ function (File $file) use ($tgLog) {
});
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-me.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Abstracts\TelegramTypes;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$getMePromise = $tgLog->performApiRequest(new GetMe());
$getMePromise->then(
Expand All @@ -22,5 +21,3 @@ function (\Exception $e) {
var_dump($e);
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-my-commands-scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
use unreal4u\TelegramAPI\Telegram\Types\BotCommandScope;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$scopeType = 'all_private_chats';

Expand All @@ -34,5 +33,3 @@ function (\Exception $e) {
var_dump($e->getTraceAsString());
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-my-commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use unreal4u\TelegramAPI\Telegram\Types\BotCommand;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$promise = $tgLog->performApiRequest(new \unreal4u\TelegramAPI\Telegram\Methods\GetMyCommands());
$promise->then(
Expand All @@ -26,5 +25,3 @@ function (\Exception $e) {
var_dump($e->getTraceAsString());
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use \unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$getUpdates = new GetUpdates();

Expand All @@ -29,5 +28,3 @@ function (\Exception $exception) {
echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage();
}
);

$loop->run();
5 changes: 1 addition & 4 deletions examples/get-user-profile-photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$userProfilePhotos = new GetUserProfilePhotos();
$userProfilePhotos->user_id = A_USER_ID;
Expand All @@ -27,5 +26,3 @@ function (\Exception $exception) {
echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage();
}
);

$loop->run();
6 changes: 2 additions & 4 deletions examples/get-webhookinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use unreal4u\TelegramAPI\Telegram\Types\WebhookInfo;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler(Loop::get()));

$webHookInfo = new GetWebhookInfo();
$promise = $tgLog->performApiRequest($webHookInfo);
Expand All @@ -25,5 +24,4 @@ function (WebhookInfo $info) {
function (\Exception $e) {
var_dump($e);
}
);
$loop->run();
);
Loading