telegram bot api pass empty inline_keyboard -> delete keyboard - telegram

I'd like to use the methode editMessageReplyMarkup to get rid of my inline_keyboard buttons. I can get rid of them by passing the 'reply_markup' a json encoded keyboard like that:
$k = ['inline_keyboard' =>
[
[ ]
]
];
but as a result I get that error
Request has failed with error 400: Bad Request: object expected as reply markup
I tried a couple variants include not sending a 'reply_markup' at all as attribute to the methode, but I get the error "Bad Request: object expected as reply markup" or "Bad Request: can't parse inline keyboard button: InlineKeyboardButton must be an Object".
That is how I call the methode:
$bot->apiRequestJson("editMessageReplyMarkup", array(
'chat_id'=>$cb_chat_id,
'message_id'=>$cb_msg_id,
'reply_markup' => json_encode($k)
));
The function is taken from here: function taken from hellobot example https://core.telegram.org/bots/samples/hellobot
And to prove my function is working, that results in a new inline keyboard without any error
$k = ['inline_keyboard' => [
[
['text' => 'caption', 'callback_data' => 'test']
]
]];
Thanks for your help!
Markus

Related

lumen - Why does PHPUnit register test returns 405?

I want to make phpunit tests for lumen app, like :
public function testRegisterUser()
{
$newUserData = [
'name' => 'test_user',
'email' => 'test_user#mail.com',
'password' => Hash::make('111111'),
'status' => 'A',
'has_debts' => false
];
$response = $this->get('/api/v1/register', $newUserData); // http://localhost:8000/api/v1/register
$this->assertResponseOk();
}
But running tests I got 405 error :
1) PagesTest::testRegisterUser
Expected response status code [200] but received 405.
Failed asserting that 200 is identical to 405.
/ProjectPath/vendor/illuminate/testing/TestResponse.php:177
/ProjectPath/vendor/illuminate/testing/TestResponse.php:99
/ProjectPath/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:415
/ProjectPath/tests/PagesTest.php:27
Why I got 405 Method Not Allowed ? I postman I check my method : https://prnt.sc/207bu03
Method /api/v1/register in postman has no any token protection.
How to make my test working ?
UPDATED BLOCK :
I got error :
Error: Call to undefined method PagesTest::getJson()
If I modify :
$response = $this->getJson('/api/v1/register', $newUserData);
method getJson is mentioned here : https://laravel.com/docs/8.x/http-tests
But on this page I see in test file header:
namespace Tests\Feature;
But not in my generated lumen test file.
In my routes/web.php I have :
$router->group(['prefix'=>'api/v1'], function() use($router){
$router->post('/register','AuthController#register');
$router->post('/login', 'AuthController#login');
$router->group(['middleware' => 'auth'], function () use ($router) {
$router->get('/profile', 'UserProfileController#index');
What is wrong ?
Thanks!
Because you maybe have to use $this->getJson instead of $this->get.
It is not http://localhost:8000 as you are testing, you are not literally accessing the URL. It is simulating that.
Also share your api.php or routes file and the controller please (also the middlewares working on that URL).
Looking at the Lumen's documentation I can see that there is no getJson, my bad. You have to use $this->json('GET' instead.

WordPress endpoint plugin shows SyntaxError: JSON.parse

i have read many pages to find out howto create a simple endpoint into my simple WP-Plugin.
links of good articles i have read for that:
https://developers.shopware.com/developers-guide/rest-api/plugin-api-extension/ , https://wptips.dev/custom-rest-api/ , https://torquemag.io/2016/07/adding-custom-endpoints-extra-touches/ , https://www.cloudways.com/blog/wordpress-rest-api-to-fetch-posts/#get-wp-v2-posts , https://www.cloudways.com/blog/wordpress-rest-api-to-fetch-posts/#wordpress-rest-api-using-json , https://developer.wordpress.org/rest-api/
this gives me most hope to get success with it and i used the source from here:
https://stackoverflow.com/a/64331655/2891692
My URL i using in Web-Browser:
http://localhost/wordpress/wp-json/a0plugin/v1/testing
excerpt of my complete source from gist
htdocs/wp-content/plugins/a0plugin/a0plugin.php
<?php
/**
* Plugin Name: a0plugin
*/
function at_rest_testing_endpoint(){
return new WP_REST_Response('Howdy!!');
}
function at_rest_init(){
$namespace = 'a0plugin/v1';
$route = 'testing';
register_rest_route($namespace, $route, array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'at_rest_testing_endpoint'
));
}
add_action('rest_api_init', 'at_rest_init');
?>
complete source:
https://gist.github.com/sl5net/10d21e8bd358b9149968885a93862424
SyntaxError: JSON.parse
Error: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
if the comments are left out, the error disappears. these are not listed in the so-called relevant source text excerpt. the cause is unknown to me.

woocommerce subscription API

I am using Woocomemrce REST API to connect with my site. Everything works fine when it comes to orders but it is not working with subscriptions. I have tried following code to get subscriptions but it is giving "Error: No route was found matching the URL and request method [rest_no_route]"
$woocommerce = new Client(
'https://www.example.com',
'ck_OUR_CONSUMER_KEY',
'cs_OUR_CONSUMER_SECRET',
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
try {
print_r($woocommerce->get('orders')); //this works and fetch orders
print_r($woocommerce->get('subscriptions')); //but this does not work
} catch (HttpClientException $e) {
echo $e->getMessage(); // Error message.
echo $e->getRequest(); // Last request data.
echo $e->getResponse(); // Last response data.
}
Can anyone help me sort out this issue. Thank You.
I changed it to the following it worked for me.
$woocommerce = new Client(
'https://www.example.com',
'ck_OUR_CONSUMER_KEY',
'cs_OUR_CONSUMER_SECRET',
[
'wp_api' => true,
'version' => 'wc/v1',
]
);

HTTP client Cakephp 3 ignores json body

I'm currently writing a RESTful API in Cakephp 3 whereby I need to test a POST operation through http://host.com/api/pictures. The code for the test:
<?php
namespace App\Test\TestCase\Controller;
use App\Controller\Api\UsersController;
use Cake\TestSuite\IntegrationTestCase;
use Cake\Network\Http\Client;
use Cake\Network\Http\FormData;
class ApiPicturesControllerTest extends IntegrationTestCase{
public $fixtures = [
'app.users',
'app.comments',
'app.albums',
'app.users_albums'
];
public function testAdd(){
// $data = new FormData();
$accessToken ='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo';
$http = new Client([
'headers' => ['Authorization' => 'Bearer ' . $accessToken, 'Content-Type' => 'application/json']
]);
$data = [
"album_id" => 1,
"link" => "http://www.google.com",
"description" => "testtesttest",
"favorite" => true
];
$result = $http->post('http://vecto.app/api/pictures/add.json', $data, ['type'=>'json']);
// $this->assertResponseOk();
// debug($result);
}
}
When I try to debug the result I get a 'cannot add or update child row' while I'm sure the responding id does exists
(the fixtures does have the id's too). Additionally, the log indicates that it only tries to insert the create/update rows. Therefore, I'm pretty sure the data is ignored but however I can't find a solution. I already tried different combination of headers like only application/json for Accept, application/json for Content-Type etc. I'm using the CRUD plugin for Cakephp to pass the data to an add function.
Postman output
Furthermore, I tried the Postman Chrome plugin to save the data and that actually does work. Does anyone know what I'm doing wrong in the test?
That's not how the integration test case is ment to be used. You are dispatching an external, real request, which will leave the test environment, while you should use the request dispatching tools that the integration test case supplies, that is
IntegrationTestCase::get()
IntegrationTestCase::post()
IntegrationTestCase::put()
etc...
These methods will dispatch simulated requests that do not leave the test environment, which is crucial for things to work properly, as you want to use test connections, inspect possible exceptions, have access to the used session, etc...
ie, you should do something along the lines of
$accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjksImV4cCI6MTQ1NzYyOTU3NH0.NnjXWEQCno3PUiwHhnUCBjiknR-NlmT42oPLA5KhuYo';
$this->configRequest([
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Content-Type' => 'application/json'
]
]);
$data = [
"album_id" => 1,
"link" => "http://www.google.com",
"description" => "testtesttest",
"favorite" => true
];
$this->post('/api/pictures/add.json', json_encode($data));
Note that a content type of application/json will require you to send raw JSON data! If you don't actually need/want to test parsing of raw input, then you could skip that header, and pass the array as data instead.
See also
Cookbook > Testing > Controller Integration Testing
API > \Cake\TestSuite\IntegrationTestCase

How should HTTP Cookie be decoded?

If I send this header to the server:
Cookie: spaces=foo+bar%20baz; repeat=foo; repeat=bar
How should it be decoded?
PHP decodes it as
array (
'spaces' => 'foo bar baz',
'repeat' => 'foo',
)
But I think it should be
array(
'spaces' => 'foo bar baz',
'repeat' => array('foo','bar'),
)
Which is correct?
PHP has the known habit/peculiarity of overwriting any external parameters (GET/POST/COOKIE), when a parameter name occurs more than once.
For example with a query string such as ?spaces=foo+bar%20baz&repeat=foo&repeat=bar, you would have the same problem – in $_GET['repeat'], only the value bar would “survive”. (That should be the same in your cookie example btw., it should be 'repeat' => 'bar', not 'repeat' => 'foo'.)
To avoid this, square brackets at the end of the parameter name have to be used. Try
Cookie: spaces=foo+bar%20baz; repeat[]=foo; repeat[]=bar
– that should get you the array structure that you want in $_COOKIE.

Resources