Implemented Telegram's currency API \TgBotLib\Classes\Utilities::getCurrency
to get basic information about the currency of the specified country, see [Telegram's currency API](https://core.telegram.org/bots/payments) for more information.
https://git.n64.cc/nosial/libs/tgbot/-/issues/3
This commit is contained in:
parent
e025f34a2e
commit
3c91058235
4 changed files with 60 additions and 0 deletions
|
@ -33,6 +33,7 @@ input objects for methods that require input objects.
|
|||
* Added object `\TgBotLib\Objects\Telegram\InlineQueryResult\InlineQueryResultGame`, see [InlineQueryResultGame](https://core.telegram.org/bots/api#inlinequeryresultgame) for more information.
|
||||
* Added class `\TgBotLib\Objects\Telegram\InlineQueryResult` which is the base class for all inline query results, additionally added `\TgBotLib\Abstracts\InlineQueryResultType` to represent the type of inline query result object.
|
||||
* Added method `\TgBotLib\Bot::answerWebAppQuery` to answer a callback query sent from a web app, which returns the newly added `\TgBotLib\Objects\Telegram\SentWebAppMessage` object on success.
|
||||
* Implemented Telegram's currency API `\TgBotLib\Classes\Utilities::getCurrency` to get basic information about the currency of the specified country, see [Telegram's currency API](https://core.telegram.org/bots/payments) for more information.
|
||||
|
||||
### Changed
|
||||
* Refactored InputMessageContent types to its own namespace so InputMessageContent can always return the correct InputMessageContent object type when calling `fromArray()`
|
||||
|
|
|
@ -1,8 +1,51 @@
|
|||
<?php
|
||||
|
||||
/** @noinspection PhpMissingFieldTypeInspection */
|
||||
|
||||
namespace TgBotLib\Classes;
|
||||
|
||||
use Exception;
|
||||
use LogLib\Log;
|
||||
use TgBotLib\Objects\Currency;
|
||||
|
||||
class Utilities
|
||||
{
|
||||
/**
|
||||
* @var Currency[]|null
|
||||
*/
|
||||
private static $currencies_cache;
|
||||
|
||||
/**
|
||||
* Returns
|
||||
*
|
||||
* @param string $code
|
||||
* @return Currency
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getCurrency(string $code): Currency
|
||||
{
|
||||
if (self::$currencies_cache === null)
|
||||
{
|
||||
$source = "https://core.telegram.org/bots/payments/currencies.json";
|
||||
Log::verbose('net.nosial.tgbotlib', "Fetching currencies from $source");
|
||||
|
||||
$data = json_decode(file_get_contents($source), true);
|
||||
|
||||
if ($data === null)
|
||||
throw new Exception("Failed to fetch currencies");
|
||||
|
||||
self::$currencies_cache = [];
|
||||
foreach ($data as $currency)
|
||||
{
|
||||
self::$currencies_cache[strtolower($currency['code'])] = Currency::fromArray($currency);
|
||||
}
|
||||
|
||||
Log::verbose('net.nosial.tgbotlib', "Fetched " . count(self::$currencies_cache) . " supported currencies");
|
||||
}
|
||||
|
||||
if(!isset(self::$currencies_cache[strtolower($code)]))
|
||||
throw new Exception("Currency $code not found");
|
||||
|
||||
return self::$currencies_cache[strtolower($code)];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace TgBotLib\Exceptions;
|
||||
|
||||
class CurrencyNotSupportedException
|
||||
{
|
||||
|
||||
}
|
8
tests/currency_test.php
Normal file
8
tests/currency_test.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
|
||||
require 'autoload.php';
|
||||
|
||||
var_dump(\TgBotLib\Classes\Utilities::getCurrency('EUR'));
|
||||
var_dump(\TgBotLib\Classes\Utilities::getCurrency('CAD'));
|
||||
var_dump(\TgBotLib\Classes\Utilities::getCurrency('USD'));
|
Loading…
Add table
Reference in a new issue