-
Notifications
You must be signed in to change notification settings - Fork 166
/
main.py
executable file
·74 lines (54 loc) · 1.75 KB
/
main.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
#!/usr/bin/python2.7
#
# Copyright Quip 2016
"""Opens a websocket and listens for updates from Quip
This is a sample app for the Quip API - https://quip.com/api/.
"""
import argparse
import json
import quip
import sys
import time
import websocket
PY3 = sys.version_info > (3,)
if PY3:
import _thread as thread
else:
import thread
HEARTBEAT_INTERVAL = 20
def open_websocket(url):
def on_message(ws, message):
print("message:")
print(json.dumps(json.loads(message), indent=4))
def on_error(ws, error):
print("error:")
print(error)
def on_close(ws):
print("### connection closed ###")
def on_open(ws):
print("### connection established ###")
def run(*args):
while True:
time.sleep(HEARTBEAT_INTERVAL)
ws.send(json.dumps({"type": "heartbeat"}))
thread.start_new_thread(run, ())
# websocket.enableTrace(True)
ws = websocket.WebSocketApp(
url, on_message=on_message, on_error=on_error, on_close=on_close)
ws.on_open = on_open
ws.run_forever()
def main():
parser = argparse.ArgumentParser(description="Twitter gateway for Quip.")
parser.add_argument("--access_token", required=True,
help="User's access token")
parser.add_argument("--quip_api_base_url", default=None,
help="Alternative base URL for the Quip API. If none is provided, "
"https://platform.quip.com will be used")
args = parser.parse_args()
quip_client = quip.QuipClient(
access_token=args.access_token,
base_url=args.quip_api_base_url or "https://platform.quip.com")
websocket_info = quip_client.new_websocket()
open_websocket(websocket_info["url"])
if __name__ == "__main__":
main()