From 4ea735cbda5f68f30d822f88b6134036c51454cb Mon Sep 17 00:00:00 2001 From: Irshad Ahmad <94346512+irshadahmad21@users.noreply.github.com> Date: Sat, 3 Feb 2024 22:38:50 -0800 Subject: [PATCH] Update `wptelegram-bot-api` to json encode nested params by default (#93) * Update wptelegram-bot-api to json encode nested params by default * Clean up * Add changeset --- .changeset/warm-tigers-cry.md | 5 +++++ packages/php/wptelegram-bot-api/src/API.php | 9 ++++++--- packages/php/wptelegram-bot-api/src/Client.php | 18 ++++++++++-------- .../php/wptelegram-bot-api/src/Request.php | 8 ++++---- 4 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .changeset/warm-tigers-cry.md diff --git a/.changeset/warm-tigers-cry.md b/.changeset/warm-tigers-cry.md new file mode 100644 index 00000000..3da14837 --- /dev/null +++ b/.changeset/warm-tigers-cry.md @@ -0,0 +1,5 @@ +--- +"@wpsocio/wptelegram-bot-api": patch +--- + +json encode nested params by default diff --git a/packages/php/wptelegram-bot-api/src/API.php b/packages/php/wptelegram-bot-api/src/API.php index 3dcbf37c..19ab084b 100644 --- a/packages/php/wptelegram-bot-api/src/API.php +++ b/packages/php/wptelegram-bot-api/src/API.php @@ -187,10 +187,13 @@ public function sendMessage( array $params ) { if ( empty( $params['parse_mode'] ) && mb_strlen( $params['text'], 'UTF-8' ) > 4096 ) { // break text after every 4096th character and preserve words. preg_match_all( '/.{1,4095}(?:\s|$)/su', $params['text'], $matches ); + foreach ( $matches[0] as $text ) { - $params['text'] = $text; - $res = $this->sendRequest( __FUNCTION__, $params ); - $params['reply_to_message_id'] = null; + $params['text'] = $text; + + $res = $this->sendRequest( __FUNCTION__, $params ); + + unset( $params['reply_to_message_id'], $params['reply_parameters'] ); } return $res; } diff --git a/packages/php/wptelegram-bot-api/src/Client.php b/packages/php/wptelegram-bot-api/src/Client.php index 1df7163d..42963a85 100644 --- a/packages/php/wptelegram-bot-api/src/Client.php +++ b/packages/php/wptelegram-bot-api/src/Client.php @@ -57,14 +57,16 @@ public function get_base_url() { public function prepare_request( Request $request ) { $url = $this->get_base_url() . $request->get_bot_token() . '/' . $request->get_api_method(); - return apply_filters( - 'wptelegram_bot_api_prepare_request', - [ - $url, - $request->get_params(), - ], - $request - ); + $params = $request->get_params(); + + // Ensure that the nested arrays are encoded as JSON. + foreach ( $params as $key => $value ) { + if ( is_array( $value ) ) { + $params[ $key ] = wp_json_encode( $value ); + } + } + + return apply_filters( 'wptelegram_bot_api_prepare_request', [ $url, $params ], $request ); } /** diff --git a/packages/php/wptelegram-bot-api/src/Request.php b/packages/php/wptelegram-bot-api/src/Request.php index af30434f..19b94d2b 100644 --- a/packages/php/wptelegram-bot-api/src/Request.php +++ b/packages/php/wptelegram-bot-api/src/Request.php @@ -54,11 +54,11 @@ class Request { /** * Creates a new Request * - * @param string|null $bot_token The API token. - * @param string|null $api_method The API method name. - * @param array|null $params The method params. + * @param string $bot_token The API token. + * @param string $api_method The API method name. + * @param array $params The method params. */ - public function __construct( $bot_token = null, $api_method = null, array $params = [] ) { + public function __construct( string $bot_token = '', string $api_method = '', array $params = [] ) { $this->set_bot_token( $bot_token ); $this->set_api_method( $api_method ); $this->set_params( $params );