不允许回复通过穆德尔API发送的消息

时间:2020-06-23 10:43:12

标签: php moodle moodle-api

我试图制作一个php脚本,以将一条消息发送给其他人。

我看过消息api,我做了这个

 $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)

问题是,当用户323在通过内部消息创建的聊天中发送回复消息时,会发生错误(消息被红色包围)并且永远不会到达。

我真的希望它能够像普通对话一样做出回应。

我不知道我要怎么做。

谢谢

2 个答案:

答案 0 :(得分:1)

我终于找到它了!

问题在于,首先您必须在用户之间建立对话,然后发送消息

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);

答案 1 :(得分:0)

可能是因为userto和userfrom必须是对象,例如:

$userto = $DB->get_record('user', array('id' => 323));

$message->userfrom = $USER; // Current user.
$message->userto = $userto;

请参阅https://docs.moodle.org/dev/Message_API#How_to_send_a_message