cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
robotcars
Community Champion

Notification Preferences API

Jump to solution

I'm trying to update 2 notification preferences for users through the API with Ruby. I'm either having a problem understanding the API or maybe something is wrong.

 

1) I have masquerading privileges, Masquerading - Canvas LMS REST API Documentation, but I might be having trouble understanding the format of the query string to be sent. Notification Preferences - Canvas LMS REST API Documentation 

 

Specifically, what notification_preferences[<X>][frequency] is supposed to look like for:

{ "frequency": "immediately", "notification": "conversation_message", "category": "conversation_message" },

{ "frequency": "never", "notification": "added_to_conversation", "category": "added_to_conversation" },

 

I have been attempting to follow this post, and format my URI the same way.

https://community.canvaslms.com/message/57363-setting-default-user-notification-preferences-via-api#... 

 

2) I get 422 UNPROCESSABLE ENTITY, as my response.

The server understands the content type of the request entity (hence a 415 Unsupported Media Type status code is inappropriate), and the syntax of the request entity is correct (thus a 400 Bad Request status code is inappropriate) but was unable to process the contained instructions.

 

Here's my Ruby/HTTParty Class, URI and Query String below each attempt.

class CommChannelUpdater
     include HTTParty
     base_uri 'https://x.test.instructure.com/api/v1'
     headers 'Authorization' => 'Bearer AbraCaDAbRA', 'User-Agent' => 'ccsd-canvas-api'
     debug_output STDOUT

     def channel(user)
          self.class.get("/users/#{user}/communication_channels", :verify => true)
     end

     def update(user, channel)
          # attempt to set multiple preferences
          # self.class.post("/users/self/communication_channels/#{channel}/notification_preferences",
          #      :verify => true,
          #      :query => {
          #           :as_user_id => user,
          #           :notification_preferences => {
          #                :added_to_conversation => {:frequency => 'immediately'},
          #                :conversation_message => {:frequency => 'immediately'}
          #           }
          #      }
          # )
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/321/notification_preferences?as_user_id=123&notification_preferences[added_to_conversation][frequency]=immediately&notification_preferences[conversation_message][frequency]=immediately
           
          # self.class.post("/users/self/communication_channels/#{channel}/notification_preferences",
          #      :verify => true,
          #      :query => {
          #           :as_user_id => user,
          #           :notification_preferences => {
          #                :added_to_conversation => 'immediately',
          #                :conversation_message => 'immediately'
          #           }
          #      }
          # )
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/321/notification_preferences?as_user_id=123&notification_preferences[added_to_conversation]=immediately&notification_preferences[conversation_message]=immediately

          # attempt to set 1 at a time
          self.class.post("/users/self/communication_channels/#{channel}/notification_preferences/added_to_conversation", :verify => true, :query => {:as_user_id => user, :notification_preferences => {:frequency => 'immediately'}})
          # 422:https://x.test.instructure.com/api/v1/users/self/communication_channels/4968576/notification_preferences/added_to_conversation?as_user_id=4244756&notification_preferences[frequency]=immediately

     end
end‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

Any thoughts or assistance would be greatly appreciated!

1 Solution

Accepted Solutions
pklove
Community Champion

I'm not familiar with Ruby, but it looks like you are doing a POST instead of a PUT.

View solution in original post

8 Replies
pklove
Community Champion

I'm not familiar with Ruby, but it looks like you are doing a POST instead of a PUT.

View solution in original post

robotcars
Community Champion

pklove‌,

 

Apparently I couldn't see the forest for the trees.

 

Thank You

 

For anyone who needs it

 

#!/usr/bin/env ruby

require 'net/http'
require 'json'
require 'httparty'

class CommChannelUpdater
     include HTTParty
     base_uri 'https://x.test.instructure.com/api/v1'
     headers 'Authorization' => 'Bearer AbraCaDAbRA', 'User-Agent' => 'ccsd-canvas-api'
     #debug_output STDOUT

     def channel(user)
          self.class.get("/users/#{user}/communication_channels", :verify => true)
     end

     def update(user, channel)
          # attempt to set multiple preferences
          self.class.put("/users/self/communication_channels/#{channel}/notification_preferences",
               :verify => true,
               :query => {
                    :as_user_id => user,
                    :notification_preferences => {
                         :added_to_conversation => {:frequency => 'immediately'},
                         :conversation_message => {:frequency => 'immediately'}
                    }
               }
          )
     end
end

comm = CommChannelUpdater.new()
update = comm.update(user_id, comm_ch)
puts update.code‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
pklove
Community Champion

Also, I don't know what querystring Ruby will create with your syntax, but you might need to break out the stuff in the curly braces.

The following curl example works:

curl -k --globoff $server/api/v1/users/self/communication_channels/11900/notification_preferences?as_user_id=$user\
"&notification_preferences[added_to_conversation][frequency]=$frequency&notification_preferences[conversation_message][frequency]=$frequency" \
-X PUT \
-H "Authorization: Bearer $token"
robotcars
Community Champion

The corrected snippet above will produce the following URI and Query String.

status code: 200
https://x.test.instructure.com/api/v1/users/self/communication_channels/456/notification_preferences...

kimhuang
Community Participant

Hi Folks,
I have used  @robotcars and @pklove  's solution to change the notification settings of "announcement_created_by_you" and "conversation_created"
Here is my curl command:
curl -k --globoff https://x.instructure.com/api/v1/users/self/communication_channels/248/notification_preferences\?as_... -X PUT -H "Authorization: Bearer <token>"
The result shows the curl call succeed:
{"notification_preferences":[{"frequency":"immediately","notification":"conversation_created","category":"conversation_created"},{"frequency":"immediately","notification":"announcement_created_by_you","category":"announcement_created_by_you"}]}
However, the display value of the "announcement_created_by_you" setting doesn't change accordingly. (account->notifications) It remains the value before the curl command is executed. in contrary, the display value of the "conversation_created" change as expected.
Could anyone please shed a light on?
Thanks so much!
-Kim

robotcars
Community Champion

@kimhuang Were you able to solve this yet?

kimhuang
Community Participant

@robotcars Thanks for getting back to me. I have created a case, and the case number is 07451668.  Will follow up with the CSM for my school and let you know if there is any update.

 

 

 

kimhuang
Community Participant

@robotcars Hi, Here is the answer from the Canvas support for the problem that I experienced and I also posted the solution down below.

-------------
Thank you for contacting Canvas Support. My name is Ryan, and I am with the L2 Canvas Support team. I understand that when using the API call to update the "announcement_created_by_you" notification preference frequency the preference updates in the API, but the change is not reflected in the UI. This is a result of multiple notifications within the same category. In the "announcement_created_by_you category" there are both the "announcement_created_by_you" and "announcement_reply" notification. In order for the UI to update, the "announcement_reply" notification would need to be updated as well. Or you could update the entire category at one time using this API call.

 

----------------------

Here is a working version:

 

curl -k --globoff https://xx.beta.instructure.com/api/v1/users/self/communication_channels/248/notification_preference... -X PUT -H "Authorization: Bearer xxxx "

 

----------------------------------

Happy Easter!

 

Best,

-Kim