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

Jump to solution
ajmccann
Community Novice

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[]...

Labels (1)
0 Likes
1 Solution
James
Community Champion

 @ajmccann  

I would say there's a typo in the documentation. It should be recipients[]=1&recipients[]=2&recipients[]=course_3

The recipients[] required string means that it is an array whose values are strings.  You're free to send integers, but it needs to be string to handle values like course_3.

To send to the two IDs 249 and 246, it should look like this:

In JSON, it would be { "recipients" : [ 249, 256 ] }

I'm doing this without testing, but you might try 'recipients[]' => array(249, 256)

If that doesn't work, try 'recipients' => array(249, 256).

I wish I could be more definite, but I use PHP to usually convert the POSTS payload to JSON and send it that way. However, some of my code does have the [] as part of the field name, so I would start with recipients[] and not recipients.

View solution in original post