Skip to content

Commit

Permalink
Update wptelegram-bot-api to json encode nested params by default (#93
Browse files Browse the repository at this point in the history
)

* Update wptelegram-bot-api to json encode nested params by default

* Clean up

* Add changeset
  • Loading branch information
irshadahmad21 authored Feb 4, 2024
1 parent e53be4e commit 4ea735c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-tigers-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpsocio/wptelegram-bot-api": patch
---

json encode nested params by default
9 changes: 6 additions & 3 deletions packages/php/wptelegram-bot-api/src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 10 additions & 8 deletions packages/php/wptelegram-bot-api/src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/php/wptelegram-bot-api/src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 4ea735c

Please sign in to comment.