AnsweredAssumed Answered

How to send a message to multiple recipients via Conversations API?

Question asked by Andrew McCann on Dec 25, 2019
Latest reply on Dec 26, 2019 by James Jones

I can send a message to a single user via the Conversations API - using PHP:

 

Snippet of PHP with a user hardcoded for testing:

$post = [
'recipients[]' => '256',
// 'recipients[]' => 'recipients[]=249&recipients=256',
'group_conversation' => true,
'bulk_message' => true,
'subject'=>$msg_subject,
'body'=>$msg_body,
];

var_dump($post);

// $header = array("Authorization: Bearer {$access_token}");

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $token_url,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $post,
CURLOPT_RETURNTRANSFER => true
));
$lti_msgresponse = curl_exec($curl);
curl_close($curl);
echo($lti_msgresponse);

The documentation for the API is:

Conversations - Canvas LMS REST API Documentation 

recipients[] Required string
An array of recipient ids. These may be user ids or course/group ids prefixed with “course_” or “group_” respectively, e.g. recipients[]=1&recipients=2&recipients[]=course_3

This comes across as doublespeak to me: it's a string...but it's an array?

I've tried following the slender documentation - to send two user IDs. But I get "invalid recipient as a response using:

 

'recipients[]' => 'recipients[]=249&recipients=256',

 

 

I've tried passing a nested array

$arrTempRecip = array(
array('recipients[]'=>249),
array('recipients[]'=>256)
);

Then using: 

'recipients[]' => arrTempRecip,

 

But PHP complains about converting an array to a string in the CURLOPT section.

 

The very end of this Google Groups conversation touches on the issue...but the solution isn't spelled out and I'm not putting the pieces together.

 

Since the API requires a string, not an array, I think it's something closer to "recipients[]=249&recipients=256"...very frustrating!

 

Help/guidance appreciated. Lots of Canvas APIs use this [] construction and I'm just not getting it. exclude[], attachments[]...

Outcomes