Community

Connecting WordPress People

buddypress/v1/messages REST API

Ddennis 7 replies 1 view 2 readers

  1. New
    #1

    Hi,

    Is there somebody that uses the buddypress messages API in combination with SimpleJWT plugin? I am having serious doubts that this API can be used in combination with Better Messages. Although it sometimes works (miracle?) it most of the times reports "There was an error trying to create the message.". I am a 100% sure I am authenticated and logged in.

    Message, POST: buddypress/v1/messages
    id = 1, // ThreadId
    context = "edit",
    message = "Hello",

    developer.buddypress.org/bp-rest-api/r…
    Really like Better Messages (actually the best out there) but if I can't insert ads and have a robot in the chat, I can not use it (monetization model down the drain). Somebody has experience with buddypress REST API / BM?

    More reactions: press the Down arrow
    1 view
  2. New
    Admin
    #2

    Hi there!

    Better Messages 2.0+ does not use BuddyPress Rest API.

    You can send messages using this way:
    better-messages.com/docs/development/p…

    Thanks!

    More reactions: press the Down arrow
  3. New
    #3

    thanks for pointing it out, missed that disconnect from buddypress, like. However my bot is external and not within wordpress itself. Is there another an endpoint? url I can abuse?

    More reactions: press the Down arrow
  4. New
    #4

    PS: like you lost buddypress, we dont need, was just there for better messages. I am c# engineer, not php, will try to hack a simple wordpress endpoint

    More reactions: press the Down arrow
  5. New
    #5

    ok, hacked-up some wordpress rest endpoints (my first php yeah!)

    1. you need simpleJWT plugin to take care of auth.
    2. make sure you enable jwt on all endpoints and pass &jwt=
    3. this is not production software! as said i am not a PHP engie (assembler, c, c++, c#, pascal but not php 🙂
    4. drop the code in functions.php in your themes folder, again not correct, this is not production software, hope bm comes with official api; below is tested and works

    More reactions: press the Down arrow
  6. New
    #6

    5. to add messages [POST] /?rest_route=/better-messages/v1/addme…

    // register add message endpoint
    add_action('rest_api_init', function () {
    register_rest_route( 'better-messages/v1', '/addmessagetothread',array(
    'methods' => 'POST',
    'callback' => 'AddMessageToThread',
    'permission_callback' => '__return_true'
    ));
    });

    // implementation
    function AddMessageToThread($request) {

    $user = wp_get_current_user();

    // check if user is logged in >> return error
    if ( $user->exists() == false) {
    return new WP_Error( 'User is not logged in.', 'User is not logged in.', array('status' => 404) );
    }

    $sender_id = $user->ID;

    $message_id = Better_Messages()->functions->new_message([
    'sender_id' => $sender_id,
    'thread_id' => $request['thread_id'],
    'content' => $request['content'],
    'return' => 'message_id',
    ]);

    if ( is_wp_error( $message_id ) ) {
    $error = $message_id->get_error_message();
    return new WP_Error( 'Unable to post message to thread.' . $error, 'Unable to post message to thread.', array('status' => 404) );

    } else {
    $response = new WP_REST_Response($message_id);
    $response->set_status(200);
    return $response;
    }
    }

    More reactions: press the Down arrow
  7. New
    #7

    6.to create new threads and post messages [POST] /?rest_route=/better-messages/v1/creat…

    // register create thread endpoint
    add_action('rest_api_init', function () {
    register_rest_route( 'better-messages/v1', '/createnewthread',array(
    'methods' => 'POST',
    'callback' => 'CreateNewThread',
    'permission_callback' => '__return_true'
    ));
    });

    function CreateNewThread($request) {

    $user = wp_get_current_user();

    // check if user is logged in >> return error
    if ( $user->exists() == false) {
    return new WP_Error( 'User is not logged in.', 'User is not logged in.', array('status' => 404) );
    }

    $sender_id = $user->ID;

    $thread_id = Better_Messages()->functions->new_message([
    'sender_id' => $sender_id,
    'content' => $request['content'],
    'subject' => $request['subject'],
    'recipients' => $request['recipients'],
    'return' => 'thread_id',
    'error_type' => 'wp_error'
    ]);

    $response = new WP_REST_Response($thread_id);
    $response->set_status(200);
    return $response;
    }

    hope to save somebody else some time. don't complain when i killed you site.

    More reactions: press the Down arrow
  8. New
    #8

    Hi,

    See that there is already API provisioning in the source, why isn't there documentation? Is it for internal use?

    More reactions: press the Down arrow
0