Skip to content

Commit

Permalink
First steps in checking all examples are still working
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Aug 2, 2017
1 parent 218899a commit 1ad7374
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 86 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
"authors": [
{
"name": "Camilo Sperberg",
"email": "[email protected]",
"email": "me+tgapi@unreal4u.com",
"homepage": "https://github.com/unreal4u/telegram-api/graphs/contributors"
}
],
"require": {
"php": ">=7.0.0",
"psr/log": "^1.0",
"react/http-client": "^0.5",
"react/promise": "^2.5"
"react/promise": "^2.5",
"clue/block-react": "^1.1"
},
"require-dev": {
"monolog/monolog": "^1.17",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

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

8 changes: 4 additions & 4 deletions examples/00.send-message.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

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

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

$sendMessage = new SendMessage();
$sendMessage->chat_id = A_USER_CHAT_ID;
Expand Down
16 changes: 16 additions & 0 deletions examples/01.blocking-get-me.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types = 1);

include __DIR__.'/basics.php';

use React\EventLoop\Factory;
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));

$response = Clue\React\Block\await($tgLog->performApiRequest(new GetMe()), $loop);
var_dump($response);
5 changes: 2 additions & 3 deletions examples/basics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

declare(strict_types = 1);

chdir(__DIR__ . '/../');
include 'vendor/autoload.php';
include 'examples/conf.php';
include __DIR__.'/../vendor/autoload.php';
include __DIR__.'/conf.php';

error_reporting(E_ALL);
ini_set('display_errors', '1');
20 changes: 10 additions & 10 deletions examples/get-chat-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

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

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

$getCM = new GetChatMember();
$getCM->chat_id = A_GROUP_CHAT_ID;
$getCM->user_id = A_USER_CHAT_ID;
$getChatMember = new GetChatMember();
$getChatMember->chat_id = A_GROUP_CHAT_ID;
$getChatMember->user_id = A_USER_CHAT_ID;

$promise = $tgLog->performApiRequest($getCM);

$promise->then(
$getChatMemberPromise = $tgLog->performApiRequest($getChatMember);
$getChatMemberPromise->then(
function ($response) {
echo '<pre>';
var_dump($response);
Expand Down
37 changes: 18 additions & 19 deletions examples/get-chat-members-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

use React\EventLoop\Factory;
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 = \React\EventLoop\Factory::create();
$handler = new \unreal4u\TelegramAPI\HttpClientRequestHandler($loop);
$tgLog = new TgLog(BOT_TOKEN, $handler);
$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));

$getCMC = new GetChatMembersCount();
$getCMC->chat_id = A_GROUP_CHAT_ID;

$promise = $tgLog->performApiRequest($getCMC);

$promise
->then(
function ($response) {
echo 'The number of participants in this chat are '.$response. ' members. Raw output as follows:'.PHP_EOL;
echo '<pre>';
var_dump($response);
echo '</pre>';
},
function (ClientException $e) {
var_dump('Captured ClientException', $e->getError());
}
)
;
$getChatMembersCountPromise = $tgLog->performApiRequest($getCMC);
$getChatMembersCountPromise->then(
function (ResultInt $getChatMembersCountResponse) {
echo 'The number of participants in this chat are '.$getChatMembersCountResponse. ' members'.PHP_EOL;
echo '<pre>';
var_dump($getChatMembersCountResponse);
echo '</pre>';
},
function (ClientException $e) {
var_dump('Captured ClientException', $e->getError());
}
);

$loop->run();
21 changes: 11 additions & 10 deletions examples/get-file.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramDocument;
use unreal4u\TelegramAPI\Telegram\Methods\GetFile;
use unreal4u\TelegramAPI\Telegram\Types\File;
use unreal4u\TelegramAPI\TgLog;

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

$getFile = new GetFile();
// You can get a file id from an update, webhook or any other message object
$getFile->file_id = A_FILE_ID;

$filePromise = $tgLog->performApiRequest($getFile);

$filePromise->then(
function (File $file) use ($tgLog) {
$documentPromise = $tgLog->downloadFile($file);

$documentPromise->then(function (TelegramDocument $document) {
echo '<pre>';
var_dump($document);
echo '</pre>';
$documentPromise->then(function (TelegramDocument $tgDocument) use ($file) {
// Offer to download the file immediately
header(sprintf('Content-Type: %s', $tgDocument->mime_type));
header(sprintf('Content-Length: %d', $tgDocument->file_size));
header(sprintf('Content-Disposition: inline; filename="%s"', basename($file->file_path)));
print $tgDocument;
});
}
);
Expand Down
29 changes: 12 additions & 17 deletions examples/get-me.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

use GuzzleHttp\Exception\ClientException;
use React\EventLoop\Factory;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\GetMe;
use unreal4u\TelegramAPI\TgLog;
use \unreal4u\TelegramAPI\Abstracts\TelegramTypes;

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

$getMe = new GetMe();

$promise = $tgLog->performApiRequest($getMe);

$promise->then(
function ($response) {
echo '<pre>';
var_dump($response);
echo '</pre>';
$getMePromise = $tgLog->performApiRequest(new GetMe());
$getMePromise->then(
function(TelegramTypes $getMeResponse) {
var_dump($getMeResponse);
},
function (\Exception $exception) {
// Onoes, an exception occurred...
echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage();
function(\Exception $e) {
var_dump($e);
}
);

Expand Down
25 changes: 13 additions & 12 deletions examples/get-updates.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use \unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\GetUpdates;
use unreal4u\TelegramAPI\Telegram\Types\Custom\UpdatesArray;
use \unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
use unreal4u\TelegramAPI\TgLog;

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

$getUpdates = new GetUpdates();

// Always set the offset to avoid getting duplicate updates.
// If using this method, send an offset (AKA last known update_id) to avoid getting duplicate update notifications.
#$getUpdates->offset = 328221148;

$promise->then(
function (UpdatesArray $response) {
echo '<pre>';
var_dump($response->getIterator());
echo '</pre>';
$updatePromise = $tgLog->performApiRequest($getUpdates);
$updatePromise->then(
function (TraversableCustomType $updatesArray) {
foreach ($updatesArray as $update) {
var_dump($update->update_id);
}
},
function (\Exception $exception) {
// Onoes, an exception occurred...
Expand Down
15 changes: 7 additions & 8 deletions examples/get-user-profile-photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

declare(strict_types = 1);

include 'basics.php';
include __DIR__.'/basics.php';

use GuzzleHttp\Exception\ClientException;
use React\EventLoop\Factory;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\GetUserProfilePhotos;
use unreal4u\TelegramAPI\TgLog;

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

$userProfilePhotos = new GetUserProfilePhotos();
$userProfilePhotos->user_id = A_USER_ID;

$promise = $tgLog->performApiRequest($userProfilePhotos);

$promise->then(
$userProfilePhotosPromise = $tgLog->performApiRequest($userProfilePhotos);
$userProfilePhotosPromise->then(
function ($response) {
echo '<pre>';
var_dump($response);
Expand Down

0 comments on commit 1ad7374

Please sign in to comment.