-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from micromagicman/develop
v2.1.0
- Loading branch information
Showing
18 changed files
with
458 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Micromagicman\TelegramWebApp\Api; | ||
|
||
/** | ||
* Abstract Telegram API | ||
* Possible implementations: | ||
* <ul> | ||
* <li>{@link https://core.telegram.org/#getting-started Telegram API}</li> | ||
* <li>{@link https://core.telegram.org/bots/api Telegram Telegram Bot API}</li> | ||
* </ul> | ||
*/ | ||
interface TelegramApi | ||
{ | ||
|
||
/** | ||
* Execution of API method | ||
*/ | ||
public function execute( TelegramApiMethod $apiMethod ): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Micromagicman\TelegramWebApp\Api; | ||
|
||
use UnexpectedValueException; | ||
|
||
/** | ||
* Abstract Telegram API method | ||
*/ | ||
abstract class TelegramApiMethod | ||
{ | ||
|
||
/** | ||
* Method name | ||
* By default, it's class name in lowerUpperCase | ||
*/ | ||
public function methodName(): string | ||
{ | ||
$fullClassName = get_called_class(); | ||
$lastBackslashIndex = strrpos( $fullClassName, '\\' ); | ||
if ( -1 === $lastBackslashIndex ) { | ||
throw new UnexpectedValueException( "Cannot resolve method name from it\'s class ($fullClassName)" ); | ||
} | ||
$className = substr( $fullClassName, $lastBackslashIndex + 1 ); | ||
return strtolower( $className[ 0 ] ) . substr( $className, 1 ); | ||
} | ||
|
||
/** | ||
* Request body for API method | ||
*/ | ||
public abstract function body(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Micromagicman\TelegramWebApp\Api; | ||
|
||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Facades\Http; | ||
use Micromagicman\TelegramWebApp\Api\WebApp\AnswerWebAppQuery; | ||
|
||
class TelegramBotApi implements TelegramApi | ||
{ | ||
|
||
/** | ||
* Root endpoint for Telegram Bot API | ||
*/ | ||
private const TELEGRAM_API_ROOT = 'https://api.telegram.org'; | ||
|
||
/** | ||
* Root endpoint with current bot token | ||
*/ | ||
private string $apiBotRoot; | ||
|
||
public function __construct() | ||
{ | ||
$this->apiBotRoot = self::TELEGRAM_API_ROOT . '/bot' . telegramToken(); | ||
} | ||
|
||
/** | ||
* Set the result of an interaction with a Web App and send a corresponding message on behalf of the user to the chat | ||
* from which the query originated. On success, an Illuminate\Http\Client\Response object is returned. | ||
*/ | ||
public function answerWebAppQuery( mixed $result ): Response | ||
{ | ||
return $this->execute( new AnswerWebAppQuery( $result ) ); | ||
} | ||
|
||
/** | ||
* Telegram Bot API method execution | ||
*/ | ||
public function execute( TelegramApiMethod $apiMethod ): Response | ||
{ | ||
return Http::post( $this->prepareUrl( $apiMethod ), $apiMethod->body() ); | ||
} | ||
|
||
/** | ||
* Create endpoint for Telegram Bot API | ||
*/ | ||
private function prepareUrl( TelegramApiMethod $apiMethod ): string | ||
{ | ||
return "$this->apiBotRoot/{$apiMethod->methodName()}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Micromagicman\TelegramWebApp\Api\WebApp; | ||
|
||
use Micromagicman\TelegramWebApp\Api\TelegramApiMethod; | ||
|
||
/** | ||
* Set the result of an interaction with a Web App and send a corresponding message on behalf of the user | ||
* to the chat from which the query originated. | ||
* | ||
* @see {@link https://core.telegram.org/bots/api#answerwebappquery} | ||
*/ | ||
class AnswerWebAppQuery extends TelegramApiMethod | ||
{ | ||
|
||
/** | ||
* A JSON-serialized object describing the message to be sent | ||
*/ | ||
private mixed $result; | ||
|
||
public function __construct( mixed $result ) | ||
{ | ||
$this->result = $result; | ||
} | ||
|
||
public function body(): array | ||
{ | ||
return [ | ||
'web_app_query_id' => webAppQueryId(), | ||
'result' => json_encode( $this->result ) | ||
]; | ||
} | ||
} |
Oops, something went wrong.