Skip to content

Commit

Permalink
Resolving merge conflict with python code sample
Browse files Browse the repository at this point in the history
  • Loading branch information
byrnereese committed Jun 5, 2023
2 parents 03bdd2c + 666fa43 commit a936ff7
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 58 deletions.
36 changes: 36 additions & 0 deletions code-samples/websockets/migration-after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from ringcentral import SDK
from dotenv import load_dotenv
import asyncio
import os
from ringcentral.websocket.events import WebSocketEvents

def on_notification(message):
print(message)

def on_sub_created(sub):
print(sub.get_subscription_info())

def on_ws_created(web_socket_client):
print(web_socket_client.get_connection_info())

async def subscribe():
load_dotenv(override=True)
sdk = SDK(
os.environ['RINGCENTRAL_CLIENT_ID'],
os.environ["RINGCENTRAL_CLIENT_SECRET"],
os.environ["RINGCENTRAL_SERVER_URL"],
)
platform = sdk.platform()
platform.login(jwt=os.environ["RINGCENTRAL_JWT_TOKEN"])

try:
web_socket_client = sdk.create_web_socket_client()
web_socket_client.on(WebSocketEvents.connectionCreated, on_ws_created)
web_socket_client.on(WebSocketEvents.subscriptionCreated, on_sub_created)
web_socket_client.on(WebSocketEvents.receiveSubscriptionNotification, on_notification)
await asyncio.gather(
web_socket_client.create_new_connection(),
web_socket_client.create_subscription([{FILTERS}]) # replace {FILTERS} with filter urls
)
except KeyboardInterrupt:
print("Stopped by User")
46 changes: 46 additions & 0 deletions code-samples/websockets/migration-before.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import sys

from dotenv import load_dotenv
from ringcentral import SDK
from multiprocessing import Process
from time import sleep
from ringcentral.subscription import Events
load_dotenv()

rcsdk = SDK( os.environ.get('RC_CLIENT_ID'),
os.environ.get('RC_CLIENT_SECRET'),
os.environ.get('RC_SERVER_URL') )
platform = rcsdk.platform()
try:
platform.login( jwt=os.environ.get('RC_JWT') )
except Exception as e:
sys.exit("Unable to authenticate to platform: " + str(e))

def on_message(msg):
print (msg)

def pubnub():
try:
s = rcsdk.create_subscription()
s.add_events([{FILTERS}]) # replace {FILTERS} with filter urls
s.on(Events.notification, on_message)
res = s.register()
try:
print("Wait for notification...")
except Exception as e:
print (e)
sys.exit(1)
while True:
sleep(0.1)

except KeyboardInterrupt:
print("Pubnub listener stopped...")

p = Process(target=pubnub)
try:
p.start()
except KeyboardInterrupt:
p.terminate()
print("Stopped by User")
sys.exit(1)
2 changes: 1 addition & 1 deletion code-samples/websockets/quick-start.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
https://developers.ringcentral.com */

require('vendor/autoload.php');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

use RingCentral\SDK\WebSocket\WebSocket;
Expand Down
94 changes: 46 additions & 48 deletions code-samples/websockets/quick-start.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
#!/usr/bin/python
# Need a .env file with following fields
# RINGCENTRAL_SERVER_URL=
# RINGCENTRAL_CLIENT_ID=
# RINGCENTRAL_CLIENT_SECRET=
# RINGCENTRAL_JWT_TOKEN=

# You get the environment parameters from your
# application dashbord in your developer account
# https://developers.ringcentral.com

import os
import sys

from dotenv import load_dotenv
from ringcentral import SDK
from multiprocessing import Process
from time import sleep
from ringcentral.subscription import Events
load_dotenv()

rcsdk = SDK( os.environ.get('RC_CLIENT_ID'),
os.environ.get('RC_CLIENT_SECRET'),
os.environ.get('RC_SERVER_URL') )
platform = rcsdk.platform()
try:
platform.login( jwt=os.environ.get('RC_JWT') )
except Exception as e:
sys.exit("Unable to authenticate to platform: " + str(e))

def on_message(msg):
print (msg)

