-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreceive-reply.py
More file actions
64 lines (57 loc) · 2.44 KB
/
receive-reply.py
File metadata and controls
64 lines (57 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from multiprocessing import Process
from time import sleep
from ringcentral.subscription import Events
from ringcentral import SDK
from dotenv import Dotenv
import os
dotenv = Dotenv(os.path.join(os.path.dirname(__file__), ".env"))
os.environ.update(dotenv)
rcsdk = SDK( os.getenv("RINGCENTRAL_CLIENT_ID"),
os.getenv("RINGCENTRAL_CLIENT_SECRET"),
os.getenv("RINGCENTRAL_SERVER_URL") )
platform = rcsdk.platform()
platform.login( os.getenv("RINGCENTRAL_USERNAME"),
os.getenv("RINGCENTRAL_EXTENSION"),
os.getenv("RINGCENTRAL_PASSWORD") )
def pubnub():
try:
eventFilters = [
'/account/~/extension/~/message-store/instant?type=SMS',
'/account/~/extension/~/voicemail'
]
subscription = rcsdk.create_subscription()
subscription.add_events(eventFilters)
subscription.on(Events.notification, on_message)
res = subscription.register()
print("Wait for notifications ...")
while True:
sleep(1)
except KeyboardInterrupt:
print("Failed to subscribe for notifications.")
def on_message(msg):
if "/message-store/instant" in msg['event']:
senderNumber = msg['body']['from']['phoneNumber']
print ("Received a voicemail from: " + senderNumber)
response = platform.post('/account/~/extension/~/sms', {
'from': {'phoneNumber': os.getenv("RINGCENTRAL_USERNAME")},
'to': [{'phoneNumber': senderNumber}],
'text' : 'This is an automatic reply. Thank you for your message!'
})
print('Replied message sent. Message delivery status: ' + response.json().messageStatus)
elif "/voicemail" in msg['event']:
if "phoneNumber" in msg['body']['from']:
senderNumber = msg['body']['from']['phoneNumber']
print ("Received a voicemail from: " + senderNumber)
response = platform.post('/account/~/extension/~/sms', {
'from': {'phoneNumber': os.getenv("RINGCENTRAL_USERNAME")},
'to': [{'phoneNumber': senderNumber}],
'text' : 'This is an automatic reply. Thank you for your voice message! I will get back to you asap.'
})
print("Replied message sent. Message delivery status: " + response.json().messageStatus)
else:
print ("Not an event we are waiting for.")
p = Process(target=pubnub)
try:
p.start()
except KeyboardInterrupt:
p.terminate()