phpmoodlemoodle-api

Does not allow responding to a message sent with the moodle api


I trying to make a php script to send one message to other person in moodle.

I've seen the message api and i make this

 $message = new \core\message\message();
$message->component = 'moodle';
$message->name = 'instantmessage';
$message->userfrom = 318;
$message->userto = 323;
$message->subject = 'message subject 1';
$message->fullmessage = 'message body';
$message->fullmessageformat = FORMAT_MARKDOWN;
$message->fullmessagehtml = '<p>message body</p>';
$message->smallmessage = 'small message';
$message->notification = '0';
$message->contexturl = 'http://GalaxyFarFarAway.com';
$message->contexturlname = 'Context name';
$message->replyto = "random@example.com";
$content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for specific processor
$message->set_additional_content('email', $content);
$message->courseid = 107; // This is required in recent versions, use it from 3.2 on https://tracker.moodle.org/browse/MDL-47162

$messageid = message_send($message)

The problem is, when the user 323 send a reply message in the chat that is created in the moodle internal messaging, an error occurs (the message is surrounded by red) and never arrives.

And I really want it to be able to respond as if it were a normal conversation.

I don't know if I'm going wrong.

Thank you


Solution

  • I finally found it !!!

    The problem is that first you have to create a conversation between the users and then send the message

    if(!\core_message\api::get_conversation_between_users([$userfrom, $userto ])){
        $conversation = \core_message\api::create_conversation(
                        \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
                        [
                            $userfrom,
                            $userto 
                        ]
                    );
    }
    
    
    $message = new \core\message\message();
    
    
    
    $message->component = 'moodle';
        $message->name = 'instantmessage';
        $message->userfrom = $userfrom ;
        $message->userto = $userto;
        $message->subject = 'Nuevo mensaje';
        $message->fullmessage = $msg;
        $message->fullmessageformat = FORMAT_MARKDOWN;
        $message->fullmessagehtml = $msg;
        $message->smallmessage = $msg;
        $message->notification = '0';
        $message->contexturl = '';
        $message->contexturlname = 'Context name';
        $message->replyto = "###@######.###";
        $content = array('*' => array('header' => '', 'footer' => '')); 
        $message->set_additional_content('email', $content);
        $message->courseid = 107; 
        message_send($message);