J.d. Jackson
J.d. Jackson

Reputation: 33

SoftLayer API - Email Subscriptions For Users

Hello: For some reason all of my users (sales, marketing, finance etc...) are receiving email notices about unplanned events, planned maintenance etc... and are complaining about the number of "spam" messages. Using the API I see class Email_Subscription and can list all object types which correspond to the portal/Account/Users/Email Preferences screen for a user but cannot figure out how to mass disable email notifications user by user. Anyone have an example in Python for this? Also, I think this is different and separate from the User_Notifications class, correct? Thanks!

Upvotes: 1

Views: 160

Answers (1)

F.Ojeda
F.Ojeda

Reputation: 728

You can try using this python example code to disable the email notifications subscriber user by user.

"""
UpdateNotificationSubscriber

Update the active status for a notification that the user is subscribed to. A notification along with an active flag
can be supplied to update the active status for a particular notification subscription.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_User_Customer/
https://softlayer.github.io/reference/services/SoftLayer_User_Customer/updateNotificationSubscriber/

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <[email protected]>
"""

import SoftLayer

# For nice debug output:
from pprint import pprint as pp

# Your SoftLayer API username and key.
API_USERNAME = 'set me'

# Generate one at https://control.softlayer.com/account/users
API_KEY = 'set me'

userId = 11111

notificationKeyName = "PLANNED_MAINTENANCE"
active = 0

client = SoftLayer.create_client_from_env(
    username=API_USERNAME,
    api_key=API_KEY
)

try:

    orderStatus = client['SoftLayer_User_Customer'].updateNotificationSubscriber(notificationKeyName, active, id=userId)
    print(orderStatus)

except SoftLayer.SoftLayerAPIError as e:
    pp('Unable to update the user notification subscriber faultCode=%s, faultString=%s'
       % (e.faultCode, e.faultString))

To disable another type of notification subscriber such as unplanned events, etc. you just have to replace the “notificationKeyName” attribute data for the notification you want.

To enable a notification subscriber, you just have to change the “active” attribute to 1.

To get all active notification subscribers in your account you can use this rest api:

Method: GET

https://[username]:[apiKey]@api.softlayer.com/rest/v3.1/SoftLayer_Account/getActiveNotificationSubscribers?objectMask=mask[notification]

You will get a response like this example:

{
    "active": 1,
    "createDate": "2017-03-09T16:17:09-06:00",
    "id": 22222,
    "modifyDate": "2017-03-09T16:17:09-06:00",
    "notificationId": 68,
    "notificationSubscriberTypeId": 2,
    "notificationSubscriberTypeResourceId": 556633,
    "notification": {
        "id": 68,
        "keyName": "UNPLANNED_INCIDENT",
        "name": "Unplanned Incident"
    }
},

Use the "KeyName" data to replace the "notificationKeyName " attribute in the python code.

Or if you want to know the notification subscribers for all the users you can use this rest api:

Method: GET

https://[username]:[apikey]@api.softlayer.com/rest/v3.1/SoftLayer_Account/getUsers?objectMask=mask[id,username,notificationSubscribers[notification]]

If you want to disable the notification subscriber for all the user you can try using the following python code:

"""
UpdateNotificationSubscriber

Update the active status for a notification that the user is subscribed to. A notification along with an active flag
can be supplied to update the active status for a particular notification subscription.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_User_Customer/
https://softlayer.github.io/reference/services/SoftLayer_User_Customer/updateNotificationSubscriber/

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <[email protected]>
"""

import SoftLayer
import json

USERNAME = 'set me'
API_KEY = 'set me'

notificationKeyName = "PLANNED_MAINTENANCE"
active = 0

client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
accountService = client['SoftLayer_Account']
customerService = client['SoftLayer_User_Customer']

try:
    users = accountService.getUsers()
    for user in users:
        id = user['id']
        result = customerService.updateNotificationSubscriber(notificationKeyName, active, id=id)
        print(json.dumps(result, sort_keys=True, indent=2, separators=(',', ': ')))

except SoftLayer.SoftLayerAPIError as e:
    print("Unable to change the subscription notification. faultCode=%s, faultString=%s" % (e.faultCode, e.faultString))

Another links where can you find more examples:

Removing subscription to "Planned Maintenance" email notifications

Subscribe users to notifications via Soft Layer API

To disable the email preferences the same that the control portal you can use the following python code example:

"""
Disable email subscription.

Important manual pages:
https://softlayer.github.io/reference/services/SoftLayer_Email_Subscription/disable/

"""
import SoftLayer

# For nice debug output:
from pprint import pprint as pp

# Your SoftLayer API username and key.
API_USERNAME = 'set me'

# Generate one at https://control.softlayer.com/account/users
API_KEY = 'set me'

emailSubscriptionId = 1

client = SoftLayer.create_client_from_env(
    username=API_USERNAME,
    api_key=API_KEY
)

try:
    billingObjects = client['SoftLayer_Email_Subscription'].disable(id = emailSubscriptioId)
    print(billingObjects)

except SoftLayer.SoftLayerAPIError as e:
    pp('Unable to disable the email subscription faultCode=%s, faultString=%s'
       % (e.faultCode, e.faultString))

The "emailSubscriptionId" comes in order from the top of the page to the bottom, it means e.g. the first option which is "Ordering" with the type of event "Order Being Reviewed" has the emailSubscriptionId = 1, another option "Planned Maintenance" with the event "High Impact" has the emailSubscriptionId = 8, the last one is emailSubscriptionId = 16.

Reference:

https://softlayer.github.io/reference/services/SoftLayer_Email_Subscription/disable/

Upvotes: 1

Related Questions