def pubnub():
try:
s = rcsdk.create_subscription()
s.add_events(['/account/~/extension/~/message-store/instant?type=SMS'])
s.on(Events.notification, on_message)
res = s.register()
try:
print("Wait for notification...")
except Exception as e:
print (e)
sys.exit(1)
while True:
sleep(0.1)
from dotenv import load_dotenv
import asyncio
import os
from ringcentral.websocket.events import WebSocketEvents

def on_notification(message):
print("\n Subscription notification:\n")
print(message)

def on_sub_created(sub):
print("\n Subscription created:\n")
print(sub.get_subscription_info())
print("\n Please go and change your user status \n")

def on_ws_created(web_socket_client):
print("\n New WebSocket connection created:")
print(web_socket_client.get_connection_info())

async def main():
load_dotenv(override=True)
sdk = SDK(
os.environ['RINGCENTRAL_CLIENT_ID'],
os.environ["RINGCENTRAL_CLIENT_SECRET"],
os.environ["RINGCENTRAL_SERVER_URL"],
)
platform = sdk.platform()
platform.login(jwt=os.environ["RINGCENTRAL_JWT_TOKEN"])

try:
web_socket_client = sdk.create_web_socket_client()
web_socket_client.on(WebSocketEvents.connectionCreated, on_ws_created)
web_socket_client.on(WebSocketEvents.subscriptionCreated, on_sub_created)
web_socket_client.on(WebSocketEvents.receiveSubscriptionNotification, on_notification)
await asyncio.gather(
web_socket_client.create_new_connection(),
web_socket_client.create_subscription(["/restapi/v1.0/account/~/extension/~/presence"])
)
except KeyboardInterrupt:
print("Pubnub listener stopped...")

p = Process(target=pubnub)
try:
p.start()
except KeyboardInterrupt:
p.terminate()
print("Stopped by User")
sys.exit(1)
print("Stopped by User")


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
10 changes: 6 additions & 4 deletions docs/notifications/websockets/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ If you use a [RingCentral SDK](../../sdks/), then you will need to update the mo

=== "Python"

Upgrade [ringcentral-python](https://pypi.org/project/ringcentral/) to version TODO or later.
**Before**
Upgrade [ringcentral-python](https://pypi.org/project/ringcentral/) to version 0.8.0 or later.
**Before**
```python
{!> code-samples/websockets/migration-before.py !}
```

**After**
```python
{!> code-samples/websockets/migration-after.py !}
```

=== "PHP"
Expand Down
12 changes: 7 additions & 5 deletions docs/notifications/websockets/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ Follow the instructions found in our guide to [running Developer Guide code samp
$ ruby websockets.rb
```

<!--
=== "Python"

### Install RingCentral Python SDK
Expand All @@ -170,20 +169,24 @@ Follow the instructions found in our guide to [running Developer Guide code samp
$ pip install ringcentral python-dotenv
```

### Create and edit websockets.py
### Create and edit websocket_quick_start.py

Create a file called <tt>websockets.py</tt> using the contents below.
Create a file called <tt>websocket_quick_start.py</tt> using the contents below.

```python
{!> code-samples/websockets/quick-start.py !}
```

### fill in .env file

You'll need RINGCENTRAL_SERVER_URL, RINGCENTRAL_CLIENT_ID, RINGCENTRAL_CLIENT_SECRET and RINGCENTRAL_JWT_TOKEN. All of them can be generated from developer portal.

### Run your code

You are almost done. Now run your script.

```bash
$ python websockets.py
$ python websocket_quick_start.py
```

=== "PHP"
Expand All @@ -210,7 +213,6 @@ Follow the instructions found in our guide to [running Developer Guide code samp
```bask
$ php websockets.php
```
-->

## Test your WebSocket subscription

Expand Down
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ plugins:
repository: ringcentral/ringcentral-api-docs
branch: main
- bootstrap-tables
- exclude:
regex:
- '^#.*'
- ringcentral-api-index:
outfile: basics/api-index.md

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ markdown_fenced_code_tabs>=1.0.3
mkdocs-markdown-filter>=0.1.1
mkdocs-pymdownx-material-extras==1.0b11
mdx_include>=0.4.1
mkdocs-exclude>=1.0.2

0 comments on commit a936ff7

Please sign in to comment.