This repository has been archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.py
60 lines (42 loc) · 1.64 KB
/
app.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
import ucwa.actions as actions
from ucwa.config import load_config
from ucwa.events import process_events
from contextlib import closing
import requests.exceptions
import yaml
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
with open('instance.yml', 'r') as instance_f:
instance_config = yaml.load(instance_f)
config = load_config()
resource = instance_config['resource']
token = instance_config['token']
logging.info('registering application against UCWA api')
try:
app = actions.register_application(resource, token, config)
except requests.exceptions.HTTPError as he:
logging.error("Error registering the application, your token might be out"
"of date, run `python authhelper.py` again. - %s "
% he.message)
exit(-1)
logging.info('Registered app %s' % app['id'])
logging.info('listening for events')
event_url = resource + app['_links']['events']['href']
logging.info('setting status to available')
available = actions.set_available(resource, app['id'], token, config)
events_stream = actions.oauth_stream_request(event_url, token, config['redirect_uri'])
event_list = {}
while True:
with closing(events_stream) as r:
# Do things with the response here.
event_response = events_stream.json()
# get communication events
comm_evt = event_response['sender']
if len(comm_evt) > 0:
event_list = comm_evt[0]['events']
process_events(event_list, resource, token, config)
events_stream = actions.oauth_stream_request(
resource + event_response['_links']['next']['href'],
token,
config['redirect_uri'])