ASIKOO Posted April 1, 2023 Posted April 1, 2023 Hi, I wanted to test the API to send a private message, I think it doesn't work? I don't understand the error, it seems correct according to the documentation, is it a software problem? No error message in IPS logs. Parameters $post = [ 'from' => 26583, // Bot Account 'to' => [17997,26105,22505], // Staff //'to' => [22505], 'title' => 'title of private message', 'body' => 'send by api', ]; Dump test.php:52:string '{ "errorCode": "1C374\/3", "errorMessage": "INVALID_RECIPIENT" }' (length=72) Sincerly, ASIKOO
DawPi Posted April 1, 2023 Posted April 1, 2023 //'to' => [22505], Why you commented it? According to code: /* Verify there are recipients and all the recipients are valid */ if( !isset( \IPS\Request::i()->to ) OR !\is_array( \IPS\Request::i()->to ) OR !\count( \IPS\Request::i()->to ) ) { throw new \IPS\Api\Exception( 'INVALID_RECIPIENT', '1C374/3', 404 ); } else { foreach( \IPS\Request::i()->to as $to ) { if( !\IPS\Member::load( (int) $to )->member_id ) { throw new \IPS\Api\Exception( 'INVALID_RECIPIENT', '1C374/4', 404 ); } } } INVALID_RECIPIENT - * @throws 1C374/3 INVALID_RECIPIENT No recipients were supplied You should really look into code more often. 😉
ASIKOO Posted April 1, 2023 Author Posted April 1, 2023 3 minutes ago, DawPi said: //'to' => [22505], Why you commented it? There are 2 lines for the "to" parameter. I tested with 1 user id then I commented it in the code, I wanted to send a private message to several members during my test. 'to' => [17997,26105,22505], // Staff
DawPi Posted April 1, 2023 Posted April 1, 2023 Gosh, sorry, didn't saw it. 😮 Well, it should work fine I think. You have them saved as an array as doc saying: https://invisioncommunity.com/developers/rest-api?endpoint=core/messages/POSTindex ASIKOO 1
ASIKOO Posted April 1, 2023 Author Posted April 1, 2023 (edited) Ahah, no problem 🙂 if it helps, to try to understand... \IPS\Request::i()->to is considered as string according to gettype();, so the script fails is_array() check. I forced hard in the code, to test and it works, PM is send (of course I restored the original file) 😄 Edited April 1, 2023 by ASIKOO
Solution Nathan Explosion Posted April 1, 2023 Solution Posted April 1, 2023 (edited) This post was recognized by Marc! Nathan Explosion was awarded the badge 'Helpful' and 5 points. Show more code 😉 Here's a working example: <?php $path = 'http://localhost/test/ips_47_test/'; $key = 'd5921836b2c8343d890a5faccb9e73af'; //Private message test $endpoint = '/core/messages'; $post = [ 'from' => 1, 'to' => [2], 'title' => 'title of private message', 'body' => 'send by api', ]; $params = http_build_query($post); $curl = curl_init($path . 'api' . $endpoint . '?' . $params); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_USERAGENT => "MyUserAgent/1.0", CURLOPT_POST => 1, CURLOPT_USERPWD => $key )); curl_exec($curl); Edited April 1, 2023 by Nathan Explosion
ASIKOO Posted April 1, 2023 Author Posted April 1, 2023 7 minutes ago, Nathan Explosion said: Show more code... Here's a working example: <?php $path = 'http://localhost/test/ips_47_test/'; $key = 'd5921836b2c8343d890a5faccb9e73af'; //Private message test $endpoint = '/core/messages'; $post = [ 'from' => 1, 'to' => [2], 'title' => 'title of private message', 'body' => 'send by api', ]; $params = http_build_query($post); $curl = curl_init($path . 'api' . $endpoint . '?' . $params); curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_USERAGENT => "MyUserAgent/1.0", CURLOPT_POST => 1, CURLOPT_USERPWD => $key )); curl_exec($curl); I didn't think to use http_build_query() for this, indeed it works with. Unlike you, I used (as it worked with the other APIs by IPS, I didn't ask myself any questions...) curl_setopt($ch, CURLOPT_POSTFIELDS, $post); Thank you @Nathan Explosion Orignal code $api = "https://domain.example/api/core/messages"; $key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $post = [ 'from' => 26583, // Bot Account 'to' => [ 17997, 26105, 22505 ], // Staff 'title' => 'title of private message', 'body' => 'send by api', ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $api); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $key); curl_setopt($ch, CURLOPT_USERAGENT, 'Core X'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $result = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl = json_decode($result); curl_close($ch); var_dump($result); Nathan Explosion and DawPi 2
Recommended Posts