Skip to content

Commit

Permalink
fix snake_case functions. fix comments. better error info. reorder fu…
Browse files Browse the repository at this point in the history
…nctions. marge server run method. fix variables.
  • Loading branch information
naftali100 committed Jul 13, 2021
1 parent 844c8a3 commit b9dc652
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 191 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ composer.phar
/vendor/
/.vscode
test.php
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
config.json
/test
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ the recomended way is to use amphp's server to run all your bots
#### server.php

```php
$server = new Server("127.0.0.1:8080"); // create server instance running on port 8080
$server = new Server("127.0.0.1:8080"); // create server instance listening to port 8080
$server->load_file("bot.php", "index"); // load the handlers in "bot.php" and store them in "index" path
$server->load_folder("folder", true); // load all files in a folder. the second param is whether to load recursivly or not
$server->run();
Expand Down Expand Up @@ -44,7 +44,7 @@ a lot more hanlder, config and server options in examples folder.

there is 4 main objects in the library

1. Server: extends Loader. load files and runing the http-server and activating handlers.
1. Server: extends Loader. load files, runing the http-server and activating handlers.
2. Config: configuration.
3. Update: extends API and HTTP. contains all the method to send request to bot api.
4. Handler: create handlers that will run asyncronicly.
Expand Down Expand Up @@ -86,7 +86,7 @@ All bot api method and some more in this class. instance of this class is passed

telegram api methods - https://core.telegram.org/bots/api#available-methods

Added methods:
#### Added methods:

- reply: reply to the message.
- delete: delete the message.
Expand All @@ -101,6 +101,7 @@ Added methods:

Also there is a lot of preset variables to many update parts. see update.php file.

#### variables
Partial list:

- chat: the chat where the message sent.
Expand All @@ -112,7 +113,7 @@ Partial list:

you can access the update as object or as array. `$u->message->chat->id` or `$u['message']['chat']['id']`.

you can skip the update type (message is above example).
you can skip the update type (message in above example).

`$u->data` will be the callback data in callback update. `$u->message_id` is the message_id.

Expand Down
6 changes: 2 additions & 4 deletions examples/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
$server->load_file("manager.php", "index.php", true); // this file is load with extra access.
$server->load_folder("folder_full_of_bots");

// you can choose whether run the server with one thread or multiple
// you can choose whether run the server with one thread or multiple. pass 'true' param to run method to run cluster
// if you use cluster you have to run the server with vendor/bin/cluster server.php
$server->run();
// OR
$server->runCluster();
$server->run();
2 changes: 1 addition & 1 deletion src/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public function text_adjust($text)
}

function __call($func, $args){
$camelCaseFunc = str_replace(' ', '', ucwords(str_replace('-', ' ', $func)));
$camelCaseFunc = str_replace(' ', '', ucwords(str_replace(['_', '-'], ' ', $func)));
if(method_exists($this, $camelCaseFunc)){
return $this->$camelCaseFunc(...$args);
}else{
Expand Down
5 changes: 2 additions & 3 deletions src/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Config{
public $ParseMode;

/**
* @var $server the base url of the server to send requests
* @var $server the base url of the server to send api requests.
*/
public $server = 'https://api.telegram.org/bot';

Expand All @@ -32,7 +32,6 @@ class Config{
*/
public $async = true;


/**
* @var $debug show debug info
*/
Expand All @@ -51,7 +50,7 @@ class Config{
public $apiErrorHandler = null;

/**
* set timeout and inactivity time upload and download requests.
* timeout and inactivity time for upload and download requests.
*/
public $fileRequstsTimeout = 30;

Expand Down
6 changes: 3 additions & 3 deletions src/handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public function __construct(Update|null $update = null){
$this->update = $update;
}

public function run($data){
list($config, $update) = $data;
public function activate($config, $update){

if ($config->token === null)
throw new \Error('token not set');
Expand Down Expand Up @@ -68,7 +67,8 @@ public function run($data){
}
return $res;
} catch (\Throwable $e) {
print $e->getMessage() . ' when running handlers in ' . $e->getFile() . ' line ' . $e->getLine() . PHP_EOL;
// TODO: get backtrace to the file where the error coming from
print $e->getMessage() . ', when running handlers in ' . $e->getFile() . ' line ' . $e->getLine() . PHP_EOL;
if (isset($this->on_error)) {
return $this->on_error->runHandler($e, $config->async) ?? [];
}
Expand Down
43 changes: 22 additions & 21 deletions src/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function Request($url, $data = [], $async = true)
*
* `promise|request` - to yield amp's response object.
*
* `update` - to yield resulse in update objcet.
* `update` - to yield result in update object.
*
* by default the Response promise yields Update object.
*
Expand All @@ -99,6 +99,27 @@ public function Request($url, $data = [], $async = true)
class Response implements \Amp\Promise {
public function __construct(private $request, private $config){ }

public function __get($key)
{
switch ($key) {
case 'result':
case 'response':
return $this->get_res();
break;
case 'decode':
case 'array':
return $this->get_decoded_res(true);
break;
case 'promise':
case 'request':
return $this->request;
break;
case 'update':
default:
return $this->get_update();
}
}

private function get_update(){
$return_update = function($req, $conf){
$res = yield $req;
Expand All @@ -123,26 +144,6 @@ private function get_decoded_res($array = false){
};
return call($return_decoded_response, $this->request);
}

public function __get($key){
switch($key){
case 'result':
case 'response':
return $this->get_res();
break;
case 'decode':
case 'array':
return $this->get_decoded_res(true);
break;
case 'promise':
case 'request':
return $this->request;
break;
case 'update':
default:
return $this->get_update();
}
}

public function onResolve(callable $cb)
{
Expand Down
4 changes: 2 additions & 2 deletions src/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public function load_folder($path, $recursive = false){
public function load_file($file_name, $as = null, $extraAccess = false){
if(is_file($file_name)){
if($extraAccess) list($handler, $config) = $this->include_file_this($file_name);
else list( $handler, $config) = self::include_file_static($file_name);
else list($handler, $config) = self::include_file_static($file_name);

$path = $file_name;
if($as)
$path = $as;

$this->files[$path] = ['file_name' => $file_name, 'active' => 1, 'handler' => $handler, 'config' => $config];
}else{
print 'file $file_name not fount' . PHP_EOL;
print 'file '. $file_name .' not fount' . PHP_EOL;
}
}

Expand Down
Loading

0 comments on commit b9dc652

Please sign in to comment.