Skip to content

Commit

Permalink
add Subscribe for New SMS tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Feb 6, 2016
1 parent 9d0114e commit bf66612
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
35 changes: 35 additions & 0 deletions docs/usage/notifications/Subscribe_for_New_SMS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Subscribe for New SMS Messages

A common use case is to subscribe to all incoming SMS messages. This can be used to either sync messages or to take action in incoming messages. RingCentral has a couple of ways to retrieve incoming messages. This tutorial describes retrieving messages one at a time where there is no replicated dataset. If you wish to replicate to your own datastore, please contact RingCentral devsupport.

To retrieve incoming SMS messages, there are two steps

1. Subscribe for events on the message store to receive information on new SMS messages
2. Retrieve the messages from the message store by querying the store for the time range

### Step 1: Subscription for new SMS events:

For this step, create a subscription to the `/restapi/v1.0/account/~/extension/~/message-store` event filter with the account id and extension id of interest where `~` represents your currently authorized values. When receiving an event, you will receive an array of `changes` of which, some can have the `type` attribute set to `SMS` along with a `newCount` attribute. When `newCount` is > 0, there is a new SMS. Information on subscription is here:

* [Dev Guide: Notifications](https://developer.ringcentral.com/api-docs/latest/index.html#!#Notifications.html)
* [API Reference: Notifications](https://developer.ringcentral.com/api-docs/latest/index.html#!#RefNotifications.html)

### Step 2: SMS Retrieval

To retrieve the new SMS message given an subscription, use the event's `body.lastUpdated` time property to retrieve inbound SMS messages matching that time. You can do this with the following steps:

1. Retrieve the event's `body.lastUpdated` property
2. Create a message store API call setting the `dateFrom` and `dateTo` parameters around the event's `body.lastUpdated` property. You can set the range to be 1 second on either side.
3. Upon receiving an array of messages in the response, filter the messages on the message `lastModifiedTime` which will be the same as the event's `body.lastUpdated` time.

For additional reading, see:

* [Dev Guide: Messaging](https://developer.ringcentral.com/api-docs/latest/index.html#!#MessagingGuide.html)
* [API Reference: Message List](https://developer.ringcentral.com/api-docs/latest/index.html#!#MessageList.html)

### Example Implementation

The above is implemented in an example script using the Ruby SDK. The script retrieves each message and sends it to a Glip chat team using the `Glip::Poster` module. Other chat modules with the same interfae can also be used.

* Script: [sms_to_chat.rb](https://github.com/grokify/ringcentral-sdk-ruby/blob/master/scripts/sms_to_chat.rb)
* SDK : [messages_retriever.rb](https://github.com/grokify/ringcentral-sdk-ruby/blob/master/lib/ringcentral_sdk/rest/messages_retriever.rb)
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Notifications: Subscription API

## Subscribing to All Extensions
# Subscribe to All Extensions

A common use case is to subscribe to the presence events on multiple or all extensions of a RingCentral account. This can be done with 2 API calls for accounts with 1000 or fewer extensions, one API call to get extension info for up to 1000 extensions and a second API call to subscribe to those extensions as event filters. Add 1 API call per each additional 1000 extensions.

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/notifications/Subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ sub.subscribe(['/restapi/v1.0/account/~/extension/~/presence'])
sub.destroy()
```

You can subscribe to multiple extensions by adding multiple event filters to a single subscription API call.
You can subscribe to multiple extensions by adding multiple event filters to a single subscription API call.
3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ pages:

## Notifications:
- ['usage/notifications/Subscriptions.md', 'Notifications', 'Subscriptions']
- ['usage/notifications/Subscriptions-All-Extensions.md', 'Notifications', 'Subscribe to All Extensions']
- ['usage/notifications/Subscribe_to_All_Extensions.md', 'Notifications', 'Subscribe to All Extensions']
- ['usage/notifications/Subscribe_for_New_SMS.md', 'Notifications', 'Subscribe for New SMS']
1 change: 1 addition & 0 deletions scripts/sms_to_chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initialize(client, posters=[], event_data={})
@posters = posters
@event = RingCentralSdk::REST::Event.new event_data
@retriever = RingCentralSdk::REST::MessagesRetriever.new @client
@retriever.range = 0.5 # minutes
end

def post_message()
Expand Down

0 comments on commit bf66612

Please sign in to comment.