-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive-reply.py
76 lines (62 loc) · 2.17 KB
/
receive-reply.py
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
65
66
67
68
69
70
71
72
73
74
75
76
from multiprocessing import Process
from time import sleep
from ringcentral.subscription import Events
from ringcentral import SDK
import os
from dotenv import load_dotenv
load_dotenv(".env")
sdk = SDK( os.getenv("RINGCENTRAL_CLIENT_ID"),
os.getenv("RINGCENTRAL_CLIENT_SECRET"),
os.getenv("RINGCENTRAL_SERVER_URL") )
platform = sdk.platform()
platform.login( os.getenv("RINGCENTRAL_USERNAME"),
os.getenv("RINGCENTRAL_EXTENSION"),
os.getenv("RINGCENTRAL_PASSWORD") )
def main():
def pubnub():
try:
subscription = sdk.create_subscription()
subscription.add_events(['/account/~/extension/~/message-store/instant?type=SMS'])
subscription.on(Events.notification, on_message)
res = subscription.register()
try:
f = open("subid.txt", "w")
print (res.json().id)
f.write(res.json().id)
f.close()
except Exception as e:
print (e)
while True:
sleep(0.1)
except KeyboardInterrupt:
print("Pubnub listener stopped...")
def on_message(msg):
senderNumber = msg['body']['from']['phoneNumber']
response = platform.post('/restapi/v1.0/account/~/extension/~/sms', {
'from': {'phoneNumber': os.getenv("RINGCENTRAL_USERNAME")},
'to': [{'phoneNumber': senderNumber}],
'text' : 'This is an automatic reply'
})
print('SMS replied: %d' % (response.json().id))
def unregister():
try:
f = open("subid.txt", "r")
subId = f.read()
f.close()
if (len(subId)):
response = platform.delete('/restapi/v1.0/subscription/%s' % (subId))
print ("Cancelled old subscription.")
else:
print ("empty")
except Exception as e:
print (e)
p = Process(target=pubnub)
try:
unregister()
p.start()
except KeyboardInterrupt:
p.terminate()
print("Stopped by User")
print("Wait for notification...")
if __name__ == '__main__':
main()